forked from OreosLab/checkinpanel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_news.py
64 lines (57 loc) · 2.08 KB
/
api_news.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*-
"""
cron: 40 7 * * *
new Env('每日新闻');
"""
import re
import json
import traceback
import requests
from notify_mtr import send
from utils import get_data
class News:
def get_obj_key_value(self, data: dict, obj_name: str) -> str:
if data.get(obj_name) != {}:
msg = ""
need_obj = data.get(obj_name)
items = need_obj.items()
for key, value in items:
if key == "content":
for i in value:
msg += str(i)
msg += "\n"
elif (
type(value) is not bool
and len(value) != 0
and not bool(re.search("[a-z]", str(value)))
):
msg += str(value) + "\n"
return msg
def main(self):
msg = ""
try:
res = requests.get(url=f"https://news.topurl.cn/api").json()
if res.get("code") == 200:
data = res.get("data", {})
if data.get("newsList") != []:
msg += "\n📮 每日新闻 📮\n"
no = 1
for news in data.get("newsList", []):
msg += f'{str(no).zfill(2)}. {news.get("title", "")}\n'
no += 1
if data.get("historyList") != []:
msg += "\n🎬 历史上的今天 🎬\n"
for history in data.get("historyList", []):
msg += f'{history.get("event", "")}\n'
msg += "\n🧩 天天成语 🧩\n" + self.get_obj_key_value(data, "phrase")
msg += "\n🎻 慧语香风 🎻\n" + self.get_obj_key_value(data, "sentence")
msg += "\n🎑 诗歌天地 🎑\n" + self.get_obj_key_value(data, "poem")
except Exception:
msg += f"每日新闻: 异常 {traceback.format_exc()}"
return msg
if __name__ == "__main__":
data = get_data()
news = data.get("NEWS")
if news:
res = News().main()
send("每日新闻", res)