-
Notifications
You must be signed in to change notification settings - Fork 10
/
utils.py
156 lines (124 loc) · 5.23 KB
/
utils.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
import sys
import struct
import urllib2
import urlparse
import cookielib
from os import path
from httplib import HTTPConnection
import xbmc
import xbmcvfs
def log(module, msg):
xbmc.log((u"### [%s] - %s" % (module, msg)).encode('utf-8'), level=xbmc.LOGDEBUG)
def notify(script_name, language, string_id):
xbmc.executebuiltin((u'Notification(%s,%s)' % (script_name, language(string_id))).encode('utf-8'))
def get_params(params_str=""):
params_str = params_str or sys.argv[2]
return dict(urlparse.parse_qsl(params_str.lstrip('?')))
def get_video_path(xbmc_path=''):
xbmc_path = xbmc_path or urlparse.unquote(xbmc.Player().getPlayingFile().decode('utf-8'))
if xbmc_path.startswith('rar://'):
return path.dirname(xbmc_path.replace('rar://', ''))
elif xbmc_path.startswith('stack://'):
return xbmc_path.split(" , ")[0].replace('stack://', '')
return xbmc_path
def get_languages_dict(languages_param):
langs = {}
for lang in languages_param.split(','):
if lang == "Portuguese (Brazil)":
langs["pob"] = lang
elif lang == "Greek":
langs["ell"] = lang
else:
langs[xbmc.convertLanguage(lang, xbmc.ISO_639_2)] = lang
return langs
class HTTP10Connection(HTTPConnection):
_http_vsn = 10
_http_vsn_str = "HTTP/1.0"
class HTTP10Handler(urllib2.HTTPHandler):
def http_open(self, req):
return self.do_open(HTTP10Connection, req)
def get_session(proxies=None, cookies=True, http_10=False):
handlers = []
if proxies:
handlers.append(urllib2.ProxyHandler(proxies))
if cookies:
cj = cookielib.CookieJar()
handlers.append(urllib2.HTTPCookieProcessor(cj))
if http_10:
handlers.append(HTTP10Handler)
return urllib2.build_opener(*handlers)
def __get_last_split(firs_rar_file, x):
if firs_rar_file[-3:] == '001':
return firs_rar_file[:-3] + ('%03d' % (x + 1))
if firs_rar_file[-11:-6] == '.part':
return firs_rar_file[0:-6] + ('%02d' % (x + 1)) + firs_rar_file[-4:]
if firs_rar_file[-10:-5] == '.part':
return firs_rar_file[0:-5] + ('%1d' % (x + 1)) + firs_rar_file[-4:]
return firs_rar_file[0:-2] + ('%02d' % (x - 1))
def __add_file_hash(name, file_hash, seek):
f = xbmcvfs.File(name)
f.seek(max(0, seek), 0)
for i in range(8192):
file_hash += struct.unpack('<q', f.read(8))[0]
file_hash &= 0xffffffffffffffff
f.close()
return file_hash
def __movie_size_and_hash_rar(firs_rar_file):
log('utils.movie_size_and_hash', 'Hashing Rar file...')
f = xbmcvfs.File(firs_rar_file)
a = f.read(4)
if a != 'Rar!':
log('utils.movie_size_and_hash', 'ERROR: This is not rar file (%s).' % path.basename(firs_rar_file))
raise Exception('ERROR: This is not rar file.')
seek = 0
for i in range(4):
f.seek(max(0, seek), 0)
a = f.read(100)
tipe, flag, size = struct.unpack('<BHH', a[2:2 + 5])
if 0x74 == tipe:
if 0x30 != struct.unpack('<B', a[25:25 + 1])[0]:
log('utils.movie_size_and_hash', 'Bad compression method! Work only for "store".')
raise Exception('Bad compression method! Work only for "store".')
s_partiize_body_start = seek + size
s_partiize_body, s_unpack_size = struct.unpack('<II', a[7:7 + 2 * 4])
if flag & 0x0100:
s_unpack_size += (struct.unpack('<I', a[36:36 + 4])[0] << 32)
log('utils.movie_size_and_hash',
'WARNING: Hash untested for files biger that 2gb. May work or may generate bad hash.')
last_rar_file = __get_last_split(firs_rar_file, (s_unpack_size - 1) / s_partiize_body)
file_hash = __add_file_hash(firs_rar_file, s_unpack_size, s_partiize_body_start)
file_hash = __add_file_hash(
last_rar_file, file_hash, (s_unpack_size % s_partiize_body) + s_partiize_body_start - 65536
)
f.close()
return s_unpack_size, "%016x" % file_hash
seek += size
log('utils.movie_size_and_hash', 'ERROR: Not Body part in rar file.')
raise Exception('ERROR: Not Body part in rar file.')
def movie_size_and_hash(file_path):
file_ext = path.splitext(file_path)[1]
if file_ext == '.rar' or file_ext == '.001':
return __movie_size_and_hash_rar(file_path)
longlong_format = '<q' # little-endian long long
byte_size = struct.calcsize(longlong_format)
f = xbmcvfs.File(file_path)
file_size = f.size()
movie_hash = file_size
if file_size < 65536 * 2:
f.close()
log('utils.movie_size_and_hash', "ERROR: SizeError (%d)." % file_size)
raise Exception("SizeError")
for x in range(65536 / byte_size):
buff = f.read(byte_size)
(l_value,) = struct.unpack(longlong_format, buff)
movie_hash += l_value
movie_hash &= 0xFFFFFFFFFFFFFFFF # to remain as 64bit number
f.seek(max(0, file_size - 65536), 0)
for x in range(65536 / byte_size):
buff = f.read(byte_size)
(l_value,) = struct.unpack(longlong_format, buff)
movie_hash += l_value
movie_hash &= 0xFFFFFFFFFFFFFFFF
returned_movie_hash = "%016x" % movie_hash
f.close()
return file_size, returned_movie_hash