-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
169 lines (140 loc) · 6.29 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
import os
import re
from datetime import datetime
from urllib import request
import dateutil.parser
import yaml
from bs4 import BeautifulSoup
from utils.utils import HEADERS, get_week_dates, translate
CONTENT_IN_BRACKET = re.compile(r'\((.*?)\)')
def parse_user():
config_root = './configs'
user_info = {}
for file in os.scandir(config_root):
if file.name.endswith('.yml'):
with open(file.path) as f:
info = yaml.load(f, Loader=yaml.FullLoader)
name, ext = os.path.splitext(file.name)
user_info[name] = info
return user_info
class Paper:
def __init__(self):
self.abs_prefix = 'https://arxiv.org/abs/'
self.pdf_prefix = 'https://arxiv.org/pdf/'
def parse(self, dt, dd):
self.id = re.findall(r'\d+\.\d+', dt.text)[0]
self.abs_url = self.abs_prefix + self.id
self.pdf_url = self.pdf_prefix + self.id
self.title = dd.find(
'div', {'class': 'list-title mathjax'}).text.replace('Title:', '').strip()
self.authors = dd.find('div', {'class': 'list-authors'}).text.replace('Authors:\n', '').replace(
'\n', '').strip()
self.subjects = CONTENT_IN_BRACKET.findall(
dd.find('div', {'class': 'list-subjects'}).text)
self.abstract = dd.find(
'p', {'class': 'mathjax'}).text.replace('\n', ' ').strip()
def find_keyword(self, keywords):
title = self.title.lower()
abstract = self.abstract.lower()
self.keywords = [
keyword for keyword in keywords if keyword in title or keyword in abstract]
def translate(self):
self.abstract_zh = translate(self.abstract)
def __str__(self):
subjects = ', '.join(self.subjects)
keywords = ', '.join(self.keywords)
text = f'<h3>Title: {self.title}</h3>\n' \
f'<ul>\n' \
f'<li><strong>Authors: </strong>{self.authors}</a></li>\n' \
f'<li><strong>Subjects: </strong>{subjects}</a></li>\n' \
f'<li><strong>Abstract URL: </strong><a href="{self.abs_url}">{self.abs_url}</a></li>\n' \
f'<li><strong>Pdf URL: </strong><a href="{self.pdf_url}">{self.pdf_url}</a></li>\n' \
f'<li><strong>Copy Paste: </strong><code><input type="checkbox">[[{self.id}]] {self.title}({self.abs_url})</code><input type="text"></li>\n' \
f'<li><strong>Keywords: </strong>{keywords}</a></li>\n' \
f'<li><strong>Abstract: </strong>{self.abstract}</li>\n'
if getattr(self, 'abstract_zh', None) is not None:
text += f'<li><strong>摘要:</strong>{self.abstract_zh}</li>\n'
text += f'</ul>\n'
return text
def main():
user_infos = parse_user()
new_url = 'https://arxiv.org/list/cs/new'
req = request.Request(url=new_url, headers=HEADERS)
page = request.urlopen(req, timeout=20)
bs = BeautifulSoup(page, features='html.parser')
content = bs.body.find('div', {'id': 'content'})
dts = content.dl.find_all('dt')
dds = content.dl.find_all('dd')
parse_date = dateutil.parser.parse(content.h3.text.replace('Showing new listings for ', ''))
assert len(dts) == len(dds)
user_papers = {}
for user_name, info in user_infos.items():
subjects = set(info['subjects'])
keywords = info['keywords']
trans = info['translate']
papers = []
for dt, dd in zip(dts, dds):
paper = Paper()
paper.parse(dt, dd)
if paper.subjects[0] in subjects:
paper.find_keyword(keywords)
if len(paper.keywords) > 0:
if trans:
paper.translate()
papers.append(paper)
user_papers[user_name] = papers
# 更新README
now = datetime.utcnow()
timestamp = datetime.strftime(now, '%Y-%m-%d %H:%M:%S')
with open('README.md', 'w', encoding='utf-8') as fp:
fp.write(f'# arxiv-daily\n')
fp.write(f'updated on {timestamp}\n')
fp.write(f'| name | count |\n')
fp.write(f'| - | - |\n')
for user_name, papers in user_papers.items():
fp.write(f'| {user_name} | {len(papers)} |\n')
out_root = 'html'
date = datetime.strftime(parse_date, '%Y-%m-%d')
week = datetime.strftime(parse_date, '%Y-W%W')
# 更新index
with open('index.html', 'w', encoding='utf-8') as f:
print(f'<link rel="stylesheet" href="css/markdown.css" />', file=f)
print(f'<article class="markdown-body">', file=f)
for user_name, info in user_infos.items():
print(f'<div><a href="./{out_root}/{user_name}.html">{user_name}</a></div>',
file=f)
print(f'</article>', file=f)
# 更新user index
for user_name, papers in user_papers.items():
os.makedirs(f'{out_root}/{user_name}', exist_ok=True)
# 更新html
with open(f'{out_root}/{user_name}/{date}.html', 'w', encoding='utf-8') as f:
print(f'<link rel="stylesheet" href="../../css/markdown.css" />',
file=f)
print(f'<article class="markdown-body">', file=f)
print(f'<h1>{date}</h1>', file=f)
for paper in papers:
print(paper, file=f)
print(f'<button id="copy">Copy All</button>', file=f)
print(f'</article>', file=f)
print(
f'<script src="../../javascript/clipboard.min.js"></script>',
file=f)
print(f'<script src="../../javascript/clipboard.js"></script>',
file=f)
with open(f'{out_root}/{user_name}.html', 'w', encoding='utf-8') as f:
print(f'<link rel="stylesheet" href="../css/markdown.css" />',
file=f)
print(f'<article class="markdown-body">', file=f)
week_dates = get_week_dates(f'{out_root}/{user_name}')
for week, dates in sorted(week_dates.items(),
key=lambda d: d[0],
reverse=True):
print(f'<h2>{week}</h2>', file=f)
for item in sorted(dates, reverse=True):
print(
f'<div><a href="./{user_name}/{item}.html">{item}</a></div>',
file=f)
print(f'</article>', file=f)
if __name__ == '__main__':
main()