-
Notifications
You must be signed in to change notification settings - Fork 2
/
functions.py
124 lines (114 loc) · 4.3 KB
/
functions.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
#!/usr/bin/env python
import urllib2, os, sys, math, urllib
from bs4 import BeautifulSoup
from datetime import timedelta, date
import time
class outputcolors:
OKGREEN = '\033[92m'
OKBLUE = '\033[94m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
def getStatus(url):
try:
connection = urllib2.urlopen(url)
code = connection.getcode()
connection.close()
return code
except urllib2.HTTPError, e:
return e.getcode()
def roundUpTo(x, base):
return int(base * math.ceil(float(x) / base))
def roundDownTo(x, base):
return int(base * math.floor(float(x) / base))
def ensureDir(f):
if not os.path.exists(f):
os.makedirs(f)
def replaceTab(s, tabstop = 4):
result = str()
for c in s:
if c == '\t':
while (len(result) % tabstop != 0):
result += ' ';
else:
result += c
return result
def fileDl(url, dir, prepend, fileName = "?"):
if fileName == "?":
fileName = url.split('/')[-1]
request = urllib2.Request(url)
u = urllib2.urlopen(request)
meta = u.info()
fileSize = -1
try:
fileSize = int(meta.getheaders("Content-Length")[0])
except Exception:
pass
if os.path.exists(dir + fileName):
if os.stat(dir + fileName).st_size == fileSize:
print(prepend + outputcolors.OKBLUE + "File already downloaded!" + outputcolors.ENDC)
return 42
else:
print(prepend + outputcolors.WARNING + "File downloaded but not fully! Restarting download..." + outputcolors.ENDC)
else:
print(prepend + outputcolors.WARNING + "Downloading file..." + outputcolors.ENDC)
fileHandle = open(dir + fileName, 'wb')
print(prepend + ("Downloading: %s Bytes: %s" % (fileName, "???" if (fileSize == -1) else fileSize)))
fileSizeDl = 0
blockSize = 65536
while True:
buffer = u.read(blockSize)
if not buffer:
break
fileSizeDl += len(buffer)
fileHandle.write(buffer)
status = prepend + r"%12d [%3.2f%%]" % (fileSizeDl, -1.0 if (fileSize == -1) else (fileSizeDl * 100. / fileSize))
status = "\r" + status
print status,
fileHandle.close()
print("\n" + prepend + outputcolors.OKGREEN + "Done :)" + outputcolors.ENDC)
return 1
def fileDlWithAuth(url, auth, dir, prepend):
fileName = url.split('/')[-1]
request = urllib2.Request(url)
request.add_header("Authorization", "Basic %s" % auth)
u = urllib2.urlopen(request)
meta = u.info()
fileSize = -1
try:
fileSize = int(meta.getheaders("Content-Length")[0])
except Exception:
pass
if os.path.exists(dir + fileName):
if os.stat(dir + fileName).st_size == fileSize:
print(prepend + outputcolors.OKBLUE + "File already downloaded!" + outputcolors.ENDC)
return 42
else:
print(prepend + outputcolors.WARNING + "File downloaded but not fully! Restarting download..." + outputcolors.ENDC)
else:
print(prepend + outputcolors.WARNING + "Downloading file..." + outputcolors.ENDC)
fileHandle = open(dir + fileName, 'wb')
print(prepend + ("Downloading: %s Bytes: %s" % (fileName, "???" if (fileSize == -1) else fileSize)))
fileSizeDl = 0
blockSize = 65536
while True:
buffer = u.read(blockSize)
if not buffer:
break
fileSizeDl += len(buffer)
fileHandle.write(buffer)
status = prepend + r"%12d [%3.2f%%]" % (fileSizeDl, -1.0 if (fileSize == -1) else (fileSizeDl * 100. / fileSize))
status = "\r" + status
print status,
fileHandle.close()
print("\n" + prepend + outputcolors.OKGREEN + "Done :)" + outputcolors.ENDC)
def getSoup(url):
try:
return BeautifulSoup(urllib2.urlopen(urllib2.Request(url)), "lxml")
except urllib2.HTTPError, e:
print("retrying in 5s")
time.sleep(5)
return getSoup(url)
def daterange(start_date, end_date):
for n in range(int ((end_date - start_date).days)):
yield start_date + timedelta(n)