forked from plintx/ChomikDownloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chomyk.py
executable file
·393 lines (338 loc) · 10.6 KB
/
chomyk.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import getopt
import hashlib
import requests
import os
import time
import threading
import re
from xml.etree import ElementTree as et
from collections import OrderedDict
from getpass import getpass
class Item(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.id = 0
self.AgreementInfo = 'own'
self.realId = 0
self.name = ''
self.url = ''
self.num = 1
self.status = 'open'
self.directory = ""
self.progress = None
def getProgress(self):
if self.progress is None:
return "{:>2s}. {: <20s} : {}".format(str(self.num), self.name[:20], "Oczekuje...",)
else:
return self.progress
def run(self):
self.status = 'inprogress'
path = self.directory+'/'+self.name
try:
file_size = os.path.getsize(path)
except:
file_size = 0
r = requests.get(self.url, stream=True, verify=False, allow_redirects=True)
total_length = int(r.headers.get('content-length'))
file_attr = 'wb'
if total_length > file_size and file_size > 0:
file_attr = 'ab'
resume_header = {'Range': 'bytes=%d-' % file_size}
r = requests.get(self.url, headers=resume_header, stream=True, verify=False, allow_redirects=True)
if file_size < total_length:
with open(path, file_attr) as fd:
dl_size = file_size
for chunk in r.iter_content(chunk_size=128):
dl_size += len(chunk)
progress = dl_size * 100. / total_length
self.progress = "{:>2s}. {: <20s} {: >10d}KB {: >3d}% [{: <25s}]".format(str(self.num), self.name[:20], int(dl_size/(1024)),int(progress),"#"*int(progress/4))
fd.write(chunk)
self.status = 'done'
elif file_size == total_length:
self.progress = "{:>2s}. {: <20s} : {}".format(str(self.num), self.name[:20], "Plik istnieje na dysku")
self.status = 'done'
class Chomyk:
def __init__(self, username, password,maxThreads,directory):
self.isLogged = True
self.lastLoginTime = 0
self.hamsterId = 0
self.token = ''
self.items = 0
self.threads = []
self.accBalance = None
self.maxThreads = int(maxThreads)
self.directory = directory
self.threadsChecker = None
self.totalItems = 0
self.username = username
self.password = hashlib.md5(password.encode("utf-8")).hexdigest()
self.cls()
self.checkThreads()
self.login()
def cls(self):
os.system('cls' if os.name=='nt' else 'clear')
def printline(self, line, text):
sys.stdout.write("\x1b7\x1b[%d;%df%s\x1b8" % (line, 2, text))
sys.stdout.flush()
def checkThreads(self):
threadsInprogress = 0
threadsOpen = 0
threadsDone = 0
for it in self.threads:
self.printline(it.num+3, it.getProgress())
if it.status == 'inprogress':
threadsInprogress += 1
if it.status == 'open':
threadsOpen += 1
if threadsInprogress < self.maxThreads:
threadsInprogress += 1
threadsOpen -= 1
it.start()
#it.join()
if it.status == 'done':
threadsDone += 1
if threadsDone == self.totalItems and threadsDone > 0 and threadsOpen == 0:
self.threadsChecker.cancel()
self.cls()
print("\r\nWszystkie pliki zostaly pobrane")
print("\r")
else:
self.threadsChecker = threading.Timer(1.0, self.checkThreads)
self.threadsChecker.start()
def postData(self, postVars):
url = "http://box.chomikuj.pl/services/ChomikBoxService.svc"
body = postVars.get("body")
headers = {
"SOAPAction": postVars.get("SOAPAction"),
"Content-Encoding": "identity",
"Content-Type": "text/xml;charset=utf-8",
"Content-Length": str(len(body)),
"Connection": "Keep-Alive",
"Accept-Encoding": "identity",
"Accept-Language": "pl-PL,en,*",
"User-Agent": "Mozilla/5.0",
"Host": "box.chomikuj.pl",
}
response = requests.post(url, data=body, headers=headers)
self.parseResponse(response.content)
def dl(self, url):
fileUrl = re.search('[http|https]://chomikuj.pl(.*)', url).group(1)
rootParams = {
"xmlns:s": "http://schemas.xmlsoap.org/soap/envelope/",
"s:encodingStyle": "http://schemas.xmlsoap.org/soap/encoding/"
}
root = et.Element('s:Envelope', rootParams)
body = et.SubElement(root, "s:Body")
downloadParams = {
"xmlns": "http://chomikuj.pl/"
}
download = et.SubElement(body, "Download", downloadParams)
downloadSubtree = OrderedDict([
("token", self.token,),
("sequence", [
("stamp", "123456789"),
("part", "0"),
("count", "1")
]),
("disposition", "download"),
("list", [
("DownloadReqEntry", [
("id", fileUrl),
])
])
])
self.add_items(download, downloadSubtree)
xmlDoc = """<?xml version="1.0" encoding="UTF-8"?>"""
xmlDoc += et.tostring(root, encoding='unicode', method='xml')
dts = {
"body": xmlDoc,
"SOAPAction": "http://chomikuj.pl/IChomikBoxService/Download"
}
self.postData(dts)
def dl_step_2(self, idx, agreementInfo,cost = 0):
rootParams = {
"xmlns:s": "http://schemas.xmlsoap.org/soap/envelope/",
"s:encodingStyle": "http://schemas.xmlsoap.org/soap/encoding/"
}
root = et.Element('s:Envelope', rootParams)
body = et.SubElement(root, "s:Body")
downloadParams = {
"xmlns": "http://chomikuj.pl/"
}
download = et.SubElement(body, "Download", downloadParams)
downloadSubtree = OrderedDict([
("token", self.token,),
("sequence", [
("stamp", "123456789"),
("part", "0"),
("count", "1")
]),
("disposition", "download"),
("list", [
("DownloadReqEntry", [
("id", idx),
("agreementInfo", [
("AgreementInfo", [
("name", agreementInfo),
("cost", cost),
])
])
])
])
])
self.add_items(download, downloadSubtree)
xmlDoc = """<?xml version="1.0" encoding="UTF-8"?>"""
xmlDoc += et.tostring(root, encoding='unicode', method='xml')
dts = {
"body": xmlDoc,
"SOAPAction": "http://chomikuj.pl/IChomikBoxService/Download"
}
self.postData(dts)
def login(self):
rootParams = {
"xmlns:s": "http://schemas.xmlsoap.org/soap/envelope/",
"s:encodingStyle": "http://schemas.xmlsoap.org/soap/encoding/"
}
root = et.Element('s:Envelope', rootParams)
body = et.SubElement(root, "s:Body")
authParams = {
"xmlns": "http://chomikuj.pl/"
}
auth = et.SubElement(body, "Auth", authParams)
authSubtree = OrderedDict([
("name", self.username,),
("passHash", self.password,),
("ver", "4"),
("client", OrderedDict([
("name", "chomikbox"),
("version", "2.0.5"),
]))
])
self.add_items(auth, authSubtree)
xmlDoc = """<?xml version="1.0" encoding="UTF-8"?>"""
xmlDoc += et.tostring(root, encoding='unicode', method='xml')
dts = {
"body": xmlDoc,
"SOAPAction": "http://chomikuj.pl/IChomikBoxService/Auth"
}
self.postData(dts)
def add_items(self, root, items):
if type(items) is OrderedDict:
for name, text in items.items():
if type(text) is str:
elem = et.SubElement(root, name)
elem.text = text
if type(text) is list:
subroot = et.SubElement(root, name)
self.add_items(subroot, text)
elif type(items) is list:
for name, text in items:
if type(text) is str:
elem = et.SubElement(root, name)
elem.text = text
if type(text) is list:
subroot = et.SubElement(root, name)
self.add_items(subroot, text)
def parseResponse(self, resp):
self.printline (3, 'Maks watkow: ' + str(self.maxThreads))
respTree = et.fromstring(resp)
#Autoryzacja
for dts in respTree.findall(".//{http://chomikuj.pl/}AuthResult/{http://chomikuj.pl}status"):
status = dts.text
if status.upper() == "OK":
self.isLogged = True
self.lastLoginTime = time.time()
self.token = respTree.findall(".//{http://chomikuj.pl/}AuthResult/{http://chomikuj.pl}token")[0].text
self.hamsterId = respTree.findall(".//{http://chomikuj.pl/}AuthResult/{http://chomikuj.pl}hamsterId")[0].text
self.printline (1,"Login: OK")
else:
self.isLogged = False
self.printline (1,"Login: " + status)
#Pobieranie urli plikow
accBalance = respTree.find(".//{http://chomikuj.pl/}DownloadResult/{http://chomikuj.pl}accountBalance/{http://chomikuj.pl/}transfer/{http://chomikuj.pl/}extra")
if accBalance is not None:
self.accBalance = accBalance.text
for dts in respTree.findall(".//{http://chomikuj.pl/}DownloadResult/{http://chomikuj.pl}status"):
status = dts.text
if status.upper() == "OK":
dlfiles = respTree.findall(".//{http://chomikuj.pl/}files/{http://chomikuj.pl/}FileEntry")
if (len(dlfiles) > self.totalItems):
self.totalItems = len(dlfiles)
self.printline (2,"Plikow: " + str(self.totalItems))
for dlfile in dlfiles:
url = dlfile.find('{http://chomikuj.pl/}url')
idx = dlfile.find('{http://chomikuj.pl/}id').text
cost = dlfile.find('{http://chomikuj.pl/}cost')
if url.text == None:
agreementInfo = dlfile.find("{http://chomikuj.pl/}agreementInfo/{http://chomikuj.pl/}AgreementInfo/{http://chomikuj.pl/}name").text
costInfo = dlfile.find("{http://chomikuj.pl/}agreementInfo/{http://chomikuj.pl/}AgreementInfo/{http://chomikuj.pl/}cost")
if costInfo.text == None:
cost = 0
else:
cost = costInfo.text
if int(self.accBalance) >= int(cost):
self.dl_step_2(idx, agreementInfo,cost)
else:
self.printline (2,"Blad: brak wystarczajacego limitu transferu")
else:
self.items = self.items +1
it = Item()
it.id = idx
it.directory = self.directory
it.num = self.items
it.url = url.text
it.name = dlfile.find('{http://chomikuj.pl/}name').text
it.daemon = True
self.threads.append(it)
def main(argv):
url = ''
output = ''
username = ''
password = ''
threads = 5
directory = os.getcwd()+"/"
try:
opts, args = getopt.getopt(argv,"h:u:p:i:t:d:o",["help","username","password","ifile","ofile"])
except getopt.GetoptError:
printUsage()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print("Help:")
printUsage()
sys.exit()
elif opt in ("-i", "--ifile"):
url = arg
elif opt in ("-o", "--ofile"):
output = arg
elif opt in ("-u", "--username"):
username = arg
elif opt in ("-p", "--password"):
password = arg
elif opt in ("-t", "--threads"):
threads = arg
elif opt in ("-d", "--directory"):
directory = arg
if len(username) == 0:
username = input("Login: ")
if len(password) == 0:
password = getpass("Haslo: ")
if len(url) == 0:
url = input("URL: ")
if len(password) > 0 and len(username) >0 and len(url)>0:
try:
os.makedirs(directory)
except OSError:
pass
ch = Chomyk(username,password,threads,directory)
ch.dl(str(url))
else:
printUsage()
def printUsage():
print ('chomyk.py --u username --p password --i <url>')
sys.exit(2)
if __name__ == "__main__":
main(sys.argv[1:])