-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParsing Arxiv Manifest and querying metadata.py
325 lines (249 loc) · 7.96 KB
/
Parsing Arxiv Manifest and querying metadata.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
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:light
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.5'
# jupytext_version: 1.16.1
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
# name: python3
# ---
# +
import pandas as pd
from glob import glob
import os
# %load_ext autoreload
# %autoreload 2
import dload
# -
mani = dload.parse_manifest('/home/luis/arXiv_src_manifest.xml')
hd1_path = '/media/hd1/arXiv_src/'
rm_path = '/home/luis/rm_me_dload/'
dl = dload.DownloadMan(rm_path, mani, hd1_path + 'downloaded_log.csv', rm_path + 'errors.log')
dl.next_file()
down_set = set([os.path.basename(g) for g in glob(hd1_path + 'src/*.tar')])
all_set = set([os.path.basename(f) for f in mani.filename])
down_list = ['src/'+os.path.basename(g) for g in glob(hd1_path + 'src/*.tar')]
#sorted(list(all_set.difference(down_set)))[:10]
down_df = pd.DataFrame({'filename': down_list})
down_df
idx = dl.downloaded_df.filename.isin(down_df.filename)
dl.downloaded_df[~idx]
dl.downloaded_df.iloc[1220]
# +
root = mani.getroot()
def parse_element(elem):
return_dict = {}
for e in elem:
return_dict[e.tag] = e.text
return return_dict
def parse_root(root):
return [parse_element(child) for child in iter(root) if child.tag != 'timestamp']
filedf = pd.DataFrame(parse_root(root))
filedf[['num_items', 'size']] = filedf[['num_items', 'size']].astype(int)
filedf['filename'] = filedf['filename'].astype(str)
# -
import hashlib
hash_md5 = hashlib.md5()
with open('../arXiv_src_1804_001.tar', "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
hash_md5.hexdigest()
s = filedf.iloc[0]; s
len(s['filename'])
import subprocess
P = subprocess.run(['/bin/s3cmd', 'get', '--requester-pays', 's3://arxiv/src/arXiv_src_1804_006.tar', '..'],
stderr=subprocess.PIPE,
stdout=subprocess.PIPE)
P.stdout
P.stderr
P.check_returncode()
df.filename.isin(notdf.filename)
df.to_csv('head_df.csv')
notdf.to_csv('notdf.csv')
# + jupyter={"outputs_hidden": true}
nondf = pd.read_csv('/home/luis/mountpoint/arXiv_src/downloaded_log.csv',index_col=0)
nondf.append(pd.DataFrame([{'filename':'hola'}]), ignore_index=True)
# -
import os
os.path.ismount('/mnt')
notdf
# %load_ext autoreload
# %autoreload 2
from dload import DownloadMan, parse_manifest
mountpoint = '/mnt/arXiv_src/'
allfiles = 'allfiles2.csv'
doun = 'downloaded_log.csv'
error_log = 'error.log'
D = DownloadMan(mountpoint, allfiles, doun, error_log)
import process as P
import re
name = re.match(r'.*([a-z]+)([0-9]{7}).+', '0703/math0703071.gz')
name.group(1)
P.tar2api2('0703/math0703071.gz', sep='*')
P.detect_format('0701/1701.00112')
# %time xx.extract_tar('../check4', 'math.DG')
[s for s in xx.art_lst if '0701590' in s]
ll = list(range(13))
length = 4
lal = []
i = 0
while i*length < len(ll):
seclen = min((i+1)*length, len(ll))
lal += list(range(i*length, seclen))
i += 1
print(lal)
xx.path_dir('../mathy')
D.check_md5()
new_all = parse_manifest('../arXiv_src_manifest_Oct_2019.xml')
new_all.to_csv('../arXiv_src_manifest_Oct_2019.csv')
for el in res.getchildren():
print(el.tag)
import sys
sys.path.insert(0,'arxiv.py/')
import arxiv
import arxiv
D = arxiv.query(id_list=['1601.00690'])
len(D)
#for d in D[0].keys():
# print(d,D[0][d])
for k in D[0].keys():
print(k , ' :: ' , D[0][k])
len(repr(D[0]['tags']))
# arxiv API usage
D = arxiv.query(id_list=['1601.00105'])
for k in D[0].keys():
print(k,D[0][k])
arxiv.query(id_list=['1805.02773'])[0].links
year_month = '1804.'
id_list = [year_month + str(k).zfill(5) for k in range(1,263)]
dicts = arxiv.query(id_list=id_list, max_results=300)
# + jupyter={"outputs_hidden": true}
diff_geom = [d for d in dicts if d.get('tags',None)[0]['term']=='math.DG']
diff_geom
# -
[d.title for d in diff_geom]
math = [d for d in dicts if 'math' in d['tags'][0]['term']]
# + jupyter={"outputs_hidden": true}
set([d['tags'][0]['term'] for d in math])
# +
import tarfile
with tarfile.open('tests/minitest.tar') as ff:
art_lst = [k for k in ff.getmembers()]
print(art_lst)
# +
import magic
with tarfile.open('tests/minitest2.tar') as ff:
for f in ff:
print(f)
ff.close()
# -
ff = tarfile.open('tests/minitest.tar')
fi = ff.getmembers()[2]
fobj = ff.extractfile(fi.name)
#print("1) ", magic.detect_from_content(fobj.read(2048)))
print("1) ", magic.from_buffer(fobj.read(2048)))
fobj.seek(0)
unz_file = tarfile.open(fileobj=fobj, mode='r:gz')
unz_file.getmembers()
#unz_file = gzip.open(fobj, 'rb')
#print("2) ", magic.detect_from_content(unz_file.read(2048)))
#unz_file.seek(0)
#unz_tar = tarfile.open(fileobj=unz_file)
#print(unz_tar.getmembers())
magic.from_file('/media/hd1/arXiv_src/src/arXiv_src_0003_001.tar')
import os
os.path.curdir
# + jupyter={"outputs_hidden": true}
with tarfile.open('tests/minitest2.tar') as ff:
for fi in ff.getmembers()[1:]:
fobj = ff.extractfile(fi.name)
the_magic = magic.detect_from_content(fobj.read(2048))
fobj.seek(0)
print(fi.name,the_magic.name)
if 'gzip compressed' in the_magic.name:
try:
with tarfile.open(fileobj=fobj, mode='r:gz') as unzipped_file:
#snd_magic = magic.detect_from_fobj(unzipped_file)
#unz_tarinfo = unzipped_file.next()
pass
#if unz_tarinfo:
# print(" * is regular ", unz_tarinfo.isreg())
#unzipped_file.seek(0)
#if snd_magic.mime_type == 'application/x-tar':
# unzipped.getmembers()
except gzip.BadGzipFile:
print('gave me badgzipfile')
# -
pro.
import gzip
with tarfile.open('tests/minitest2.tar') as ff:
for fi in ff.getmembers()[1:]:
fobj = ff.extractfile(fi.name)
the_magic = magic.detect_from_content(fobj.read(2048))
fobj.seek(0)
print(pro.Tarfi.name,the_magic.name)
if '.tex.cry"' in the_magic.name:
print("cry baby cry")
else:
try:
with gzip.open(fobj,'rb') as unzipped_file:
snd_magic = magic.detect_from_content(unzipped_file.read(2048))
unzipped_file.seek(0)
print(" *", snd_magic.name)
if snd_magic.mime_type == 'application/x-tar':
with tarfile.open(fileobj=unzipped_file) as tars:
print(" * There are ", len(tars.getmembers()), 'items')
except gzip.BadGzipFile:
print('gave me badgzipfile')
print(' ')
print(snd_magic.mime_type)
print(snd_magic.encoding)
print(snd_magic.name)
with open('./zhu_untared.tex', 'w') as f:
f.write(zhu_str.decode('utf-8'))
# +
import gzip
import chardet
import tarfile
file_tar = tarfile.open('/mnt/arXiv_src/src/arXiv_src_9904_001.tar')
#for f in file_tar.getmembers():
# print(f.get_info())
tar2 = file_tar.extractfile('9904/math9904086.gz')
#print(chardet.detect(tar2))
#with open('../todo_macizo.pdf','w') as todo_macizo:
# todo_macizo.write(tar2.read().decode('utf-8'))
#with tarfile.open(fileobj=tar2) as tars:
# for t in tars:
# print(t.get_info()['name'])
#gz = gzip.open(tar2, 'rb')
with gzip.open(tar2,'r') as tars:
t = tars.read()
#print(t.decode('utf-8'))
chardet.detect(t)
#with open('../holita.txt','w') as holaf:
# holaf.write(allfile.read().decode('utf-8'))
# + jupyter={"outputs_hidden": true}
t.decode('cp932')
#t.decode(errors='ignore')
# -
try:
t.decode()
except UnicodeDecodeError as e:
print('todo bien')
print(e)
import difflib
Diffs = difflib.unified_diff(t.decode(errors='ignore').splitlines(), t.decode('koi8-r'))
for d in Diffs:
print(d)
with tarfile.open('tests/minitest.tar') as morgan:
for mor in morgan:
print(mor.name)
import chardet
chardet.detect(t)
import glob
glob.glob('/mnt/arXiv_src/src/arXiv_src_1805_*')