forked from fjxmlzn/paper_downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpd.py
379 lines (310 loc) · 11.1 KB
/
pd.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
import argparse
import os
import shutil
import json
import time
import re
import resource
from encodings import hex_codec
from tqdm import tqdm
from PyPDF2 import PdfFileReader, PdfFileWriter
from paper_downloader.constant import CONF_URL_FILE_SUFFIX
from paper_downloader.constant import PDF_URL_FILE_SUFFIX
from paper_downloader.constant import PAPER_LIST_FILE_SUFFIX
from paper_downloader.constant import CONF_NAME
from paper_downloader.constant import CONF_URL
from paper_downloader.constant import CONF_ELE
from paper_downloader.constant import CONF_ATTRS
from paper_downloader.constant import PAPER_TITLE
from paper_downloader.constant import PAPER_LINKS
from paper_downloader.constant import PDF_URL
from paper_downloader.paper_list import paper_list_from_url
from paper_downloader.pdf_url import gscholar_pdf_url_from_title
from paper_downloader.scholar.scholar import ScholarConf
from paper_downloader.scholar.scholar import ScholarUtils
from paper_downloader.downloader import Downloader
__author__ = 'Zinan Lin'
__email__ = '[email protected]'
def get_conf_url_path(args):
if args.conference is None:
root_folder = args.temp_folder
else:
root_folder = args.url_folder
return os.path.join(
root_folder, str(args.conference) + CONF_URL_FILE_SUFFIX)
def get_pdf_url_path(args):
if args.conference is None:
root_folder = args.temp_folder
else:
root_folder = args.url_folder
return os.path.join(
root_folder, str(args.conference) + PDF_URL_FILE_SUFFIX)
def get_paper_list_path(args):
if args.conference is None:
root_folder = args.temp_folder
else:
root_folder = args.url_folder
return os.path.join(
root_folder, str(args.conference) + PAPER_LIST_FILE_SUFFIX)
def get_paper_pdf_path(args, paper):
if args.store:
root_folder = args.pdf_folder
else:
root_folder = args.temp_folder
file_name = re.sub('[^a-zA-Z0-9 ]+', '', paper)
return os.path.join(root_folder, file_name + '.pdf')
def get_merged_pdf_path(args):
return os.path.join(args.pdf_folder, args.merge_file)
def conf_url_exists(args):
return os.path.exists(get_conf_url_path(args))
def pdf_url_exists(args):
return os.path.exists(get_pdf_url_path(args))
def paper_list_exists(args):
return os.path.exists(get_paper_list_path(args))
def process_conf_url(args):
print('Pre-Parse conference webpage')
if args.url is None:
raise Exception('Please specify url')
data = {
CONF_NAME: str(args.conference),
CONF_URL: args.url}
_, keys, contents = paper_list_from_url(
args, args.url, args.ele, args.attrs)
data[CONF_ELE] = keys
data[CONF_ATTRS] = args.attrs
path = get_conf_url_path(args)
with open(path, 'w') as f:
json.dump(data, f, indent=4)
print('Conference webpage parsing results saved to: {}'.format(path))
if args.debug:
str_ = ''
for key in contents:
str_ += '---\n'
str_ += ('If following strings are paper titles, add\n{}\nto {}'
' in {}\n\n'.format(
json.dumps(key, indent=4), CONF_ELE, path))
for i, title in enumerate(contents[key]):
str_ += '{}: '.format(i + 1)
str_ += title
str_ += '\n'
str_ += '---\n'
with open(args.debug_file, 'wb') as f:
f.write(str_.encode('utf-8'))
def process_paper_list(args):
print('Post-Parse conference webpage')
conf_url_path = get_conf_url_path(args)
with open(conf_url_path, 'r') as f:
data = json.load(f)
print('Using {}'.format(conf_url_path))
conf_url = data[CONF_URL]
conf_ele = data[CONF_ELE]
conf_attrs = data[CONF_ATTRS]
papers, _, _ = paper_list_from_url(args, conf_url, conf_ele, conf_attrs)
print('Found {} papers'.format(len(papers)))
paper_list_path = get_paper_list_path(args)
with open(paper_list_path, 'w') as f:
json.dump(papers, f, indent=4)
print('Paper list saved to: {}'.format(paper_list_path))
def process_pdf_url(args):
print('Get PDF URLs')
if args.fix_pdf_url and pdf_url_exists(args):
pdf_url_path = get_pdf_url_path(args)
with open(pdf_url_path, 'r') as f:
old_data = json.load(f)
print('Found {}'.format(pdf_url_path))
papers = [t[PAPER_TITLE] for t in old_data]
else:
paper_list_path = get_paper_list_path(args)
with open(paper_list_path, 'r') as f:
papers = json.load(f)
old_data = []
print('Using {}'.format(paper_list_path))
def search(paper, data):
for i in range(len(data)):
if data[i][PAPER_TITLE] == paper:
return data[i][PAPER_LINKS]
return []
data = []
for paper in tqdm(papers):
links = search(paper, old_data)
if len(links) == 0:
links = gscholar_pdf_url_from_title(paper)
time.sleep(args.delay)
data.append({
PAPER_TITLE: paper,
PAPER_LINKS: links})
pdf_url_path = get_pdf_url_path(args)
with open(pdf_url_path, 'w') as f:
json.dump(data, f, indent=4)
print('PDF links saved to: {}'.format(pdf_url_path))
def download_papers(args):
print('Download papers')
pdf_url_path = get_pdf_url_path(args)
with open(pdf_url_path, 'r') as f:
papers = json.load(f)
print('Using {}'.format(pdf_url_path))
downloader = Downloader(args)
for paper in tqdm(papers):
success = False
path = get_paper_pdf_path(args, paper[PAPER_TITLE])
if os.path.exists(path):
print('Skip: {}'.format(paper[PAPER_TITLE].encode('utf-8')))
success = True
else:
for item in paper[PAPER_LINKS]:
if downloader.download(item[PDF_URL], path):
success = True
break
if not success:
print('Failed to download: {}'.format(paper))
def merge_papers(args):
print('Merge papers')
if len(args.merge) == 0:
raise Exception('Please specify merge')
pdf_url_path = get_pdf_url_path(args)
with open(pdf_url_path, 'r') as f:
papers = json.load(f)
print('Using {}'.format(pdf_url_path))
soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
resource.setrlimit(
resource.RLIMIT_NOFILE, (max(soft, len(papers) + 10), hard))
writer = PdfFileWriter()
file_steams = []
for paper in tqdm(papers):
path = get_paper_pdf_path(args, paper[PAPER_TITLE])
if os.path.exists(path):
f = open(path, 'rb')
file_steams.append(f)
reader = PdfFileReader(f, strict=False)
for page in args.merge:
if page >= 1 and page <= reader.getNumPages():
writer.addPage(reader.getPage(page - 1))
else:
print('Skip: {} page of {}'.format(
page, paper[PAPER_TITLE].encode('utf-8')))
else:
print('Missing: {}'.format(paper[PAPER_TITLE]))
path = get_merged_pdf_path(args)
with open(path, 'wb') as f:
writer.write(f)
for f in file_steams:
f.close()
print('Merged PDF saved to: {}'.format(path))
def create_url_folder(args):
if not os.path.exists(args.url_folder):
os.makedirs(args.url_folder)
def create_temp_folder(args):
if not os.path.exists(args.temp_folder):
os.makedirs(args.temp_folder)
def create_pdf_folder(args):
if not os.path.exists(args.pdf_folder):
os.makedirs(args.pdf_folder)
def clean_temp_folder(args):
shutil.rmtree(args.temp_folder)
if __name__ == '__main__':
# links = gscholar_pdf_url_from_title('On Utilizing Smartphone Time-of-Flight Sensors to Detect Hidden Spy Cameras')
# print(links)
# exit(0)
parser = argparse.ArgumentParser(
description='Paper downloader',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'-u', '--url',
help='url of the paper list',
type=str)
parser.add_argument(
'-e', '--ele',
help='UI element path for finding the paper titles',
type=str,
action='append')
parser.add_argument(
'-a', '--attrs',
help='UI element attributes for finding the paper titles',
type=str,
action='append',
default=[])
parser.add_argument(
'-c', '--conference',
help='name of the conference',
type=str)
parser.add_argument(
'-s', '--store',
help='store papers',
action='store_true')
parser.add_argument(
'-m', '--merge',
help='the page to be merged into a single PDF file',
type=int,
action='append')
parser.add_argument(
'--merge_file',
help='the file name for the merged file',
type=str,
default='merged.pdf')
parser.add_argument(
'-d', '--delay',
help='the delay (seconds) between queries to Google Scholar to avoid'
'being banned',
type=float,
default=0.0)
parser.add_argument(
'--pdf_folder',
help='the folder to store the downloaded/merged PDF files',
type=str,
default='pdf')
parser.add_argument(
'--url_folder',
help='the folder to store the urls of conferences and pdfs',
type=str,
default='conf_url')
parser.add_argument(
'--temp_folder',
help='the temporary folder',
type=str,
default='temp')
parser.add_argument(
'--debug',
help='outputs for manual correcting paper list or url list',
action='store_true')
parser.add_argument(
'--debug_file',
help='the debug for storing debug outputs',
type=str,
default='debug.txt')
parser.add_argument(
'--fix_pdf_url',
help='Querying the pdf links that are missing',
action='store_true')
parser.add_argument(
'--user_agent',
help='user agent for HTTP requests',
type=str,
default=('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:56.0) '
'Gecko/20100101 Firefox/56.0'))
parser.add_argument(
'--cookie_file',
help='the path to the cookie.txt for HTTP requests',
type=str,
default=os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'cookies.txt'))
args = parser.parse_args()
if args.debug:
ScholarConf.LOG_LEVEL = ScholarUtils.LOG_LEVELS['debug']
ScholarConf.USER_AGENT = args.user_agent
ScholarConf.COOKIE_JAR_FILE = args.cookie_file
create_temp_folder(args)
create_url_folder(args)
create_pdf_folder(args)
if not (conf_url_exists(args) or paper_list_exists(args) or
pdf_url_exists(args)):
process_conf_url(args)
if not (paper_list_exists(args) or pdf_url_exists(args)):
process_paper_list(args)
if not pdf_url_exists(args) or args.fix_pdf_url:
process_pdf_url(args)
if args.store or (args.merge is not None):
download_papers(args)
if args.merge is not None:
merge_papers(args)
clean_temp_folder(args)