-
Notifications
You must be signed in to change notification settings - Fork 1
/
arxivscraper.py
211 lines (180 loc) · 5.39 KB
/
arxivscraper.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
from datetime import datetime
from datetime import timedelta
import logging
import argparse
import requests
import io
import re
import smtplib
import ssl
from functools import reduce
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import arxiv
import slate3k as slate
from dateutil.parser import parse
from timeout_decorator import (
timeout,
TimeoutError,
)
_logger = logging.getLogger(__name__)
def send_email(cfg, matches, errors):
# Create the message
message = MIMEMultipart('alternative')
message['From'] = cfg['from_email']
message['To'] = cfg['to_email']
message['Subject'] = 'Important arxiv post(s)'
text = ''
html = '<html>\n'
html += '<body>\n'
for regex, papers in matches.items():
if papers:
text += f'Matches for regex {regex}\n'
html += f'<p>\nMatches for regex {regex}\n<ul>\n'
for paper in papers:
text += f' {str(paper.id)}\n'
html += f'<li><a href="{str(paper.id)}">{str(paper.title)}</a></li>\n'
html += '</ul>\n</p>\n'
text += f'There were {len(errors)} errors.\n'
html += f'There were {len(errors)} errors.<br>\n'
for error in errors:
text += error
text += '\n'
html += error+'<br>'
html += '</body>'
html += '</html>'
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
message.attach(part1)
message.attach(part2)
# Actually send the email
context = ssl.create_default_context()
with smtplib.SMTP_SSL(
cfg['smtp_server'],
cfg['port'],
context=context,
) as server:
server.login(
cfg['from_email'],
cfg['password'],
)
server.sendmail(
cfg['from_email'],
cfg['to_email'],
message.as_string(),
)
def posted_today(paper, date=datetime.today()):
paper_published = parse(paper.published)
if date.date() == paper_published.date():
return True
return False
def get_pdf_text(url):
fr = io.BytesIO(requests.get(url).content)
text = slate.PDF(fr)
return str(text)
def sanitize_chem_term(chem_term):
legal = r'(\s|\$|_|\{|\})*'
chem_list = chem_term.split(' ')
for i,element in enumerate(chem_list):
head = element.rstrip('123456789')
tail = element[len(head):]
if tail != '':
head = head + legal
tail = tail + legal
chem_list[i] = head+tail
return r'\s*'.join(chem_list)
def main(cfg):
##
## Make terms list
##
terms = cfg['terms']
for chem_term in cfg['chem_terms']:
terms.append(sanitize_chem_term(chem_term))
query_terms = [
'cat:'+cfg['section'],
]
results = arxiv.query(
query=' AND '.join(query_terms),
sort_by='submittedDate',
start=0,
max_results=cfg['max_results'],
sort_order='descending',
iterative=True,
)
requested_date = cfg['date']
if requested_date is None:
requested_date = datetime.today()
today_results = (
p for p in results()
if posted_today(p, requested_date-timedelta(days=1))
)
matches = {}
flagsdict = {
'IGNORECASE': re.IGNORECASE,
'MULTILINE': re.MULTILINE,
'DOTALL': re.DOTALL,
'UNICODE': re.UNICODE,
'LOCALE': re.LOCALE,
'VERBOSE': re.VERBOSE,
}
flags = [flagsdict[flag.upper()] for flag in cfg['flags']]
@timeout(cfg['timeout'])
def in_pdf(url, list_of_regex, flags=0):
text = get_pdf_text(url)
answers = {regex:False for regex in list_of_regex}
for regex in list_of_regex:
prog = re.compile(regex, flags)
result = prog.search(text)
if result is not None:
answers[regex] = True
return answers
errors = []
for paper in today_results:
try:
_logger.info(f'Checking {paper.id}...')
for regex, is_in in in_pdf(
paper.pdf_url,
terms,
flags=reduce(int.__or__, flags),
).items():
if is_in:
if regex not in matches.keys():
matches[regex] = [paper]
else:
matches[regex].append(paper)
except Exception as exception:
if isinstance(exception, TimeoutError):
msg = f'PDF reader timeout reached for {paper.id}.'
_logger.warning(msg)
else:
msg = str(exception)+f' called for {paper.id}.'
errors.append(msg)
if matches:
send_email(cfg, matches, errors)
if errors:
_logger.warning('There were PDF errors.')
for error in errors:
_logger.warning(error)
def get_cfg():
from runpy import run_path
parser = argparse.ArgumentParser()
parser.add_argument(
'--config-file',
type=str,
dest='config_file',
required=True,
)
parser.add_argument(
'--date',
type=parse,
dest='date',
required=False,
default=None,
)
args = parser.parse_args()
cfg = run_path(args.config_file)['cfg']
cfg['date'] = args.date
return cfg
if __name__ == '__main__':
cfg = get_cfg()
main(cfg)