-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWARCMerge.py
469 lines (428 loc) · 14.8 KB
/
WARCMerge.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#!/usr/bin/env python
# Author: Mohamed Aturban, WS-DL Group, CS department, Old Dominion University
import datetime
import getopt
import sys
import os
import warc # from : http://warc.readthedocs.org/en/latest/ (read WARC records)
MaxWarcSize = 500.0 # in MB
forConvertToMB = float(2<<19)
# return a list of all WARC files in the given directory
# from http://stackoverflow.com/questions/3964681/find-all-files-in-# directory-with-extension-txt-with-python
def list_files(startPath):
l = []
for root, dirs, files in os.walk(startPath):
for file in files:
if file.endswith(".warc"):
l.append(os.path.join(root, file))
return l
def timeStampedFilename(fn):
tstr = fn+''+str(datetime.datetime.utcnow())
tstr = tstr.replace(':', '').replace('-', '').replace('.', '').replace(' ', '')
return tstr
# Create "worcinfo" record header and content
def createWarcInfoReacord(filename):
H = warc.WARCHeader({"WARC-Type": "warcinfo", \
"WARC-Filename" : filename}, \
defaults=True)
Content = "software: WARCMerge/1.0" + "\r\n" \
+ "format: WARC File Format 1.0" + "\r\n" \
+ "description: "+" Merging WARC files into a single one " + "\r\n" + \
"robots: ignore" + "\r\n"
R = warc.WARCRecord(H, Content)
return R
# Sort file list by size
# from : http://stackoverflow.com/questions/20252669/get-files-from-directory-argument-sorting-by-size
def sortFiles(fileList):
for i in xrange(len(fileList)):
fileList[i] = (fileList[i], os.path.getsize(fileList[i]))
fileList.sort(key=lambda filename: filename[1], reverse=False)
for i in xrange(len(fileList)):
fileList[i] = fileList[i][0]
return fileList
# check if WARC file is valid
# from https://bitbucket.org/nclarkekb/jwat-tools/downloads
def isWarcValid(warcfile):
flagV = 0 # valid
str = "java -Xms256m -Xmx1048m -XX:PermSize=64M -XX:MaxPermSize=256M -jar target/jwat-tools-*-jar-with-dependencies.jar -t "+warcfile
res = os.popen(str).read().split()
try:
resIndex = res.index('Errors:')
if(res[resIndex + 1] == '0'):
flagV = 0 # WARC valid
else:
flagV = -1 # WARC invalid
except Exception as e:
flagV = -2 # not working
if quietMode == False:
print ("Warning: WARC validator (jwattools.sh) cannot be run")
pass
return flagV
def showUsage():
if quietMode == False:
print '\n usage: WARCMerge [[-q] -a <source-file> <Destination-file> ]\n' + \
' [[-q] <input-directory> <output-directory> ]\n' + \
' [[-q] <file1> <file2> <file3> ... <output-directory> ]\n'
sys.exit(0)
############################################
outputFilesList = []
quietMode = False
flags = []
try:
opts, args = getopt.getopt(sys.argv[1:],"haq",)
for v in opts:
if v[0] == '-q':
quietMode = True
else:
flags.append(v[0])
except getopt.GetoptError:
showUsage()
if len(flags) == 0:
for s in args:
if s[0] == '-':
showUsage()
if len(args) < 2:
showUsage()
# merging WARCs into a new WARC file
else:
Ddir = ''
dirTree = []
# meging WARCs in a directory into an output dirctory
if len(args) == 2:
Sdir = args[0]#sys.argv[1]
Ddir = args[1]#sys.argv[2]
if os.path.isdir(Sdir) == False:
if quietMode == False:
print('\n No such directory: '+Sdir+' \n')
print('\n use -a to append one file to another \n')
sys.exit(0)
if os.path.isdir(Ddir) == False:
# Create the destination file if possible
os.makedirs(Ddir)
dirTree = list_files(Sdir)
if len(dirTree) == 0:
if quietMode == False:
print('\n No WARCs found in the directory: '+Sdir+' \n')
sys.exit(0)
# meging list of WARCs into an output dirctory
else:
Ddir = args[-1]
if os.path.isdir(Ddir) == False:
# Create the destination file if possible
os.makedirs(Ddir)
flagCheckFiles = 0
for f in args[:-1]:
if (os.path.isfile(f) == True) and (f.endswith('.warc') == True):
dirTree.append(f)
else:
flagCheckFiles = 1
if flagCheckFiles == 1:
if quietMode == False:
print "\n The given list contains one or more files that are not in valid WARC format"
if len(dirTree) == 0:
if quietMode == False:
print('\n No WARC files found in given list \n')
sys.exit(0)
outputPath = Ddir
# generate new warc file name
newFile = timeStampedFilename("WARCMerge")+'.warc'
newFileFullPath = outputPath+'/'+newFile
filePtr = warc.open(newFileFullPath, "w")
outputFilesList.append(newFileFullPath)
flag = 0
outputFileSize = os.path.getsize(newFileFullPath) / forConvertToMB
# Sorting files by sizes
sortFiles(dirTree)
if quietMode == False:
print
print 'Merging the following WARC files: '
print '----------------------------------: '
for warcFile in dirTree:
outputFileSize = os.path.getsize(newFileFullPath) / forConvertToMB
fileSize = os.path.getsize(warcFile) / forConvertToMB
if ((fileSize + outputFileSize) > MaxWarcSize) and (outputFileSize != 0) :
filePtr.close()
# generate new warc file name if current file exceeds WARC file size limit
newFile = timeStampedFilename("WARCMerge")+'.warc'
newFileFullPath = outputPath+'/'+newFile
filePtr = warc.open(newFileFullPath, "w")
outputFilesList.append(newFileFullPath)
flag = 0
f = warc.WARCFile(warcFile, "rb")
try:
for record in f:
if flag == 0:
R = createWarcInfoReacord(newFile)
filePtr.write_record(R)
flag = 1
if ("warcinfo" in record['WARC-Type']):
New_Payload = record.payload.read().strip()+ "\r\n" +"WARC-appended-by-WARCMerge: "+datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ') + "\r\n"
record['Content-Length'] = str(len(New_Payload))
R = warc.WARCRecord(record.header , New_Payload, defaults=False)
else:
R = warc.WARCRecord(payload=record.payload.read(), headers=record.header, defaults=False)
filePtr.write_record(R)
if quietMode == False:
print '[Yes]' + warcFile
except Exception as e:
#print("Exceptionq: %s"%(str(e)))
if quietMode == False:
print '[No]' + warcFile
pass
filePtr.close()
outputFileSize = os.path.getsize(newFileFullPath) / forConvertToMB
if outputFileSize == 0:
os.remove(newFileFullPath)
if quietMode == False:
print '\nValidating the resulting WARC files: '
print '----------------------------------: '
#dirTreeCheckWARCs = list_files(outputPath)
dirTreeCheckWARCs = outputFilesList
for filePath in dirTreeCheckWARCs:
correct = isWarcValid(filePath)
if correct == -2:
break
if correct == 0:
if quietMode == False:
print '[valid] '+filePath+''
else:
if correct == -1:
if quietMode == False:
print '[Invalid] '+filePath+''
else:
if quietMode == False:
print '[WARC validator (jwattools.sh) cannot be run]\t\t'+filePath+''
if quietMode == False:
print
elif len(flags) == 1:
if flags[0] == '-h':
showUsage()
elif flags[0] == '-a':
if len(args) != 2:
showUsage()
################ Appending ###############
# Appending to an existing WARC file
#if len(sys.argv) == 3:
Sfile = args[0]#sys.argv[1]
Dfile = args[1]#sys.argv[2]
if not os.path.isfile(Sfile):
if quietMode == False:
print('\n No such file: '+Sfile+' \n')
sys.exit(0)
if not os.path.isfile(Dfile):
if quietMode == False:
print('\n No such file: '+Dfile+' \n')
sys.exit(0)
if (not Sfile.endswith(".warc")):
if quietMode == False:
print ("\n Source file ("+Sfile+") is not WARC file")
sys.exit(0)
if (not Dfile.endswith(".warc")):
if quietMode == False:
print ("\n Destination file ("+Dfile+") is not WARC file")
sys.exit(0)
correct = isWarcValid(Sfile)
if correct == -2:
if quietMode == False:
print ("Warning: WARC validator (jwattools.sh) cannot be run")
else:
if correct != 0:
if quietMode == False:
print ("\n Source file ("+Sfile+") is not valid WARC file")
sys.exit(0)
correct = isWarcValid(Dfile)
if correct == -2:
if quietMode == False:
print ("Warning: WARC validator (jwattools.sh) cannot be run")
else:
if correct != 0:
if quietMode == False:
print ("\n Destination file ("+Dfile+") is not valid WARC file")
sys.exit(0)
if (( (os.path.getsize(Sfile)/ forConvertToMB) + (os.path.getsize(Dfile)/ forConvertToMB)) > MaxWarcSize):
if quietMode == False:
print ("\n Connot merge <Souce> file with <Destination> file: [Maximum <Destination> WARC file is "+str(MaxWarcSize)+" MB].")
sys.exit(0)
try:
filePtr = warc.open(Dfile, "a")
except Exception as e:
if quietMode == False:
print 'Error in writing in:' + Dfile
sys.exit(0)
try:
f = warc.WARCFile(Sfile, "rb")
for record in f:
if ("warcinfo" in record['WARC-Type']):
New_Payload = record.payload.read().strip()+ "\r\n" +"WARC-appended-by-WARCMerge: "+datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ') + "\r\n"
record['Content-Length'] = str(len(New_Payload))
R = warc.WARCRecord(record.header , New_Payload, defaults=False)
else:
R = warc.WARCRecord(payload=record.payload.read(), headers=record.header, defaults=False)
filePtr.write_record(R)
except Exception as e:
if quietMode == False:
print 'Error in reading:' + Sfile
sys.exit(0)
f.close()
filePtr.close()
correct = isWarcValid(Dfile)
if correct ==0:
if quietMode == False:
print ("\n\t The resulting Destination: ("+Dfile+") is valid WARC file")
if correct != 0:
if quietMode == False:
print ("\n The resulting Destination file ("+Dfile+") is not valid WARC file")
sys.exit(0)
else:
showUsage()
if os.path.isfile("./v.out"):
os.remove("./v.out")
if os.path.isfile("./e.out"):
os.remove("./e.out")
'''
# Appending to an existing WARC file
if len(sys.argv) == 3:
Sfile = sys.argv[1]
Dfile = sys.argv[2]
if not os.path.isfile(Sfile):
print('\n No such file: '+Sfile+' \n')
sys.exit(0)
if not os.path.isfile(Dfile):
print('\n No such file: '+Dfile+' \n')
sys.exit(0)
if (not Sfile.endswith(".warc")):
print ("\n Source file ("+Sfile+") is not WARC file")
sys.exit(0)
if (not Dfile.endswith(".warc")):
print ("\n Dest file ("+Dfile+") is not WARC file")
sys.exit(0)
correct = isWarcValid(Sfile)
if correct == -2:
print ("Warning: WARC validator (jwattools.sh) cannot be run")
else:
if correct != 0:
print ("\n Source file ("+Sfile+") is not valid WARC file")
sys.exit(0)
correct = isWarcValid(Dfile)
if correct == -2:
print ("Warning: WARC validator (jwattools.sh) cannot be run")
else:
if correct != 0:
print ("\n Dest file ("+Dfile+") is not valid WARC file")
sys.exit(0)
if (( (os.path.getsize(Sfile)/ forConvertToMB) + (os.path.getsize(Dfile)/ forConvertToMB)) > MaxWarcSize):
print ("\n Connot merge <Souce> file with <Dest> file: [Maximum <Dest> WARC file is "+str(MaxWarcSize)+" MB].")
sys.exit(0)
try:
filePtr = warc.open(Dfile, "a")
except Exception as e:
print 'Error in writing in:' + Dfile
sys.exit(0)
try:
f = warc.WARCFile(Sfile, "rb")
for record in f:
if ("warcinfo" in record['WARC-Type']):
New_Payload = record.payload.read().strip()+ "\r\n" +"WARC-appended-by-WARCMerge: "+datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ') + "\r\n"
record['Content-Length'] = str(len(New_Payload))
R = warc.WARCRecord(record.header , New_Payload, defaults=False)
else:
R = warc.WARCRecord(payload=record.payload.read(), headers=record.header, defaults=False)
filePtr.write_record(R)
except Exception as e:
print 'Error in reading:' + Sfile
sys.exit(0)
f.close()
filePtr.close()
correct = isWarcValid(Dfile)
if correct ==0:
print ("\n\t The resulting Dest: ("+Dfile+") is valid WARC file")
if correct != 0:
print ("\n The resulting Dest file ("+Dfile+") is not valid WARC file")
sys.exit(0)
else:
# Merging to new WARC file(s)
if len(sys.argv) == 2:
if os.path.isdir(sys.argv[1]) == False:
print('\n No such directory: '+sys.argv[1]+' \n')
sys.exit(0)
dirTree = list_files(sys.argv[1])
if len(dirTree) == 0:
print('\n No WARCs found in the directory: '+sys.argv[1]+' \n')
sys.exit(0)
outputPath = "./merging-WARCs/"
if not os.path.exists(outputPath):
os.makedirs(outputPath)
# create a new folder
outputPath = outputPath + timeStampedFilename("WARCMerge")
os.makedirs(outputPath)
# generate new warc file name
newFile = timeStampedFilename("WARCMerge")+'.warc'
newFileFullPath = outputPath+'/'+newFile
filePtr = warc.open(newFileFullPath, "w")
flag = 0
outputFileSize = os.path.getsize(newFileFullPath) / forConvertToMB
# Sorting files by sizes
sortFiles(dirTree)
print
print 'Merging the following WARC files: '
print '----------------------------------: '
for warcFile in dirTree:
outputFileSize = os.path.getsize(newFileFullPath) / forConvertToMB
fileSize = os.path.getsize(warcFile) / forConvertToMB
if ((fileSize + outputFileSize) > MaxWarcSize) and (outputFileSize != 0) :
filePtr.close()
# generate new warc file name if current file exceeds WARC file size limit
newFile = timeStampedFilename("WARCMerge")+'.warc'
newFileFullPath = outputPath+'/'+newFile
filePtr = warc.open(newFileFullPath, "w")
flag = 0
f = warc.WARCFile(warcFile, "rb")
try:
for record in f:
if flag == 0:
R = createWarcInfoReacord(newFile)
filePtr.write_record(R)
flag = 1
if ("warcinfo" in record['WARC-Type']):
New_Payload = record.payload.read().strip()+ "\r\n" +"WARC-appended-by-WARCMerge: "+datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ') + "\r\n"
record['Content-Length'] = str(len(New_Payload))
R = warc.WARCRecord(record.header , New_Payload, defaults=False)
else:
R = warc.WARCRecord(payload=record.payload.read(), headers=record.header, defaults=False)
filePtr.write_record(R)
print '[ Yes ]' + warcFile
except Exception as e:
#print("Exceptionq: %s"%(str(e)))
print '[ No ]' + warcFile
pass
filePtr.close()
outputFileSize = os.path.getsize(newFileFullPath) / forConvertToMB
if outputFileSize == 0:
os.remove(newFileFullPath)
print '\nValidating the resulting WARC files: '
print '----------------------------------: '
dirTreeCheckWARCs = list_files(outputPath)
for filePath in dirTreeCheckWARCs:
correct = isWarcValid(filePath)
if correct == -2:
break
if correct == 0:
print '- [ valid ]\t'+filePath+''
else:
if correct == -1:
print '- [ Invalid ]\t'+filePath+''
else:
print '- [ WARC validator (jwattools.sh) cannot be run ]\t\t'+filePath+''
print
else:
print '\n Usage: %python WARCMerge.py <Path-to-directory-of-WARC-files> \n Or'
print ' %python WARCMerge.py <Path-to-SOURCE-WARC-file> <Path-to-DEST-WARC-file> \n'
sys.exit(0)
if os.path.isfile("./v.out"):
os.remove("./v.out")
if os.path.isfile("./e.out"):
os.remove("./e.out")
'''
'''
record['WARC-Type']
'''