-
Notifications
You must be signed in to change notification settings - Fork 0
/
super.py
113 lines (100 loc) · 4.16 KB
/
super.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
import os
from PyQt4 import QtCore, QtGui
import urllib2
import multiprocessing as mp
from main import dziecko, del_and_combine, fileLogSave
from test2 import setTableParts, setTableDwn
def download1(url, file_size, data_block, N, dir_tmp):
p = mp.Pool(N)
for i in range(0, N):
# algorytm podzialu dla sciagania. W przypadku ostatniego watku musimy dopelnic zakres pobierania do wielkosci pliku
start = i * data_block
stop = 0
if not i == N - 1:
stop = i * data_block + data_block - 1
else:
stop = file_size
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0",
"Accept-Encoding": "gzip, deflate, sdch",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Language": "pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4",
"Connection": "keep-alive",
"Range": "bytes=" + str(start) + "-" + str(stop)
}
# ustalenie obiektu Request dla konkretnego procesu
req = urllib2.Request(url, headers=headers)
p.apply_async(dziecko, [i, req, dir_tmp])
return p
def download2(url,dir_tmp):
N=1
p = mp.Pool(N)
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0",
"Accept-Encoding": "gzip, deflate, sdch",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Language": "pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4",
"Connection": "keep-alive",
}
req = urllib2.Request(url,headers=headers)
p.apply_async(dziecko, [0,req,dir_tmp])
return p
def supervisor1(*args):
N = args[0]
url = args[1]
dir = args[2]
ww = args[3]
tableDwn = args[4]
f_name = url.split("/")[len(url.split("/")) - 1]
dir_tmp = dir + "\\TMP." + f_name
try:
os.mkdir(dir_tmp)
except:
pass
data = urllib2.urlopen(url)
if data.headers["Accept-Ranges"] == "bytes":
if data.headers["Content-Length"]:
for n in range(0, N):
with open(dir_tmp + "\\file" + str(n), "w+b") as f:
pass
setTableParts(ww.tableParts, N)
file_size = round(float((data.headers["Content-Length"].strip()))) # odnajdujemy w naglowku linijke "Content-Length", a nastepnie zapisujemy informacje o wielkosci pliku
data_block = float(file_size / N)
p = download1(url, int(file_size), int(data_block), N, dir_tmp)
else:
N=1
for n in range(0, N):
with open(dir_tmp + "\\file" + str(n), "w+b") as f:
pass
setTableParts(ww.tableParts, N)
p = download2(url, dir_tmp)
else:
N=1
for n in range(0, N):
with open(dir_tmp + "\\file" + str(n), "w+b") as f:
pass
setTableParts(ww.tableParts, N)
p = download2(url, dir_tmp)
while True:
QtCore.QCoreApplication.processEvents()
sum = 0
for n in range(0, N):
sum = sum + os.path.getsize(dir_tmp + "\\file" + str(n))
if not sum < file_size:
p.close()
p.join()
ww.end.setEnabled(True)
for n in range(0,N):
s = float(os.path.getsize(dir_tmp + "\\file" + str(n)))
item = QtGui.QTableWidgetItem("%.2f/%.2f (100.00%%)" % (data_block/(1024**2), data_block/(1024**2)))
item.setFlags(QtCore.Qt.ItemIsSelectable|QtCore.Qt.ItemIsDragEnabled|QtCore.Qt.ItemIsEnabled)
ww.tableParts.setItem(n,0,item)
del_and_combine(dir, dir_tmp, f_name, N)
fileLogSave(f_name, url)
setTableDwn(tableDwn)
break
for n in range(0,N):
s = float(os.path.getsize(dir_tmp + "\\file" + str(n)))
item = QtGui.QTableWidgetItem("%.2f/%.2f (%2.2f%%)" % (s/(1024**2), float(data_block/(1024**2)), ((s*100)/data_block)))
item.setFlags(QtCore.Qt.ItemIsSelectable|QtCore.Qt.ItemIsDragEnabled|QtCore.Qt.ItemIsEnabled)
ww.tableParts.setItem(n,0,item)