-
Notifications
You must be signed in to change notification settings - Fork 22
/
daily_arxiv.py
307 lines (236 loc) · 8.7 KB
/
daily_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
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
#!/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 datetime
import requests
import json
import arxiv
import os
import shutil
import yaml
import time
import random
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
base_url = "https://arxiv.paperswithcode.com/api/v0/papers/"
def get_authors(authors, first_author=False):
output = str()
if first_author == False:
output = ", ".join(str(author) for author in authors)
else:
output = authors[0]
return output
def sort_papers(papers):
output = dict()
keys = list(papers.keys())
keys.sort(reverse=True)
for key in keys:
output[key] = papers[key]
return output
def get_yaml_data(yaml_file: str):
fs = open(yaml_file)
data = yaml.load(fs, Loader=Loader)
# print(data)
return data
def get_daily_papers(topic: str, query: str = "slam", max_results=2):
# output
content = dict()
# content
output = dict()
search_engine = arxiv.Search(
query=query,
max_results=max_results,
sort_by=arxiv.SortCriterion.SubmittedDate
)
cnt = 0
for result in search_engine.results():
paper_id = result.get_short_id()
paper_title = result.title
paper_url = result.entry_id
code_url = base_url + paper_id
paper_abstract = result.summary.replace("\n", " ")
paper_authors = get_authors(result.authors)
paper_first_author = get_authors(result.authors, first_author=True)
primary_category = result.primary_category
publish_time = result.published.date()
print("Time = ", publish_time,
" title = ", paper_title,
" author = ", paper_first_author)
# eg: 2108.09112v1 -> 2108.09112
ver_pos = paper_id.find('v')
if ver_pos == -1:
paper_key = paper_id
else:
paper_key = paper_id[0:ver_pos]
try:
r = requests.get(code_url).json()
# source code link
if "official" in r and r["official"]:
cnt += 1
repo_url = r["official"]["url"]
content[
paper_key] = f"|**{publish_time}**|**{paper_title}**|{paper_first_author} et.al.|[{paper_id}]({paper_url})|**[link]({repo_url})**|\n"
else:
content[
paper_key] = f"|**{publish_time}**|**{paper_title}**|{paper_first_author} et.al.|[{paper_id}]({paper_url})|null|\n"
except Exception as e:
print(f"exception: {e} with id: {paper_key}")
data = {topic: content}
return data
def update_json_file(filename, data):
with open(filename, "r") as f:
content = f.read()
if not content:
m = {}
else:
m = json.loads(content)
json_data = m.copy()
# update papers in each keywords
for topic in data.keys():
if not topic in json_data.keys():
json_data[topic] = {}
for subtopic in data[topic].keys():
papers = data[topic][subtopic]
if subtopic in json_data[topic].keys():
json_data[topic][subtopic].update(papers)
else:
json_data[topic][subtopic] = papers
with open(filename, "w") as f:
json.dump(json_data, f)
def json_to_md(filename, to_web=False):
"""
@param filename: str
@return None
"""
DateNow = datetime.date.today()
DateNow = str(DateNow)
DateNow = DateNow.replace('-', '.')
with open(filename, "r") as f:
content = f.read()
if not content:
data = {}
else:
data = json.loads(content)
if to_web == False:
md_filename = "README.md"
# clean README.md if daily already exist else create it
with open(md_filename, "w+") as f:
pass
# write data into README.md
with open(md_filename, "a+") as f:
f.write("## Updated on " + DateNow + "\n\n")
f.write(
"> Welcome to contribute! Add your topics and keywords in `topic.yml`\n\n")
for topic in data.keys():
f.write("## " + topic + "\n\n")
for subtopic in data[topic].keys():
day_content = data[topic][subtopic]
if not day_content:
continue
# the head of each part
f.write(f"### {subtopic}\n\n")
f.write("|Publish Date|Title|Authors|PDF|Code|\n" +
"|---|---|---|---|---|\n")
# sort papers by date
day_content = sort_papers(day_content)
for _, v in day_content.items():
if v is not None:
f.write(v)
f.write(f"\n")
else:
if os.path.exists('docs'):
shutil.rmtree('docs')
if not os.path.isdir('docs'):
os.mkdir('docs')
shutil.copyfile('README.md', os.path.join('docs', 'index.md'))
for topic in data.keys():
os.makedirs(os.path.join('docs', topic), exist_ok=True)
md_indexname = os.path.join('docs', topic, "index.md")
with open(md_indexname, "w+") as f:
f.write(f"# {topic}\n\n")
# print(f'web {topic}')
for subtopic in data[topic].keys():
md_filename = os.path.join('docs', topic, f"{subtopic}.md")
# print(f'web {subtopic}')
# clean README.md if daily already exist else create it
with open(md_filename, "w+") as f:
pass
with open(md_filename, "a+") as f:
day_content = data[topic][subtopic]
if not day_content:
continue
# the head of each part
f.write(f"# {subtopic}\n\n")
f.write("| Publish Date | Title | Authors | PDF | Code |\n")
f.write(
"|:---------|:-----------------------|:---------|:------|:------|\n")
# sort papers by date
day_content = sort_papers(day_content)
for _, v in day_content.items():
if v is not None:
f.write(v)
f.write(f"\n")
with open(md_indexname, "a+") as f:
day_content = data[topic][subtopic]
if not day_content:
continue
# the head of each part
f.write(f"## {subtopic}\n\n")
f.write("| Publish Date | Title | Authors | PDF | Code |\n")
f.write(
"|:---------|:-----------------------|:---------|:------|:------|\n")
# sort papers by date
day_content = sort_papers(day_content)
for _, v in day_content.items():
if v is not None:
f.write(v)
f.write(f"\n")
print("finished")
if __name__ == "__main__":
data_collector = dict()
yaml_path = os.path.join(".", "topic.yml")
yaml_data = get_yaml_data(yaml_path)
print('yaml_path:')
print(yaml_path)
print('yaml_data:')
print(yaml_data)
keywords = dict(yaml_data)
for topic in keywords.keys():
for subtopic, keyword in dict(keywords[topic]).items():
# topic = keyword.replace("\"","")
# print("Keyword: " + subtopic)
print(subtopic)
try:
data = get_daily_papers(
subtopic, query=keyword, max_results=10)
except:
print(f'CANNOT get {subtopic} data from arxiv')
data = None
# time.sleep(random.randint(2, 10))
if not topic in data_collector.keys():
data_collector[topic] = {}
if data:
data_collector[topic].update(data)
print(data)
# print(data_collector)
print("\n")
print(data_collector)
# update README.md file
json_file = "arxiv-daily.json"
# if ~os.path.exists(json_file):
# with open(json_file,'w')as a:
# print("create " + json_file)
# update json data
update_json_file(json_file, data_collector)
# json data to markdown
json_to_md(json_file)
# json data to markdown
json_to_md(json_file, to_web=True)