Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:获取冷笑话、中国石油化工92号汽油指导价 #50

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,199 changes: 3,199 additions & 0 deletions assets/gasoline_price_china/city_ids.json

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions config.ini.example
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,40 @@ custom_command_key_dict = {
"bili-hot": ["bh"],
"weather": ["w", "温度"]
}


[gasoline-price-cron]
# 汽油价格自动推送定时任务
# 是否开启汽油价格自动推送定时任务(True/<<False>>)
gasoline_price_cron_enabled = True
# 自动推送规则列表(JSON格式)
# 每个规则是一个字典,包含两个字段:cron, tasks
# cron: 定时任务的 Cron 表达式,即执行时间。详见:https://apscheduler.readthedocs.io/en/3.x/modules/triggers/cron.html
# tasks: 任务内容列表,即每次执行的任务,包含三个字段:to_persons, to_groups, city
# to_persons: 消息接收者(们)的微信名称(仅用户)
# to_groups: 消息接收群名称(仅群)
# city: 城市名称
gasoline_price_cron_rule_list = [ {
"cron": {
"year": "*",
"month": "*",
"day": "*",
"week": "*",
"day_of_week": "*",
"hour": "7",
"minute": "0",
"second": "0",
"start_date": null,
"end_date": null,
"timezone": "Asia/Shanghai"
},
"tasks": [ {
"city": "广州",
"to_persons": ["张三", "Tom"],
"to_groups": ["测试群"]
}, {
"city": "北京",
"to_persons": ["李四"],
"to_groups": []
} ]
} ]
155 changes: 155 additions & 0 deletions docs/config_detail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# ⚙️ 配置说明

