generated from wanghaisheng/arxiv-paper-daily-page-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
384 lines (320 loc) · 12.8 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@File : daily_arxiv.py
@Time : 2021-10-29 22:34:09
@Author : Bingjie Yan
@Email : [email protected]
@License : Apache License 2.0
"""
import json.decoder
import os.path
import shutil
from gevent import monkey
monkey.patch_all()
import gevent
from gevent.queue import Queue
from datetime import datetime
import requests
import arxiv
import yaml
from fire import Fire
from config import (
SERVER_PATH_TOPIC,
SERVER_DIR_STORAGE,
SERVER_PATH_README,
SERVER_PATH_DOCS,
SERVER_PATH_STORAGE_MD,
TIME_ZONE_CN,
logger
)
class ToolBox:
@staticmethod
def log_date(mode="log"):
if mode == "log":
return str(datetime.now(TIME_ZONE_CN)).split(".")[0]
elif mode == "file":
return str(datetime.now(TIME_ZONE_CN)).split(" ")[0]
@staticmethod
def get_yaml_data() -> dict:
with open(SERVER_PATH_TOPIC, "r", encoding="utf8") as f:
data = yaml.load(f, Loader=yaml.SafeLoader)
print(data)
return data
@staticmethod
def handle_html(url: str):
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/95.0.4638.69 Safari/537.36 Edg/95.0.1020.44"
}
proxies = {"http": None, "https": None}
session = requests.session()
response = session.get(url, headers=headers, proxies=proxies)
try:
data_ = response.json()
return data_
except json.decoder.JSONDecodeError as e:
logger.error(e)
class CoroutineSpeedup:
"""轻量化的协程控件"""
def __init__(
self,
work_q: Queue = None,
task_docker=None,
):
# 任务容器:queue
self.worker = work_q if work_q else Queue()
self.channel = Queue()
# 任务容器:迭代器
self.task_docker = task_docker
# 协程数
self.power = 32
# 任务队列满载时刻长度
self.max_queue_size = 0
self.cache_space = []
self.max_results = 30
def _adaptor(self):
while not self.worker.empty():
task: dict = self.worker.get_nowait()
if task.get("pending"):
self.runtime(context=task.get("pending"))
elif task.get("response"):
self.parse(context=task)
def _progress(self):
p = self.max_queue_size - self.worker.qsize() - self.power
p = 0 if p < 1 else p
return p
def runtime(self, context: dict):
keyword_ = context.get("keyword")
res = arxiv.Search(
query=keyword_,
max_results=self.max_results,
sort_by=arxiv.SortCriterion.SubmittedDate
).results()
context.update({"response": res, "hook": context})
self.worker.put_nowait(context)
def parse(self, context):
base_url = "https://arxiv.paperswithcode.com/api/v0/papers/"
_paper = {}
arxiv_res = context.get("response")
for result in arxiv_res:
# beaware result is a https://github.com/lukasschwab/arxiv.py
attributes_and_methods = dir(result)
paper_id = result.get_short_id()
paper_title = result.title
paper_url = result.entry_id
paper_abstract= result.summary.strip().replace('\n',' ').replace('\r'," ")
print(paper_abstract)
code_url = base_url + paper_id
paper_first_author = result.authors[0]
publish_time = result.published.date()
ver_pos = paper_id.find('v')
paper_key = paper_id if ver_pos == -1 else paper_id[0:ver_pos]
# 尝试获取仓库代码
# ----------------------------------------------------------------------------------
# Origin(r)
# ----------------------------------------------------------------------------------
# {
# 'paper_url': 'https://',
# 'official': {'url': 'https://github.com/nyu-wireless/mmwRobotNav'},
# 'all_official': [{'url': 'https://github.com/nyu-wireless/mmwRobotNav'}],
# 'unofficial_count': 0,
# 'frameworks': [],
# 'status': 'OK'
# }
# ----------------------------------------------------------------------------------
# None(r)
# ----------------------------------------------------------------------------------
# {
# 'paper_url': 'https://',
# 'official': None,
# 'all_official': [],
# 'unofficial_count': 0,
# 'frameworks': [],
# 'status': 'OK'
# }
response = ToolBox.handle_html(code_url)
official_ = response.get("official")
repo_url = official_.get("url", "null") if official_ else "null"
# ----------------------------------------------------------------------------------
# 编排模型
# ----------------------------------------------------------------------------------
# IF repo
# |publish_time|paper_title|paper_first_author|[paper_id](paper_url)|`[link](url)|`paper_abstract``
# ELSE
# |publish_time|paper_title|paper_first_author|[paper_id](paper_url)|`null`
_paper.update({
paper_key: {
"publish_time": publish_time,
"title": paper_title,
"authors": f"{paper_first_author} et.al.",
"id": paper_id,
"paper_url": paper_url,
"repo": repo_url,
"abstract" :paper_abstract
},
})
self.channel.put_nowait({
"paper": _paper,
"topic": context["hook"]["topic"],
"subtopic": context["hook"]["subtopic"],
"fields": ["Publish Date", "Title", "Authors", "PDF", "Code","Abstract"]
})
logger.success(
f"handle [{self.channel.qsize()}/{self.max_queue_size}]"
f" | topic=`{context['topic']}` subtopic=`{context['hook']['subtopic']}`")
def offload_tasks(self):
if self.task_docker:
for task in self.task_docker:
self.worker.put_nowait({"pending": task})
self.max_queue_size = self.worker.qsize()
def overload_tasks(self):
ot = _OverloadTasks()
file_obj: dict = {}
while not self.channel.empty():
# 将上下文替换成 Markdown 语法文本
context: dict = self.channel.get()
md_obj: dict = ot.to_markdown(context)
# 子主题分流
if not file_obj.get(md_obj["hook"]):
file_obj[md_obj["hook"]] = md_obj["hook"]
file_obj[md_obj["hook"]] += md_obj["content"]
# 生成 mkdocs 所需文件
os.makedirs(os.path.join(SERVER_PATH_DOCS, f'{context["topic"]}'), exist_ok=True)
with open(os.path.join(SERVER_PATH_DOCS, f'{context["topic"]}', f'{context["subtopic"]}.md'), 'w') as f:
f.write(md_obj["content"])
# 生成 Markdown 模板文件
template_ = ot.generate_markdown_template(
content="".join(list(file_obj.values())))
# 存储 Markdown 模板文件
ot.storage(template_, obj_="database")
return template_
def go(self, power: int):
# 任务重载
self.offload_tasks()
# 配置弹性采集功率
if self.max_queue_size != 0:
self.power = self.max_queue_size if power > self.max_queue_size else power
# 任务启动
task_list = []
for _ in range(self.power):
task = gevent.spawn(self._adaptor)
task_list.append(task)
gevent.joinall(task_list)
class _OverloadTasks:
def __init__(self):
self._build()
# yyyy-mm-dd
self.update_time = ToolBox.log_date(mode="log")
self.storage_path_by_date = SERVER_PATH_STORAGE_MD.format(
ToolBox.log_date('file'))
self.storage_path_readme = SERVER_PATH_README
self.storage_path_docs = SERVER_PATH_DOCS
# -------------------
# Private API
# -------------------
@staticmethod
def _build():
if not os.path.exists(SERVER_DIR_STORAGE):
os.mkdir(SERVER_DIR_STORAGE)
@staticmethod
def _set_markdown_hyperlink(text, link):
return f"[{text}]({link})"
def _generate_markdown_table_content(self, paper: dict):
paper['publish_time'] = f"**{paper['publish_time']}**"
paper['title'] = f"**{paper['title']}**"
_pdf = self._set_markdown_hyperlink(
text=paper['id'], link=paper['paper_url'])
_repo = self._set_markdown_hyperlink(
text="link", link=paper['repo']) if "http" in paper['repo'] else "null"
paper['abstract']=f"{paper['abstract']}"
line = f"|{paper['publish_time']}" \
f"|{paper['title']}" \
f"|{paper['authors']}" \
f"|{_pdf}" \
f"|{_repo}" \
f"|{paper['abstract']}|\n"
print(':::',line)
return line
@staticmethod
def _set_style_to(style: str = "center"):
return " :---: " if style == "center" else " --- "
# -------------------
# Public API
# -------------------
def storage(self, template: str, obj_: str = "database"):
"""
将 Markdown 模板存档
@param template:
@param obj_: database:将 Markdown 模板存档至 database/store 中。其他值,替换根目录下的 README
@return:
"""
path_factory = {
'database': self.storage_path_by_date,
'readme': self.storage_path_readme,
'docs': self.storage_path_docs
}
if obj_ not in path_factory.keys():
path_ = path_factory['readme']
else:
path_ = path_factory[obj_]
with open(path_, "w", encoding="utf8") as f:
for i in template:
f.write(i)
def generate_markdown_template(self, content: str):
repo_url=os.getenv('repo')
repo_name=repo_url.split('/')[-1].replace('-',' ')
repo_url="https://github.com/"+repo_url
_project = f"# arxiv-daily latest papers around {repo_name}\n"
_pin = f"Automated deployment @ {self.update_time} Asia/Shanghai\n"
_tos = "> Welcome to contribute! Add your topics and keywords in " \
"[`topic.yml`]({repo_url}/blob/main/database/topic.yml).\n"
_tos += "> You can also view historical data through the " \
"[storage]({repo_url}/blob/main/database/storage).\n"
_form = _project + _pin + _tos + content
return _form
def to_markdown(self, context: dict) -> dict:
_fields = context["fields"]
_topic = context["topic"]
_subtopic = context["subtopic"]
_paper_obj = context["paper"]
_topic_md = f"\n## {_topic}\n"
_subtopic_md = f"\n### {_subtopic}\n"
_fields_md = f"|{'|'.join(_fields)}|\n"
_style_md = f"|{'|'.join([self._set_style_to('center') for _ in range(len(_fields))])}|\n"
table_lines = "".join([self._generate_markdown_table_content(
paper) for paper in _paper_obj.values()])
_content_md = _subtopic_md + _fields_md + _style_md + table_lines
return {"hook": _topic_md, "content": _content_md}
class Scaffold:
def __init__(self):
pass
@staticmethod
@logger.catch()
def run(env: str = "development", power: int = 16):
"""
Start the test sample.
Usage: python daily_arxiv.py run
or: python daily_arxiv.py run --env=production 生产环境下运行
@param power: synergy power. The recommended value interval is [2,16].The default value is 37.
@param env: Optional with [development production]
@return:
"""
# Get tasks
context = ToolBox.get_yaml_data()
# Set tasks
pending_atomic = [{"subtopic": subtopic, "keyword": keyword.replace('"', ""), "topic": topic}
for topic, subtopics in context.items() for subtopic, keyword in subtopics.items()]
# Offload tasks
booster = CoroutineSpeedup(task_docker=pending_atomic)
booster.go(power=power)
# Overload tasks
template_ = booster.overload_tasks()
# Replace project README file.
if env == "production":
with open(SERVER_PATH_README, "w", encoding="utf8") as f:
for i in template_:
f.write(i)
shutil.copyfile(SERVER_PATH_README, os.path.join(SERVER_PATH_DOCS, "index.md"))
if __name__ == "__main__":
Fire(Scaffold)