nonebot-plugin-apod 0.0.2__tar.gz → 0.0.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,85 @@
1
+ Metadata-Version: 2.1
2
+ Name: nonebot-plugin-apod
3
+ Version: 0.0.4
4
+ Summary: 定时发送 NASA 每日提供的天文图片
5
+ Home-page: https://github.com/lyqgzbl/nonebot-plugin-apod
6
+ License: MIT
7
+ Keywords: nonebot2,NASA
8
+ Author: lyqgzbl
9
+ Author-email: admin@lyqgzbl.com
10
+ Requires-Python: >=3.8,<4.0
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3.8
14
+ Classifier: Programming Language :: Python :: 3.9
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
19
+ Requires-Dist: httpx (>=0.28.0,<0.29.0)
20
+ Requires-Dist: nonebot-plugin-alconna (>=0.54.0,<0.55.0)
21
+ Requires-Dist: nonebot-plugin-apscheduler (>=0.5.0,<0.6.0)
22
+ Requires-Dist: nonebot-plugin-localstore (>=0.7.0,<0.8.0)
23
+ Requires-Dist: nonebot-plugin-send-anything-anywhere (>=0.7.0,<0.8.0)
24
+ Requires-Dist: nonebot2 (>=2.2.1,<3.0.0)
25
+ Project-URL: Documentation, https://github.com/lyqgzbl/nonebot-plugin-apod#readme
26
+ Project-URL: Repository, https://github.com/lyqgzbl/nonebot-plugin-apod
27
+ Description-Content-Type: text/markdown
28
+
29
+ <!-- markdownlint-disable MD033 MD036 MD041 -->
30
+
31
+ <div align="center">
32
+
33
+ <a href="https://v2.nonebot.dev/store">
34
+ <img src="https://raw.githubusercontent.com/A-kirami/nonebot-plugin-template/resources/nbp_logo.png" width="180" height="180" alt="NoneBotPluginLogo">
35
+ </a>
36
+
37
+ <p>
38
+ <img src="https://raw.githubusercontent.com/lgc-NB2Dev/readme/main/template/plugin.svg" alt="NoneBotPluginText">
39
+ </p>
40
+
41
+ # nonebot-plugin-apod
42
+
43
+ _✨ 每日天文一图 ✨_
44
+
45
+ ![License](https://img.shields.io/pypi/l/nonebot-plugin-apod)
46
+ ![PyPI](https://img.shields.io/pypi/v/nonebot-plugin-apod.svg)
47
+ ![Python](https://img.shields.io/badge/python-3.8+-blue.svg)
48
+ [![NoneBot Registry](https://img.shields.io/endpoint?url=https%3A%2F%2Fnbbdg.lgc2333.top%2Fplugin%2Fnonebot-plugin-apod)](https://registry.nonebot.dev/plugin/nonebot-plugin-apod:nonebot_plugin_apod)
49
+ [![Supported Adapters](https://img.shields.io/endpoint?url=https%3A%2F%2Fnbbdg.lgc2333.top%2Fplugin-adapters%2Fnonebot-plugin-send-anything-anywhere)](https://registry.nonebot.dev/plugin/nonebot-plugin-send-anything-anywhere:nonebot_plugin_saa)
50
+
51
+ </div>
52
+
53
+ ## 安装
54
+ 使用nb-cli [推荐]
55
+ ```shell
56
+ nb plugin install nonebot-plugin-apod
57
+ ```
58
+ 使用pip
59
+ ```shell
60
+ pip install nonebot-plugin-apod
61
+ ```
62
+
63
+ ## 使用
64
+ 命令需要加 [NoneBot 命令前缀](https://nonebot.dev/docs/appendices/config#command-start-和-command-separator) (默认为`/`)
65
+ 命令需要用户为 [SuperUsers](https://nonebot.dev/docs/appendices/config#superusers)
66
+ 使用命令`APOD`/`apod`触发插件
67
+ 命令选项`状态` 查询定时任务状态
68
+ 命令选项`关闭` 关闭定时任务
69
+ 命令选项`开启` 开启定时任务
70
+
71
+ ## 配置项
72
+
73
+ 配置方式:直接在 NoneBot 全局配置文件中添加以下配置项即可
74
+
75
+ ### apod_api_key [必填]
76
+
77
+ - 类型:`str`
78
+ - 默认值:`None`
79
+ - 说明:用于获取每日天文一图的 NASA API Key
80
+
81
+ ### apod_default_send_time [选填]
82
+
83
+ - 类型:`str`
84
+ - 默认值:`13:00`
85
+ - 说明:每日天文一图的默认发送时间
@@ -0,0 +1,57 @@
1
+ <!-- markdownlint-disable MD033 MD036 MD041 -->
2
+
3
+ <div align="center">
4
+
5
+ <a href="https://v2.nonebot.dev/store">
6
+ <img src="https://raw.githubusercontent.com/A-kirami/nonebot-plugin-template/resources/nbp_logo.png" width="180" height="180" alt="NoneBotPluginLogo">
7
+ </a>
8
+
9
+ <p>
10
+ <img src="https://raw.githubusercontent.com/lgc-NB2Dev/readme/main/template/plugin.svg" alt="NoneBotPluginText">
11
+ </p>
12
+
13
+ # nonebot-plugin-apod
14
+
15
+ _✨ 每日天文一图 ✨_
16
+
17
+ ![License](https://img.shields.io/pypi/l/nonebot-plugin-apod)
18
+ ![PyPI](https://img.shields.io/pypi/v/nonebot-plugin-apod.svg)
19
+ ![Python](https://img.shields.io/badge/python-3.8+-blue.svg)
20
+ [![NoneBot Registry](https://img.shields.io/endpoint?url=https%3A%2F%2Fnbbdg.lgc2333.top%2Fplugin%2Fnonebot-plugin-apod)](https://registry.nonebot.dev/plugin/nonebot-plugin-apod:nonebot_plugin_apod)
21
+ [![Supported Adapters](https://img.shields.io/endpoint?url=https%3A%2F%2Fnbbdg.lgc2333.top%2Fplugin-adapters%2Fnonebot-plugin-send-anything-anywhere)](https://registry.nonebot.dev/plugin/nonebot-plugin-send-anything-anywhere:nonebot_plugin_saa)
22
+
23
+ </div>
24
+
25
+ ## 安装
26
+ 使用nb-cli [推荐]
27
+ ```shell
28
+ nb plugin install nonebot-plugin-apod
29
+ ```
30
+ 使用pip
31
+ ```shell
32
+ pip install nonebot-plugin-apod
33
+ ```
34
+
35
+ ## 使用
36
+ 命令需要加 [NoneBot 命令前缀](https://nonebot.dev/docs/appendices/config#command-start-和-command-separator) (默认为`/`)
37
+ 命令需要用户为 [SuperUsers](https://nonebot.dev/docs/appendices/config#superusers)
38
+ 使用命令`APOD`/`apod`触发插件
39
+ 命令选项`状态` 查询定时任务状态
40
+ 命令选项`关闭` 关闭定时任务
41
+ 命令选项`开启` 开启定时任务
42
+
43
+ ## 配置项
44
+
45
+ 配置方式:直接在 NoneBot 全局配置文件中添加以下配置项即可
46
+
47
+ ### apod_api_key [必填]
48
+
49
+ - 类型:`str`
50
+ - 默认值:`None`
51
+ - 说明:用于获取每日天文一图的 NASA API Key
52
+
53
+ ### apod_default_send_time [选填]
54
+
55
+ - 类型:`str`
56
+ - 默认值:`13:00`
57
+ - 说明:每日天文一图的默认发送时间
@@ -13,11 +13,11 @@ require("nonebot_plugin_localstore")
13
13
  require("nonebot_plugin_apscheduler")
14
14
  import nonebot_plugin_localstore as store
15
15
  from nonebot_plugin_apscheduler import scheduler
16
- from nonebot_plugin_alconna import Args, Match, Option, Alconna, CommandMeta, on_alconna
17
- from nonebot_plugin_saa import SaaTarget, enable_auto_select_bot, PlatformTarget, get_target
16
+ from nonebot_plugin_alconna import Args, Match, Option, Alconna, CommandMeta, on_alconna, UniMessage
17
+ from nonebot_plugin_saa import Text, Image, SaaTarget, enable_auto_select_bot, PlatformTarget, get_target
18
18
 
19
19
  from .config import Config
20
- from .apod import send_apod, remove_apod_task, schedule_apod_task
20
+ from .apod import send_apod, remove_apod_task, schedule_apod_task, fetch_apod_data
21
21
 
22
22
 
23
23
  __plugin_meta__ = PluginMetadata(
@@ -35,15 +35,17 @@ __plugin_meta__ = PluginMetadata(
35
35
 
36
36
  enable_auto_select_bot()
37
37
  plugin_config = get_plugin_config(Config)
38
- if not plugin_config.nasa_api_key:
38
+ apod_cache_image = store.get_plugin_cache_file("apod_image")
39
+ task_config_file = store.get_plugin_data_file("apod_task_config.json")
40
+ if not plugin_config.apod_api_key:
39
41
  logger.opt(colors=True).warning("<yellow>缺失必要配置项 'nasa_api_key',已禁用该插件</yellow>")
40
42
  def is_enable() -> Rule:
41
43
  def _rule() -> bool:
42
- return bool(plugin_config.nasa_api_key)
44
+ return bool(plugin_config.apod_api_key)
43
45
  return Rule(_rule)
44
46
 
45
47
 
46
- apod = on_alconna(
48
+ apod_setting = on_alconna(
47
49
  Alconna(
48
50
  "apod",
49
51
  Option("状态|status"),
@@ -67,6 +69,19 @@ apod = on_alconna(
67
69
  )
68
70
 
69
71
 
72
+ apod = on_alconna(
73
+ Alconna(
74
+ "今日天文一图",
75
+ meta=CommandMeta(
76
+ description="获取今日天文一图",
77
+ example="/今日天文一图",
78
+ ),
79
+ ),
80
+ rule=is_enable(),
81
+ use_cmd_start=True,
82
+ )
83
+
84
+
70
85
  def is_valid_time_format(time_str: str) -> bool:
71
86
  if not re.match(r"^\d{1,2}:\d{2}$", time_str):
72
87
  return False
@@ -77,19 +92,34 @@ def is_valid_time_format(time_str: str) -> bool:
77
92
  return False
78
93
 
79
94
 
80
- @apod.assign("status")
95
+ @apod.handle()
96
+ async def apod_handle():
97
+ if not apod_cache_image.exists():
98
+ is_image = await fetch_apod_data()
99
+ else:
100
+ is_image = True
101
+ if is_image and apod_cache_image.exists():
102
+ try:
103
+ await UniMessage.text("今日天文一图为").image(raw=apod_cache_image.read_bytes()).send(reply_to=True)
104
+ except Exception as e:
105
+ logger.error(f"发送天文一图时发生错误:{e}")
106
+ await apod.finish("发送今日天文一图失败,请稍后再试")
107
+ else:
108
+ await apod.finish("今日 NASA 提供的为天文视频")
109
+
110
+
111
+ @apod_setting.assign("status")
81
112
  async def apod_status(event):
82
- task_config_file = store.get_plugin_data_file("apod_task_config.json")
83
113
  if not task_config_file.exists():
84
- await apod.finish("NASA 每日天文一图定时任务未开启")
114
+ await apod_setting.finish("NASA 每日天文一图定时任务未开启")
85
115
  try:
86
116
  with task_config_file.open("r", encoding="utf-8") as f:
87
117
  config = json.load(f)
88
118
  tasks = config.get("tasks", [])
89
119
  except Exception as e:
90
- await apod.finish(f"加载任务配置时发生错误:{e}")
120
+ await apod_setting.finish(f"加载任务配置时发生错误:{e}")
91
121
  if not tasks:
92
- await apod.finish("NASA 每日天文一图定时任务未开启")
122
+ await apod_setting.finish("NASA 每日天文一图定时任务未开启")
93
123
  current_target = get_target(event)
94
124
  for task in tasks:
95
125
  target_data = task["target"]
@@ -103,31 +133,31 @@ async def apod_status(event):
103
133
  job.next_run_time.strftime("%Y-%m-%d %H:%M:%S")
104
134
  if job.next_run_time else "未知"
105
135
  )
106
- await apod.finish(f"NASA 每日天文一图定时任务已开启 | 下次发送时间: {next_run}")
136
+ await apod_setting.finish(f"NASA 每日天文一图定时任务已开启 | 下次发送时间: {next_run}")
107
137
  else:
108
- await apod.finish("NASA 每日天文一图定时任务未开启")
109
- await apod.finish("NASA 每日天文一图定时任务未开启")
138
+ await apod_setting.finish("NASA 每日天文一图定时任务未开启")
139
+ await apod_setting.finish("NASA 每日天文一图定时任务未开启")
110
140
 
111
141
 
112
- @apod.assign("stop")
142
+ @apod_setting.assign("stop")
113
143
  async def apod_stop(target: SaaTarget):
114
144
  remove_apod_task(target)
115
- await apod.finish("已关闭 NASA 每日天文一图定时任务")
145
+ await apod_setting.finish("已关闭 NASA 每日天文一图定时任务")
116
146
 
117
147
 
118
- @apod.assign("start")
148
+ @apod_setting.assign("start")
119
149
  async def apod_start(send_time: Match[str], target: SaaTarget):
120
150
  if send_time.available:
121
151
  time = send_time.result
122
152
  if not is_valid_time_format(time):
123
- await apod.send("时间格式不正确,请使用 HH:MM 格式")
153
+ await apod_setting.send("时间格式不正确,请使用 HH:MM 格式")
124
154
  try:
125
155
  schedule_apod_task(time, target)
126
- await apod.send(f"已开启 NASA 每日天文一图定时任务,发送时间为 {time}")
156
+ await apod_setting.send(f"已开启 NASA 每日天文一图定时任务,发送时间为 {time}")
127
157
  except Exception as e:
128
158
  logger.error(f"设置 NASA 每日天文一图定时任务时发生错误:{e}")
129
- await apod.finish("设置 NASA 每日天文一图定时任务时发生错误")
159
+ await apod_setting.finish("设置 NASA 每日天文一图定时任务时发生错误")
130
160
  else:
131
- default_time = plugin_config.default_apod_send_time
161
+ default_time = plugin_config.apod_default_send_time
132
162
  schedule_apod_task(default_time, target)
133
- await apod.finish(f"已开启 NASA 每日天文一图定时任务,默认发送时间为 {default_time}")
163
+ await apod_setting.finish(f"已开启 NASA 每日天文一图定时任务,默认发送时间为 {default_time}")
@@ -1,17 +1,19 @@
1
- import datetime
2
- import httpx
3
1
  import json
4
- import nonebot_plugin_localstore as store
2
+ import httpx
3
+ import datetime
5
4
 
6
- from nonebot import get_plugin_config, get_bot
7
5
  from nonebot.log import logger
6
+ import nonebot_plugin_localstore as store
7
+ from nonebot import get_plugin_config, get_bot
8
8
  from nonebot_plugin_apscheduler import scheduler
9
9
  from nonebot_plugin_saa import Text, Image, PlatformTarget
10
+
10
11
  from .config import Config
11
12
 
12
13
  plugin_config = get_plugin_config(Config)
13
14
  NASA_API_URL = "https://api.nasa.gov/planetary/apod"
14
- NASA_API_KEY = plugin_config.nasa_api_key
15
+ NASA_API_KEY = plugin_config.apod_api_key
16
+ apod_cache_image = store.get_plugin_cache_file("apod_image")
15
17
  task_config_file = store.get_plugin_data_file("apod_task_config.json")
16
18
 
17
19
 
@@ -43,31 +45,36 @@ def load_task_configs():
43
45
  return []
44
46
 
45
47
 
46
- async def fetch_apod_data():
48
+ async def fetch_apod_data() -> bool:
47
49
  try:
48
50
  async with httpx.AsyncClient() as client:
49
51
  response = await client.get(NASA_API_URL, params={"api_key": NASA_API_KEY})
50
52
  response.raise_for_status()
51
- return response.json()
53
+ data = response.json()
54
+ if data.get("media_type") == "image" and "url" in data:
55
+ image_url = data["url"]
56
+ image_response = await client.get(image_url)
57
+ image_response.raise_for_status()
58
+ apod_cache_image.write_bytes(image_response.content)
59
+ return True
60
+ else:
61
+ return False
52
62
  except httpx.RequestError as e:
53
63
  logger.error(f"获取 NASA 每日天文一图数据时发生错误: {e}")
54
- return None
64
+ return False
55
65
 
56
66
 
57
67
  async def send_apod(target: PlatformTarget):
58
- apod_data = await fetch_apod_data()
59
- if apod_data:
60
- title = apod_data.get("title", "NASA APOD")
61
- url = apod_data.get("url")
68
+ apod_is_image = await fetch_apod_data()
69
+ if apod_is_image:
62
70
  try:
63
- await Image(url).send_to(target, bot=get_bot())
64
- await Text(f"链接:{url}").send_to(target, bot=get_bot())
71
+ await Text("今日天文一图为").send_to(target, bot=get_bot())
72
+ await Image(str(apod_cache_image)).send_to(target, bot=get_bot())
65
73
  except Exception as e:
66
74
  logger.error(f"发送 NASA 每日天文一图时发生错误:{e}")
67
75
  await Text("发送 NASA 每日天文一图时发生错误").send_to(target, bot=get_bot())
68
76
  else:
69
- logger.error("无法获取今天的天文图片")
70
- await Text("无法获取今天的天文图片。").send_to(target, bot=get_bot())
77
+ await Text("今日 NASA 提供的为天文视频").send_to(target, bot=get_bot())
71
78
 
72
79
 
73
80
  def schedule_apod_task(send_time: str, target: PlatformTarget):
@@ -118,4 +125,13 @@ try:
118
125
  schedule_apod_task(send_time, target)
119
126
  logger.debug("已恢复所有 NASA 每日天文一图定时任务")
120
127
  except Exception as e:
121
- logger.error(f"恢复 NASA 每日天文一图定时任务时发生错误:{e}")
128
+ logger.error(f"恢复 NASA 每日天文一图定时任务时发生错误:{e}")
129
+
130
+
131
+ @scheduler.scheduled_job("cron", hour=13, minute=0, id="clear_apod_cache")
132
+ async def clear_apod_cache():
133
+ if apod_cache_image.exists():
134
+ apod_cache_image.unlink()
135
+ logger.debug("apod缓存图片已清除")
136
+ else:
137
+ logger.debug("apod缓存图片不存在")
@@ -3,5 +3,5 @@ from pydantic import BaseModel
3
3
 
4
4
 
5
5
  class Config(BaseModel):
6
- nasa_api_key: Optional[str] = None
7
- default_apod_send_time: str = "13:00"
6
+ apod_api_key: Optional[str] = None
7
+ apod_default_send_time: str = "13:00"
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "nonebot-plugin-apod"
3
- version = "0.0.2"
3
+ version = "0.0.4"
4
4
  description = "定时发送 NASA 每日提供的天文图片"
5
5
  readme = "README.md"
6
6
  authors = ["lyqgzbl <admin@lyqgzbl.com>"]
@@ -1,29 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: nonebot-plugin-apod
3
- Version: 0.0.2
4
- Summary: 定时发送 NASA 每日提供的天文图片
5
- Home-page: https://github.com/lyqgzbl/nonebot-plugin-apod
6
- License: MIT
7
- Keywords: nonebot2,NASA
8
- Author: lyqgzbl
9
- Author-email: admin@lyqgzbl.com
10
- Requires-Python: >=3.8,<4.0
11
- Classifier: License :: OSI Approved :: MIT License
12
- Classifier: Programming Language :: Python :: 3
13
- Classifier: Programming Language :: Python :: 3.8
14
- Classifier: Programming Language :: Python :: 3.9
15
- Classifier: Programming Language :: Python :: 3.10
16
- Classifier: Programming Language :: Python :: 3.11
17
- Classifier: Programming Language :: Python :: 3.12
18
- Classifier: Programming Language :: Python :: 3.13
19
- Requires-Dist: httpx (>=0.28.0,<0.29.0)
20
- Requires-Dist: nonebot-plugin-alconna (>=0.54.0,<0.55.0)
21
- Requires-Dist: nonebot-plugin-apscheduler (>=0.5.0,<0.6.0)
22
- Requires-Dist: nonebot-plugin-localstore (>=0.7.0,<0.8.0)
23
- Requires-Dist: nonebot-plugin-send-anything-anywhere (>=0.7.0,<0.8.0)
24
- Requires-Dist: nonebot2 (>=2.2.1,<3.0.0)
25
- Project-URL: Documentation, https://github.com/lyqgzbl/nonebot-plugin-apod#readme
26
- Project-URL: Repository, https://github.com/lyqgzbl/nonebot-plugin-apod
27
- Description-Content-Type: text/markdown
28
-
29
- # nonebot-plugin-apod
@@ -1 +0,0 @@
1
- # nonebot-plugin-apod