-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
82 lines (69 loc) · 2.7 KB
/
Copy pathmain.py
File metadata and controls
82 lines (69 loc) · 2.7 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# -*- coding: utf-8 -*
import requests
import json
import random
import time
import textwrap
def get_api():
last_timestamp = None
# 调用 API 获取新闻数据
response_api_news = requests.get(
"https://newsdata.io/api/1/news?apikey=YOU_API_KEY&q=defi",
verify=False,
)
msg_news = response_api_news.text
# 解析新闻数据
news_data = None
parse_json = json.loads(msg_news)
if "results" in parse_json and parse_json["results"]:
# 检查 "results" 字段是否包含有效数据
news_data = {
"title": parse_json["results"][0]["title"],
"content": parse_json["results"][0]["content"],
"pubDate": parse_json["results"][0]["pubDate"],
}
# 检查新闻内容是否为新的
if last_timestamp is None or news_data["pubDate"] > last_timestamp:
# 更新最后一次获取新闻的时间戳
last_timestamp = news_data["pubDate"]
# 返回新闻数据
return news_data
# 如果获取到的新闻数据不是新的,则返回 None
return None
def chat(chanel_list, authorization_list, news_data):
for authorization in authorization_list:
header = {
"Authorization": authorization,
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36",
}
for chanel_id in chanel_list:
if news_data is not None:
# 截取json字段
lines = textwrap.wrap(news_data["content"], width=400)
for line in lines:
msg = {
"title": news_data["title"],
"content": line,
}
msg_json = json.dumps(msg)
url ="https://discord.com/api/v9/channels/{}/messages".format(chanel_id)
try:
res = requests.post(url=url, headers=header, data=msg_json)
print(res.content)
except:
pass
continue
time.sleep(random.randrange(3600,3600*4))
if __name__ == "__main__":
# Discord 频道列表和授权列表
chanel_list = ["channel_id"]
authorization_list = ["authorization_list"]
while True:
# 获取新闻数据
news_data = get_api()
# 如果新闻数据不为空,则将新闻发送到 Discord 频道
if news_data is not None:
chat(chanel_list, authorization_list, news_data)
# 每隔一小时获取一次新闻数据
time.sleep(3600)