-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·187 lines (141 loc) · 5.93 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python3
import os
import json
import aria2p.options
import requests
import xmltodict
import uuid
from time import sleep
import aria2p
import html
import urllib.parse
def get_uuid(name, uuid_table_path):
def write_uuid(_name):
_uuid = str(uuid.uuid4())
if os.path.exists(uuid_table_path):
with open(uuid_table_path) as f:
_uuid_table = json.loads(f.read())
_uuid_table.update({_name: _uuid})
with open(uuid_table_path, 'w') as f:
f.write(json.dumps(_uuid_table))
else:
with open(uuid_table_path, 'w') as f:
f.write(json.dumps({_name: _uuid}))
return _uuid
if os.path.exists(uuid_table_path):
with open(uuid_table_path) as f:
uuid_table = json.loads(f.read())
if name in uuid_table:
return uuid_table[name]
else:
return write_uuid(name)
else:
return write_uuid(name)
def compare_lists(local_list, upstream_list):
added_items = [item for item in upstream_list if item not in local_list]
modified_items = []
for item in upstream_list:
if item in local_list:
local_item_index = local_list.index(item)
if item != local_list[local_item_index]:
modified_items.append((local_list[local_item_index], item))
deleted_items = [item for item in local_list if item not in upstream_list]
return added_items, modified_items, deleted_items
def parse_html(text):
return html.escape(text)
# return text.replace('<', '<').replace('>', '>').replace('&', '&')
def simplify_magnet_link(magnet_link):
parsed_url = urllib.parse.urlparse(magnet_link)
query_params = urllib.parse.parse_qs(parsed_url.query)
xt_param = query_params.get('xt', [''])[0]
# dn_param = query_params.get('dn', [''])[0]
simplified_magnet_link = f"magnet:?xt={xt_param}"
# if dn_param:
# simplified_magnet_link += f"&dn={dn_param}"
return simplified_magnet_link
def push_to_telegram(config, message, proxies):
api_url = f"https://api.telegram.org/bot{config['token']}/sendMessage"
payload = {
"chat_id": config['chat_id'],
"text": message,
"parse_mode": "HTML",
"link_preview_options": {
"is_disabled": True
}
}
response = requests.post(api_url, json=payload, timeout=config['timeout'], proxies=proxies)
return response.json()
def win32_namespace_compatible(text):
def half2full(s):
'''
Convert all ASCII characters to the full-width counterpart.
https://stackoverflow.com/a/36693548
'''
HALF2FULL = dict((i, i + 0xFEE0) for i in range(0x21, 0x7F))
HALF2FULL[0x20] = 0x3000
return str(s).translate(HALF2FULL)
foo = r'/\:*"?<>|'
for _ in foo:
if _ in text:
text = text.replace(_, half2full(_))
return text
def push_magnet_to_aria2_rpc(config, magnet, bangumi_name):
config['options']['dir'] = config['options']['dir'].replace(r'{bangumi_name}',
win32_namespace_compatible(bangumi_name))
aria2 = aria2p.API(aria2p.Client(
host=config['host'], port=config['port'], secret=config['secret'], timeout=config['timeout']))
return aria2.add_magnet(magnet, options=config['options'])
def main():
cd = os.path.split(os.path.realpath(__file__))[0]
config_path = os.path.join(cd, 'config.json')
uuid_table_path = os.path.join(cd, 'data', 'uuid_table.json')
with open(config_path) as f:
config = json.loads(f.read())
bangumi_list = config['bangumi']['list']
config_aria2_rpc = config['aria2_rpc']
config_telegram = config['telegram']
config_proxies = config['proxies']
for bangumi in bangumi_list:
data_file = os.path.join(cd, 'data', '{}.json'.format(get_uuid(bangumi['name'], uuid_table_path)))
if os.path.exists(data_file):
with open(data_file) as f:
local_result_json = f.read()
local_result_list = json.loads(local_result_json)
else:
local_result_json = ''
local_result_list = []
_xml = requests.get(url=bangumi['upstream-url'], timeout=config['bangumi']['timeout'], proxies=config_proxies).text
upstream_result_list = xmltodict.parse(_xml)['rss']['channel']['item']
upstream_result_json = json.dumps(upstream_result_list)
added, modified, deleted = compare_lists(local_result_list, upstream_result_list)
if added == []:
reaction = '[ T_T ]'
message = 'There is no update for {}'.format(bangumi['name'])
print(f'{reaction} {message}')
else:
# print(added)
reaction = '[ >w< ]'
message = '{} Update{} for {}'.format(len(added), 's' if len(added) > 1 else '', bangumi['name'])
print(f'{reaction} {message}')
tg_msgs = []
for _ in added:
webpage = parse_html(_['link'])
magnet = simplify_magnet_link(parse_html(_['enclosure']['@url']))
push_magnet_to_aria2_rpc(config=config_aria2_rpc, magnet=magnet, bangumi_name=bangumi['name'])
msg = (
f"{_['title']}\n"
f"Poster: {_['author']}\n"
f"Date: {_['pubDate']}\n"
f"🔗 <a href='{webpage}'>Webpage</a>\n"
f"🧲 <code>{magnet}</code>"
)
tg_msgs.append(msg)
tg_msgs.append(parse_html(message))
for _ in tg_msgs:
push_to_telegram(config=config_telegram, message=_, proxies=config_proxies)
sleep(1)
with open(data_file, 'w') as f:
f.write(upstream_result_json)
sleep(1)
if __name__ == '__main__':
main()