-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTTPDownload.py
55 lines (47 loc) · 1.46 KB
/
HTTPDownload.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
import urllib2
import os
import datetime
from datetime import datetime
import csv
errors = []
def downloadUrl(url, count, row_count):
try:
file_name = url.split('/')[-1]
u = urllib2.urlopen(url)
f = open(file_name, 'wb')
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
print "[%d / %d] Downloading: %s Bytes: %s" %(count, row_count, file_name, file_size)
file_size_dl = 0
block_sz = 8192
while True:
buffer = u.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
f.write(buffer)
status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
status = status + chr(8)*(len(status)+1)
print status,
f.close()
except:
errors.append(url)
print '*** ERROR WITH:', url
file = open("urls.csv")
row_count = len(file.readlines())
print("")
with open('urls.txt', 'rb') as f:
count = 1
reader = csv.reader(f, delimiter=' ')
exportDIR = datetime.now().strftime('%Y-%m-%d_%H.%M.%S')
os.system('mkdir ' + exportDIR)
os.chdir(exportDIR)
for row in reader:
url = row[0]
downloadUrl(url, count, row_count)
count = count + 1
print '\n\n[ FINISHED DOWNLOADING FILES ]\n'
if len(errors) > 0:
print '(%d) ERRORS:' %(len(errors))
for element in errors:
print element