Skip to content

Commit

Permalink
Merge pull request #22 from MinatoAquaCrews/dev
Browse files Browse the repository at this point in the history
🔖 v0.2.6.post2: add PluginMetadata
  • Loading branch information
KafCoppelia authored Mar 30, 2023
2 parents 2cd5667 + 247e850 commit d2b6849
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 209 deletions.
9 changes: 8 additions & 1 deletion .github/release-drafter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ version-resolver:
patch:
labels:
- "patch"
default: patch
default: patch

template: |
## What’s Changed
$CHANGES
Changelog: [`$PREVIOUS_TAG...`](https://github.com/MinatoAquaCrews/nonebot_plugin_crazy_thursday/compare/$PREVIOUS_TAG...)
13 changes: 13 additions & 0 deletions .github/workflows/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"

target-branch: "master"
2 changes: 1 addition & 1 deletion .github/workflows/poetry-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ jobs:
python_version: "3.10"
pypi_token: ${{ secrets.PYPI_API_TOKEN }}
build_format: "wheel"
ignore_dev_requirements: "yes"
ignore_dev_requirements: "yes"
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ repos:
- id: check-yaml
- id: check-added-large-files
- id: mixed-line-ending
- id: check-json
- id: check-json
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ _🍗 疯狂星期四 🍗_
</div>

<p align="center">

<a href="https://github.com/MinatoAquaCrews/nonebot_plugin_crazy_thursday/blob/master/LICENSE">
<img src="https://img.shields.io/github/license/MinatoAquaCrews/nonebot_plugin_crazy_thursday?color=blue">
</a>

<a href="https://github.com/nonebot/nonebot2">
<img src="https://img.shields.io/badge/nonebot2-2.0.0rc1+-green">
</a>
<a href="https://github.com/MinatoAquaCrews/nonebot_plugin_crazy_thursday/releases/tag/v0.2.6.post1">

<a href="https://github.com/MinatoAquaCrews/nonebot_plugin_crazy_thursday/releases/tag/v0.2.6.post2">
<img src="https://img.shields.io/github/v/release/MinatoAquaCrews/nonebot_plugin_crazy_thursday?color=orange">
</a>

Expand All @@ -31,12 +31,12 @@ _🍗 疯狂星期四 🍗_
<a href="https://results.pre-commit.ci/latest/github/MinatoAquaCrews/nonebot_plugin_crazy_thursday/master">
<img src="https://results.pre-commit.ci/badge/github/MinatoAquaCrews/nonebot_plugin_crazy_thursday/master.svg" alt="pre-commit.ci status">
</a>

</p>

## 版本

[v0.2.6.post1](https://github.com/MinatoAquaCrews/nonebot_plugin_crazy_thursday/releases/tag/v0.2.6.post1)
[v0.2.6.post2](https://github.com/MinatoAquaCrews/nonebot_plugin_crazy_thursday/releases/tag/v0.2.6.post2)

⚠ 适配nonebot2-2.0.0rc1+

Expand All @@ -46,10 +46,10 @@ _🍗 疯狂星期四 🍗_

2. 文案的默认路径位于**插件同级目录**下;也可放置在别处,在 `.env` 下设置即可;`CRAZY_AUTO_UPDATE` 默认关闭,开启则插件将在启动时自动检查资源更新。例如:

```python
CRAZY_PATH="your-path-to-post.json"
CRAZY_AUTO_UPDATE=false
```
```python
CRAZY_PATH="your-path-to-post.json"
CRAZY_AUTO_UPDATE=false
```

## 功能

Expand All @@ -65,4 +65,4 @@ _🍗 疯狂星期四 🍗_

## 本插件改自

[HoshinoBot-fucking_crazy_thursday](https://github.com/Nicr0n/fucking_crazy_thursday)
[HoshinoBot-fucking_crazy_thursday](https://github.com/Nicr0n/fucking_crazy_thursday)
44 changes: 32 additions & 12 deletions nonebot_plugin_crazy_thursday/__init__.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,67 @@
import random
from typing import List

from nonebot import on_regex
from nonebot.matcher import Matcher
from nonebot.params import Depends, RegexMatched
from nonebot.plugin import PluginMetadata

from .config import *

__crazy_thursday_version__ = "v0.2.6.post1"
__crazy_thursday_notes__ = f"""
__crazy_thursday_version__ = "v0.2.6.post2"
__crazy_thursday_usages__ = f"""
KFC疯狂星期四 {__crazy_thursday_version__}
[疯狂星期X] 随机输出KFC疯狂星期四文案
[狂乱X曜日] 随机输出KFC疯狂星期四文案""".strip()

__plugin_meta__ = PluginMetadata(
name="疯狂星期四",
description="持续疯狂!KFC疯狂星期四🍗",
usage=__crazy_thursday_usages__,
extra={
"author": "KafCoppelia <[email protected]>",
"version": __crazy_thursday_version__
}
)

crazy_cn = on_regex(pattern=r"^疯狂星期\S$", priority=15, block=False)
crazy_jp = on_regex(pattern=r"^狂乱\S曜日$", priority=15, block=False)


async def get_weekday_cn(arg: str = RegexMatched()) -> str:
return arg[-1].replace("天", "日")


async def get_weekday_jp(arg: str = RegexMatched()) -> str:
return arg[2]



@crazy_cn.handle()
async def _(matcher: Matcher, weekday: str = Depends(get_weekday_cn)):
await matcher.finish(rndKfc(weekday))
await matcher.finish(randomKFC(weekday))


@crazy_jp.handle()
async def _(matcher: Matcher, weekday: str = Depends(get_weekday_jp)):
await matcher.finish(rndKfc(weekday))
await matcher.finish(randomKFC(weekday))


def rndKfc(day: str) -> str:
def randomKFC(day: str) -> str:
# jp en cn
tb: List[str] = ["月", "Monday", "一", "火", "Tuesday", "二", "水", "Wednesday", "三", "木", "Thursday", "四", "金", "Friday", "五", "土", "Saturday", "六", "日", "Sunday", "日"]
tb: List[str] = ["月", "Monday", "一", "火", "Tuesday", "二", "水", "Wednesday", "三",
"木", "Thursday", "四", "金", "Friday", "五", "土", "Saturday", "六", "日", "Sunday", "日"]
if day not in tb:
return "给个准确时间,OK?"

# Get the weekday group index
idx: int = int(tb.index(day)/3)*3

# json数据存放路径
path: Path = crazy_config.crazy_path / "post.json"

# 将json对象加载到数组
with open(path, "r", encoding="utf-8") as f:
kfc = json.load(f).get("post")

# 随机选取数组中的一个对象,并替换日期
return random.choice(kfc).replace("木曜日", tb[idx] + "曜日").replace("Thursday", tb[idx+1]).replace("thursday", tb[idx+1]).replace("星期四", "星期" + tb[idx+2]).replace("周四", "周" + tb[idx+2]).replace("礼拜四", "礼拜" + tb[idx+2])
return random.choice(kfc).replace("木曜日", tb[idx] + "曜日").replace("Thursday", tb[idx+1]).replace("thursday", tb[idx+1]).replace("星期四", "星期" + tb[idx+2]).replace("周四", "周" + tb[idx+2]).replace("礼拜四", "礼拜" + tb[idx+2])
22 changes: 14 additions & 8 deletions nonebot_plugin_crazy_thursday/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from pathlib import Path
from typing import Any, Dict, Union

import httpx
from nonebot import get_driver
from nonebot.log import logger
from pydantic import BaseModel, Extra
from pathlib import Path
from typing import Union, Dict, Any
import httpx

try:
import ujson as json
except ModuleNotFoundError:
Expand All @@ -29,7 +31,7 @@ class ResourceError(Exception):

async def download_url() -> Union[Dict[str, Any], None]:
url: str = "https://raw.fgit.ml/MinatoAquaCrews/nonebot_plugin_crazy_thursday/master/nonebot_plugin_crazy_thursday/post.json"

async with httpx.AsyncClient() as client:
for i in range(3):
try:
Expand All @@ -40,7 +42,8 @@ async def download_url() -> Union[Dict[str, Any], None]:
return response.json()

except Exception:
logger.warning(f"Error occured when downloading {url}, {i+1}/3")
logger.warning(
f"Error occured when downloading {url}, {i+1}/3")

logger.warning("Abort downloading")
return None
Expand All @@ -54,7 +57,7 @@ async def kfc_post_check() -> None:
Otherwise just abort it.
'''
json_path: Path = crazy_config.crazy_path / "post.json"

cur_version: float = 0
if json_path.exists():
with json_path.open("r", encoding="utf-8") as f:
Expand All @@ -74,11 +77,14 @@ async def kfc_post_check() -> None:
try:
version: float = response.get("version", 0)
except KeyError:
logger.warning("KFC post text resource downloaded incompletely! Please check!")
logger.warning(
"KFC post text resource downloaded incompletely! Please check!")
raise DownloadError

# Update when there is a newer version
if version > cur_version:
with json_path.open("w", encoding="utf-8") as f:
json.dump(response, f, ensure_ascii=False, indent=4)
logger.info(f"Updated post.json, version: {cur_version} -> {version}")

logger.info(
f"Updated post.json, version: {cur_version} -> {version}")
Loading

0 comments on commit d2b6849

Please sign in to comment.