-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDownload.py
170 lines (142 loc) · 5.42 KB
/
Download.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
import pycurl
import re
import os.path
import os
import urllib
from io import BytesIO
class Download:
"""
Download object, should be called at least with two paramteres
one the url to download and then the path which the url should be
saved in.
example:
down = Download("https://google.com", "/home/user/google/")
down.perform()
"""
def __init__(self, url, download_path=None, username=None, password=None,
output_name=None, as_var=False, post_dict=None):
self.username = username
self.url = url
self.password = password
self.header = BytesIO()
self.curl = pycurl.Curl()
self.curli = pycurl.Curl()
self.output_name = output_name
self.download_path = download_path
self.local_file_size = None
self.remote_file_size = None
self.result = None
self.data = None
self.as_var = as_var
self.post_dict = post_dict
def set_output_name(self, output_name):
self.output_name = output_name
def set_url(self, url):
self.url = url
def set_username(self, username):
self.username = username
def set_password(self, password):
self.password = password
def set_header(self, header):
self.header = header
def get_output_name(self):
return self.output_name
def get_url(self):
return self.url
def get_password(self):
return self.password
def get_username(self):
return self.username
def get_header(self):
return self.header
def set_download_path(self, download_path):
if(download_path[len(download_path) - 1] != "/"):
self.download_path = download_path + "/"
def get_download_path(self):
return self.download_path
def open_file(self, name):
self.data = open(self.download_path + str(name), 'wb')
def output_name_fallback(self):
if(not self.get_output_name()):
splited_url = self.url.split("/")
self.set_output_name(splited_url[len(splited_url) - 1])
def check_file_exsists(self):
self.set_download_path(self.download_path)
return os.path.isfile(self.download_path + self.get_output_name())
def ok(self):
header = str(self.get_header().getvalue())
splited_header = header.split("\\r\\n")
if(re.search("200", splited_header[0])):
return True
else:
return False
def get_result(self):
return self.result
def success(self):
if(self.ok() and (self.local_file_size == self.remote_file_size)):
return True
else:
if(self.check_file_exsists()):
os.remove(self.get_file_address())
return False
def get_file_address(self):
return self.get_download_path() + self.get_output_name()
def get_file_size(self):
self.curli.setopt(self.curli.URL, self.get_url())
self.curli.setopt(self.curli.NOBODY, 1)
self.curli.perform()
self.remote_file_size = self.curli.getinfo(
self.curl.CONTENT_LENGTH_DOWNLOAD)
self.curli.close()
if(self.check_file_exsists()):
self.local_file_size = os.path.getsize(self.get_file_address())
def perform_var(self):
self.result = BytesIO()
self.curl.setopt(self.curl.URL, self.get_url())
self.curl.setopt(self.curl.FOLLOWLOCATION, True)
self.curl.setopt(self.curl.VERBOSE, 1)
if (self.get_username() and self.get_password()):
self.curl.setopt(self.curl.USERPWD,
self.get_username() + ":" + self.get_password())
if (self.post_dict):
self.curl.setopt(self.curl.POSTFIELDS,
urllib.urlencode(self.post_dict))
self.curl.setopt(self.curl.WRITEDATA, self.result)
self.curl.setopt(self.curl.HEADERFUNCTION, self.header.write)
self.curl.perform()
self.curl.close()
return self.ok()
def perform_file(self):
self.output_name_fallback()
if(not self.check_file_exsists()):
self.open_file(self.get_output_name())
self.curl.setopt(self.curl.URL, self.get_url())
self.curl.setopt(self.curl.FOLLOWLOCATION, True)
self.curl.setopt(self.curl.VERBOSE, 1)
if (self.get_username() and self.get_password()):
self.curl.setopt(self.curl.USERPWD,
self.get_username() + ":" + self.get_password())
if (self.post_dict):
self.curl.setopt(self.curl.POSTFIELDS,
urllib.urlencode(self.post_dict))
self.curl.setopt(self.curl.WRITEDATA, self.data)
self.curl.setopt(self.curl.HEADERFUNCTION, self.header.write)
self.curl.perform()
self.curl.close()
self.data.close()
self.get_file_size()
return self.success()
else:
self.get_file_size()
return (self.check_file_exsists() and
(self.local_file_size == self.remote_file_size))
def perform(self):
if(self.as_var):
return self.perform_var()
else:
return self.perform_file()
# down = Download(
# ("http://eoimages.gsfc.nasa.gov/images/imagerecords/36000/36377/
# "Australia_AMO_2009006_lrg.jpg"), "images")
# a = down.perform()
# print(a)