-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename_arxiv.py
253 lines (214 loc) · 9.72 KB
/
rename_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
'''python 3.7'''
'''requirments: arxiv, shutil, pdfrw'''
import os
import arxiv
import time
import shutil
import requests
from pdfrw import PdfReader, PdfWriter
from glob import glob
import signal
new_dir = '/Users/yuancc/Dropbox/' #destination folder
non_arxiv_dir = '/Users/yuancc/Downloads/other_pdf/' # to accommodate non-arxiv pdf files
waittime = 1 # wait [waittime] seconds between each loop
print()
def interrupted(signum, frame):
print("Time out!")
signal.signal(signal.SIGALRM, interrupted)
TIMEOUT = 9999
def input_des():
try:
foo = int(input("Input the index >> "))
return foo
except:
# timeout
return -1
def manual_rename():
author=input("Authors >> ") or -1
year=input("Year >> ") or -1
title=input("Title >> ") or -1
if author != -1 and year != -1 and title !=-1:
title = title.replace(':', '').replace(',', '').replace('-',' ').replace('/',' ')
return author + ' (' + year + ') - ' + title + '.pdf'
else:
return -1
print('--------------------------------------')
print("Rename_arxiv_paper: running")
print("Scan the folder every "+str(waittime)+' second(s)')
print("Type \'ctrl-C\' to stop")
print('--------------------------------------')
try:
while True:
pdfs = [f for f in next(os.walk(os.getcwd()))[2] if f.endswith('.pdf')]
if len(pdfs) > 0:
if not os.path.exists(new_dir):
os.makedirs(new_dir)
try:
requests.head('https://arxiv.org/')
except requests.ConnectionError:
logging.error('failed to connect to arxiv.org')
else:
time.sleep(waittime)
continue
for pdf in pdfs:
try:
if len(pdf) > 18 or len(pdf) < 5:
print('Drop '+'\033[1m \"'+pdf+'\" \033[0m'+" to:")
subfolders = glob(new_dir+"*/")
subfolders.sort()
n=0
print('{:>15} {:<60}'.format("\033[1m"+"0\033[0m:",new_dir))
subfolders = glob(new_dir+"*/")
subfolders.sort()
for item in subfolders:
n=n+1
sub_dir=item.replace(new_dir,'')
print('{:>15} {:<60}'.format('\033[1m'+str(n)+'\033[0m:',sub_dir))
print('{:>15} {:<60}'.format('\033[1m'+str(n+1)+'\033[0m:','new folder'))
print(' Enter: '+non_arxiv_dir)
signal.alarm(TIMEOUT)
destination=input_des()
signal.alarm(0)
if(destination==0):
path=new_dir
pdf_name = manual_rename()
if pdf_name != -1:
shutil.move(pdf,path+pdf_name)
print("Move the renamed pdf to "+path)
else:
shutil.move(pdf,path+pdf)
print("Move the origianl pdf to "+path)
elif(destination>0 and destination <= len(subfolders)):
path = subfolders[destination-1]
pdf_name = manual_rename()
if pdf_name != -1:
shutil.move(pdf,path+pdf_name)
print("Move the renamed pdf to "+path)
else:
shutil.move(pdf,path+pdf)
print("Move the original pdf to "+path)
elif(destination == len(subfolders)+1):
print('Input the name of the new folder: (xxx/)')
newfolder=str(input() or " ")
path=new_dir+newfolder
os.mkdir(path)
pdf_name = manual_rename()
if pdf_name != -1:
shutil.move(pdf,path+pdf_name)
print("Move the renamed pdf to "+path)
else:
shutil.move(pdf,path+pdf)
print("Move the original pdf to "+path)
else:
if not os.path.exists(non_arxiv_dir):
os.makedirs(non_arxiv_dir)
shutil.move(pdf,non_arxiv_dir+pdf)
print('Move the original pdf to '+non_arxiv_dir)
print('')
time.sleep(waittime)
continue
metadata = PdfReader(pdf).Info
if '/arxiv_id' in metadata and '/updated' in metadata:
paper = arxiv.query(id_list=[metadata['/arxiv_id'][1:-1]])[0]
updated = metadata['/updated'][1:-1]
else:
paper = arxiv.query(id_list=[pdf[:-4].split('v')[0]])[0]
updated = ''
authors = paper['authors']
title = ' '.join(paper['title'].split())
year = str(paper['published_parsed'].tm_year)
arxiv_id = paper['id'].split('/')[-1].split('v')[0]
authors = paper['authors'][0].split(' ')[-1]
title = title.replace(':', '').replace(',', '').replace('-',' ').replace('/',' ')
pdf_name = authors + ' (' + year + ') - ' + title + '.pdf'
print()
print('Drop '+'\033[1m' +'\"'+pdf_name+'\"'+'\033[0m'+' to')
n=0
print('{:>15} {:<60}'.format("\033[1m"+"0\033[0m:",new_dir))
subfolders = glob(new_dir+"*/")
subfolders.sort()
for item in subfolders:
n=n+1
sub_dir=item.replace(new_dir,'')
print('{:>15} {:<60}'.format('\033[1m'+str(n)+'\033[0m:',sub_dir))
print('{:>15} {:<60}'.format('\033[1m'+str(n+1)+'\033[0m:','new folder'))
print(' Enter: '+non_arxiv_dir)
signal.alarm(TIMEOUT)
destination=input_des()
signal.alarm(0)
if(destination==0):
path=new_dir
shutil.move(pdf,path+pdf_name)
print("Move the renamded pdf to "+path)
elif(destination>0 and destination <= len(subfolders)):
path = subfolders[destination-1]
shutil.move(pdf,path+pdf_name)
print("Move the renamed pdf to "+path)
elif(destination == len(subfolders)+1):
print('Input the name of the new folder: (xxx/)')
newfolder=str(input() or " ")
path=new_dir+newfolder
os.mkdir(path)
shutil.move(pdf,path+pdf_name)
print("Move the renamed pdf to "+path)
else:
if not os.path.exists(new_dir):
os.makedirs(new_dir)
shutil.move(pdf,non_arxiv_dir+pdf_name)
print('Move the original pdf to '+non_arxiv_dir)
except:
print('Drop '+'\033[1m \"'+pdf+'\" \033[0m'+" to:")
subfolders = glob(new_dir+"*/")
subfolders.sort()
n=0
print('{:>15} {:<60}'.format("\033[1m"+"0\033[0m:",new_dir))
subfolders = glob(new_dir+"*/")
subfolders.sort()
for item in subfolders:
n=n+1
sub_dir=item.replace(new_dir,'')
print('{:>15} {:<60}'.format('\033[1m'+str(n)+'\033[0m:',sub_dir))
print('{:>15} {:<60}'.format('\033[1m'+str(n+1)+'\033[0m:','new folder'))
print(' Enter: '+non_arxiv_dir)
signal.alarm(TIMEOUT)
destination=input_des()
signal.alarm(0)
if(destination==0):
path=new_dir
pdf_name = manual_rename()
if pdf_name != -1:
shutil.move(pdf,path+pdf_name)
print("Move the renamed pdf to "+path)
else:
shutil.move(pdf,path+pdf)
print("Move the original pdf to "+path)
elif(destination>0 and destination <= len(subfolders)):
path = subfolders[destination-1]
pdf_name = manual_rename()
if pdf_name != -1:
shutil.move(pdf,path+pdf_name)
print("Move the renamed pdf to "+path)
else:
shutil.move(pdf,path+pdf)
print("Move the original pdf to "+path)
elif(destination == len(subfolders)+1):
print('Input the name of the new folder: (xxx/)')
newfolder=str(input() or " ")
path=new_dir+newfolder
os.mkdir(path)
pdf_name = manual_rename()
if pdf_name != -1:
shutil.move(pdf,path+pdf_name)
print("Move the renamed pdf to "+path)
else:
shutil.move(pdf,path+pdf)
print("Move the original pdf to "+path)
else:
if not os.path.exists(non_arxiv_dir):
os.makedirs(non_arxiv_dir)
shutil.move(pdf,non_arxiv_dir+pdf)
print('Move the original pdf to '+non_arxiv_dir)
print('')
time.sleep(waittime)
except KeyboardInterrupt:
print(' ')