forked from Doragd/Algorithm-Practice-in-Industry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arxiv.py
230 lines (170 loc) · 6.35 KB
/
arxiv.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
'''
credit to original author: Glenn ([email protected])
Author: Doragd
'''
import os
import requests
import time
import json
import datetime
from tqdm import tqdm
SERVERCHAN_API_KEY = os.environ.get("SERVERCHAN_API_KEY", None)
QUERY = os.environ.get('QUERY', 'cs.IR')
LIMITS = int(os.environ.get('LIMITS', 3))
CAIYUN_TOKEN = os.environ.get("CAIYUN_TOKEN", None)
FEISHU_URL = os.environ.get("FEISHU_URL", None)
def translate(source, direction='en2zh', CAIYUN_TOKEN=CAIYUN_TOKEN):
url = "http://api.interpreter.caiyunai.com/v1/translator"
payload = {
"source": source,
"trans_type": direction,
"request_id": "demo",
"detect": True,
}
headers = {
"content-type": "application/json",
"x-authorization": "token " + CAIYUN_TOKEN,
}
try:
response = requests.request("POST", url, data=json.dumps(payload), headers=headers)
return json.loads(response.text)["target"]
except:
return []
def get_yesterday():
today = datetime.datetime.now()
yesterday = today - datetime.timedelta(days=1)
return yesterday.strftime('%Y-%m-%d')
def search_arxiv_papers(search_term, max_results=10):
papers = []
url = f'http://export.arxiv.org/api/query?' + \
f'search_query=all:{search_term}' + \
f'&start=0&&max_results={max_results}' + \
f'&sortBy=submittedDate&sortOrder=descending'
response = requests.get(url)
if response.status_code != 200:
return []
feed = response.text
entries = feed.split('<entry>')[1:]
if not entries:
return []
print('[+] 开始处理每日最新论文....')
for entry in entries:
title = entry.split('<title>')[1].split('</title>')[0].strip()
summary = entry.split('<summary>')[1].split('</summary>')[0].strip()
url = entry.split('<id>')[1].split('</id>')[0].strip()
pub_date = entry.split('<published>')[1].split('</published>')[0]
pub_date = datetime.datetime.strptime(pub_date, "%Y-%m-%dT%H:%M:%SZ").strftime("%Y-%m-%d")
papers.append({
'title': title,
'url': url,
'pub_date': pub_date,
'summary': summary,
'translated': '',
})
print('[+] 开始翻译每日最新论文并缓存....')
papers = save_and_translate(papers)
return papers
def send_wechat_message(title, content, SERVERCHAN_API_KEY):
url = f'https://sctapi.ftqq.com/{SERVERCHAN_API_KEY}.send'
params = {
'title': title,
'desp': content,
}
requests.post(url, params=params)
def send_feishu_message(title, content, url=FEISHU_URL):
card_data = {
"config": {
"wide_screen_mode": True
},
"header": {
"template": "green",
"title": {
"tag": "plain_text",
"content": title
}
},
"elements": [
{
"tag": "img",
"img_key": "img_v2_9781afeb-279d-4a05-8736-1dff05e19dbg",
"alt": {
"tag": "plain_text",
"content": ""
},
"mode": "fit_horizontal",
"preview": True
},
{
"tag": "markdown",
"content": content
}
]
}
card = json.dumps(card_data)
body =json.dumps({"msg_type": "interactive","card":card})
headers = {"Content-Type":"application/json"}
requests.post(url=url, data=body, headers=headers)
def save_and_translate(papers, filename='arxiv.json'):
with open(filename, 'r', encoding='utf-8') as f:
results = json.load(f)
cached_title2idx = {result['title'].lower():i for i, result in enumerate(results)}
untranslated_papers = []
translated_papers = []
for paper in papers:
title = paper['title'].lower()
if title in cached_title2idx.keys():
translated_papers.append(
results[cached_title2idx[title]]
)
else:
untranslated_papers.append(paper)
source = []
for paper in untranslated_papers:
source.append(paper['summary'])
target = translate(source)
if len(target) == len(untranslated_papers):
for i in range(len(untranslated_papers)):
untranslated_papers[i]['translated'] = target[i]
results.extend(untranslated_papers)
with open(filename, 'w', encoding='utf-8') as f:
json.dump(results, f, indent=4, ensure_ascii=False)
print(f'[+] 总检索条数: {len(papers)} | 命中缓存: {len(translated_papers)} | 实际返回: {len(untranslated_papers)}....')
return untranslated_papers # 只需要发送缓存中没有的
def cronjob():
if SERVERCHAN_API_KEY is None:
raise Exception("未设置SERVERCHAN_API_KEY环境变量")
print('[+] 开始执行每日推送任务....')
yesterday = get_yesterday()
today = datetime.datetime.now().strftime('%Y-%m-%d')
print('[+] 开始检索每日最新论文....')
papers = search_arxiv_papers(QUERY, LIMITS)
if papers == []:
push_title = f'Arxiv:{QUERY}[X]@{today}'
send_wechat_message('', '[WARN] NO UPDATE TODAY!', SERVERCHAN_API_KEY)
print('[+] 每日推送任务执行结束')
return True
print('[+] 开始推送每日最新论文....')
for ii, paper in enumerate(tqdm(papers, total=len(papers), desc=f"论文推送进度")):
title = paper['title']
url = paper['url']
pub_date = paper['pub_date']
summary = paper['summary']
translated = paper['translated']
yesterday = get_yesterday()
if pub_date == yesterday:
msg_title = f'[Newest]{title}'
else:
msg_title = f'{title}'
msg_url = f'URL: {url}'
msg_pub_date = f'Pub Date:{pub_date}'
msg_summary = f'Summary:\n\n{summary}'
msg_translated = f'Translated:\n\n{translated}'
push_title = f'Arxiv:{QUERY}[{ii}]@{today}'
msg_content = f"[{msg_title}]({url})\n\n{msg_pub_date}\n\n{msg_url}\n\n{msg_translated}\n\n{msg_summary}\n\n"
# send_wechat_message(push_title, msg_content, SERVERCHAN_API_KEY)
send_feishu_message(push_title, msg_content, FEISHU_URL)
time.sleep(12)
print('[+] 每日推送任务执行结束')
return True
if __name__ == '__main__':
cronjob()