本篇文档仅包含配置文件中部分复杂配置的详细说明,更多配置请查看项目[自述文件](../README.md#配置文件)

## Weather Cron 配置详细

- `weather_cron_enabled`: 功能开关,是否开启定时天气推送
- `weather_cron_rules`: 推送规则列表,每个规则包含两个字段:`cron` 和 `tasks`
- `cron`: 定时任务的时间设置,包含以下字段:
- `year`: 4位数年份,可以是任何值或者特定的年份
- `month`: 月份,可以是1-12的任何值,或者特定的月份(支持英文月份缩写 jan - dec)
- `day`: 月份中的日期,可以是1-31的任何值,或者特定的日期
- `week`: ISO周数,可以是1-53的任何值,或者特定的周数
- `day_of_week`: 一周中的某一天,可以是0(周一)-6(周日)的任何值,或者特定的一天(支持工作日缩写 mon - sun)
- `hour`: 小时,可以是0-23的任何值,或者特定的小时
- `minute`: 分钟,可以是0-59的任何值,或者特定的分钟
- `second`: 秒,可以是0-59的任何值,或者特定的秒
- `start_date`: 定时任务的开始日期
- `end_date`: 定时任务的结束日期
- `timezone`: 用于日期/时间计算的时区(默认为调度器时区)
- `tasks`: 推送任务列表,每个任务包含三个字段:`city`, `to_persons` 和 `to_groups`
- `city`: 推送天气的城市名
- `to_persons`: 推送目标用户列表,即消息接收用户
- `to_groups`: 推送目标群列表,即消息接收群

以下表格列出了除`timezone`外所有字段中可以使用的表达式。一个字段中可以用逗号分隔多个表达式。

| 表达式 | 字段 | 描述 |
| --- | --- | --- |
| `*` | 任何字段 | 在每个值上触发 |
| `*/a` | 任何字段 | 从最小值开始,每隔a个值触发 |
| `a-b` | 任何字段 | 在a-b范围内的任何值上触发(a必须小于b) |
| `a-b/c` | 任何字段 | 在a-b范围内,每隔c个值触发 |
| `xth y` | `day`字段 | 在月份中第x次出现的工作日y上触发 |
| `last x` | `day`字段 | 在月份中最后一次出现的工作日x上触发 |
| `last` | `day`字段 | 在月份的最后一天触发 |
| `x,y,z` | 任何字段 | 在任何匹配的表达式上触发;可以组合上述表达式中的任何数量 |

### 配置示例

<details>
<summary>
<b>[示例一]</b> 每天早上7点推送北京天气给张三、文件传输助手和家人群,推送广州天气给李四
</summary>

```ini
weather_cron_rules = [ {
"cron": {
"year": "*",
"month": "*",
"day": "*",
"week": "*",
"day_of_week": "*",
"hour": "7",
"minute": "0",
"second": "0",
"start_date": null,
"end_date": null,
"timezone": "Asia/Shanghai"
},
"tasks": [ {
"city": "北京",
"to_persons": ["张三", "文件传输助手"],
"to_groups": ["家人群"]
}, {
"city": "广州",
"to_persons": ["李四"],
"to_groups": []
} ]
} ]
```

</details>

<details>
<summary>
<b>[示例二]</b> 每周一到周五早上8点30分推送广州天气给张三和李四,每周六到周日下午6点推送广州天气给张三
</summary>

```ini
weather_cron_rules = [ {
"cron": {
"year": "*",
"month": "*",
"day": "*",
"week": "*",
"day_of_week": "1-5",
"hour": "8",
"minute": "30",
"second": "0",
"start_date": null,
"end_date": null,
"timezone": "Asia/Shanghai"
},
"tasks": [ {
"city": "广州",
"to_persons": ["张三", "李四"],
"to_groups": []
} ]
}, {
"cron": {
"year": "*",
"month": "*",
"day": "*",
"week": "*",
"day_of_week": "6,0",
"hour": "18",
"minute": "0",
"second": "0",
"start_date": null,
"end_date": null,
"timezone": "Asia/Shanghai"
},
"tasks": [ {
"city": "广州",
"to_persons": ["张三"],
"to_groups": []
} ]
} ]
```

</details>

<details>
<summary>
<b>[示例三]</b> 一月的每一天每整点推送一次深圳天气给张三
</summary>

```ini
weather_cron_rules = [ {
"cron": {
"year": "*",
"month": "1",
"day": "*",
"week": "*",
"day_of_week": "*",
"hour": "*",
"minute": "0",
"second": "0",
"start_date": null,
"end_date": null,
"timezone": "Asia/Shanghai"
},
"tasks": [ {
"city": "深圳",
"to_persons": ["张三"],
"to_groups": []
} ]
} ]
```

</details>

> [!TIP]
> 更多关于`cron`定时器查看[APScheduler文档](https://apscheduler.readthedocs.io/en/3.x/modules/triggers/cron.html)
Empty file.
33 changes: 33 additions & 0 deletions tests/commands/test_trivia/test_trivia.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import unittest
import json
from requests import Response
from wechatter.commands._commands import trivia
from wechatter.exceptions import Bs4ParsingError


class TestTriviaCommand(unittest.TestCase):

def setUp(self):
with open("tests/commands/test_trivia/trivia_response.html.test") as f:
r_html = f.read()
self.response = Response()
self.response._content = r_html.encode('utf-8')
with open("tests/commands/test_trivia/trivia_data.json") as f:
self.trivia_list = json.load(f)

def test_parse_trivia_response_success(self):
result = trivia._parse_trivia_response(self.response)
self.assertListEqual(result, self.trivia_list)

def test_parse_trivia_response_failure(self):
with self.assertRaises(Bs4ParsingError):
trivia._parse_trivia_response(Response())

def test_generate_trivia_message_success(self):
result = trivia._generate_trivia_message(self.trivia_list, 666)
true_result = "✨=====冷知识=====✨"
self.assertIn(true_result, result)

def test_generate_trivia_message_empty_list(self):
result = trivia._generate_trivia_message([], 666)
self.assertEqual(result, "获取冷知识失败")
17 changes: 17 additions & 0 deletions tests/commands/test_trivia/trivia_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
"据汇集了大约7万名网友的出轨秘事的《2016中国人出轨态度调查报告》发现,83%的男生和64.1%的女生都想过出轨,但把出轨付诸行动的男生有60.2%,而女生却连40%都不到。",
"有去过以色列签证记录的,是百分百去不了黎巴嫩的,没有任何商量的余地。",
"根据《三大平台种草力研究报告》,67.8%的用户认为线上种草内容对选择某个商品和最终的购买选择有很大的影响。",
"黎巴嫩的建军节是8月1号。PS:与我大中华建军节时间一样。",
"在19世纪的时候,世界上一部分国家的钞票都是塑料材质而不是纸质的,而这么做的主要目的是为了防止钞票受潮出现破损。",
"在日本可以租赁婚礼嘉宾。对于一些希望婚礼足够热闹,或者缺少亲朋好友的人而言,可以采用这项目服务。这些嘉宾可能从来都不认识新婚夫妇,但他们会在自己的职业范围内为新婚夫妇带来祝福,并尽可能地调动婚礼现场的气氛。",
"黎巴嫩第二大城市叫的黎波里,和利比亚的首都正好重名。",
"2007年的电影《灵动:鬼影实录》成本仅为15000美元,但最终票房却高达1.93亿美元。PS:一万两千八百多倍的回报,可遇不可求~",
"为啥一下雨就想睡觉?因为雨天昏暗的光线,会刺激大脑中一个叫松果体的结构,分泌一种叫褪黑素的激素。而褪黑素正是调节睡眠和昼夜节律的“传令兵”。",
"细菌和病毒可以被冷冻数百万年,并且仍然具有传染性,如果它们被发现并设法感染人或动物,那么灾难性的后果将永无止境,而冻土现在每天都在一点一点地融化……",
"黎巴嫩很多偏远的地方,依然保持着落后的婚俗,比如童婚,近亲结婚等,而且这边结婚的时候非常喜欢热闹,讲排场,婚礼要连续举办七天,婚礼现场不放鞭炮,而是鸣枪助兴。",
"黎巴嫩每年5月份的第一个星期天都会在首都贝鲁特的郊区举办赛狗节,胜者会得到500美金的奖励,狗狗会赠送半年的狗粮。",
"大象可以通过它们的额头呼吸。因为它们在额头较高的位置有连通肺部的内鼻孔。",
"一项对于90后下班状态的调研报告显示,超7成90后不等领导下班就先走,超3成年轻人从未准点下班,超6成90后愿意接受有偿加班。95%的90后不想带电脑回家,但事实上66%还是会带电脑回家。",
"麻雀有多礼貌?当一排麻雀站在电线杆上时,如果新的麻雀即将落下,大家都会主动让出空间。"
]
Loading
Loading