-
Notifications
You must be signed in to change notification settings - Fork 46
/
humble_download.py
218 lines (183 loc) · 8.52 KB
/
humble_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
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
import os
import requests
from config_data import ConfigData
from humble_api.events import Events
from humble_api.humble_hash import HumbleHash
__author__ = "Brian Schkerke"
__copyright__ = "Copyright 2016 Brian Schkerke"
__license__ = "MIT"
class HumbleDownload(object):
order_number = ""
humble_md5 = ""
filename = ""
platform = ""
product_name = ""
product_name_machine = ""
subproduct_name = ""
download_url = ""
humble_file_size = 0
humble_file_size_human = ""
machine_name = ""
status_message = ""
requires_download = False
partial_download = False
def __init__(self, cd, cds, co, csp, cv):
self.order_number = cv
self.download_url = cds.download_web
self.filename = cds.filename
self.humble_file_size = cds.file_size
self.humble_file_size_human = cds.human_size
self.platform = cd.platform
self.product_name = co.product.human_name
self.product_name_machine = co.product.machine_name
self.subproduct_name = csp.product_name
self.humble_md5 = cds.md5
self.machine_name = cd.machine_name
@property
def local_file_size(self):
"""
Returns the size of the file.
:return: A value representing the size of the file.
"""
if os.path.exists(self.full_filename):
return os.path.getsize(self.full_filename)
else:
return 0
@property
def md5_matches(self):
"""
Determines whether the local MD5 matches the Humble Bundle specified MD5.
:return: True or False depending upon whether the MD5 checksums match.
:rtype: bool
"""
if len(self.local_md5) == 0:
return False
else:
return self.humble_md5 == self.local_md5
@property
def local_md5(self):
"""
Returns the MD5 of the local file.
:return: The checksum for the local file.
"""
if os.path.exists(self.full_filename):
return HumbleHash.checksum(self.full_filename)
else:
return ""
@property
def full_filename(self):
"""
Calculates and returns the full path and filename for the current Humble Bundle download.
:return: The full path and filename for the current download.
"""
return os.path.join(ConfigData.download_location, self.subproduct_name, self.platform, self.filename)
def remove(self):
"""
Removes the file associated with the Humble Bundle download and any accompanying MD5 file.
:return: None
"""
if os.path.exists(self.full_filename):
os.remove(self.full_filename)
HumbleHash.remove_md5file(self.full_filename)
def check_status(self):
"""
This method determines whether or not the given file has already been successfully downloaded.
:return: True if the existing file matches size and checksum verification.
"""
if not os.path.exists(self.full_filename):
self.status_message = "Target %s doesn't exist." % self.filename
self.requires_download = True
elif not self.humble_file_size == self.local_file_size:
self.status_message = "%s file sizes don't match (expected %d actual %d)." % (self.filename,
self.humble_file_size or 0,
self.local_file_size or 0)
self.partial_download = True
self.requires_download = True
elif not ConfigData.ignore_md5:
if not self.md5_matches:
self.status_message = "MD5 of %s doesn't match (expected %s actual %s)." % (self.filename,
self.humble_md5,
self.local_md5)
self.requires_download = True
else:
self.requires_download = False
return not self.requires_download
def download_file(self):
""" Downloads a file from the location specified in the provided DownloadStruct. """
self.__create_directory()
if not ConfigData.resume_downloads:
self.remove()
Events.trigger(Events.EVENT_DOWNLOAD_START, self.filename)
if ConfigData.resume_downloads and self.local_file_size > 0:
self.__resume_download()
else:
self.__start_download()
Events.trigger(Events.EVENT_DOWNLOAD_END, self.filename)
def __resume_download(self):
""" Resumes a download if the server supports it. """
resume_header = {'Range': 'bytes=%d-' % self.local_file_size}
web_request = requests.get(self.download_url, headers=resume_header, stream=True)
mode = "ab"
self.__download_file(web_request, mode, self.local_file_size)
def __start_download(self):
""" Starts a download afresh. """
web_request = requests.get(self.download_url, stream=True)
mode = "wb"
self.__download_file(web_request, mode, 0)
def __download_file(self, web_request, mode, read_bytes=0):
current_percentage = 0
with open(self.full_filename, mode) as f:
# For a download that's resumed the content-length will be the remaining bytes, not the total.
# total_length = int(web_request.headers.get("content-length"))
total_length = self.humble_file_size
chunk_size = ConfigData.chunk_size
for chunk in web_request.iter_content(chunk_size=chunk_size):
read_bytes += chunk_size
read_bytes = min(total_length, read_bytes)
current_percentage = Events.check_percent(read_bytes, total_length, current_percentage)
if chunk:
f.write(chunk)
f.flush()
def __create_directory(self):
""" Creates the directory for storing the current file if it doesn't exist. """
full_directory = os.path.join(ConfigData.download_location, self.subproduct_name, self.platform)
if not os.path.exists(full_directory):
os.makedirs(full_directory)
def is_valid(self):
if self.humble_file_size is None or self.humble_file_size == 0:
self.status_message = "Humble file size reported as 0. Download is invalid."
return False
if self.download_url is None or len(self.download_url) == 0:
self.status_message = "Humble download URL is an empty string. Download is invalid."
return False
if self.humble_md5 is None or len(self.humble_md5) == 0:
self.status_message = "Humble MD5 is an empty string. Download is invalid."
return False
if self.filename is None or len(self.filename) == 0:
self.status_message = "Filename is an empty string. Download is invalid."
return False
return True
def status(self):
if not self.is_valid():
return self.status_message
object_dump = "order_number: %s\n" % self.order_number
object_dump += "humble_md5: %s\n" % self.humble_md5
object_dump += "local_md5: %s\n" % self.local_md5
object_dump += "filename: %s\n" % self.filename
object_dump += "platform: %s\n" % self.platform
object_dump += "product_name: %s\n" % self.product_name
object_dump += "full_filename: %s\n" % self.full_filename
object_dump += "filename: %s\n" % self.filename
object_dump += "platform: %s\n" % self.platform
object_dump += "product_name: %s\n" % self.product_name
object_dump += "product_name_machine: %s\n" % self.product_name_machine
object_dump += "subproduct_name: %s\n" % self.subproduct_name
object_dump += "download_url: %s\n" % self.download_url
object_dump += "humble_file_size: %s\n" % self.humble_file_size
object_dump += "humble_file_size_human: %s\n" % self.humble_file_size_human
object_dump += "local_file_size: %d\n" % self.local_file_size
object_dump += "machine_name: %s\n" % self.machine_name
object_dump += "status_message: %s\n" % self.status_message
object_dump += "requires_download: %s\n" % self.requires_download
object_dump += "partial_download: %s\n" % self.partial_download
return object_dump.encode("utf-8")