-
Notifications
You must be signed in to change notification settings - Fork 0
/
subtitle.py
160 lines (133 loc) · 5.02 KB
/
subtitle.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
from xmlrpclib import ServerProxy
import base64, zlib, struct
import os
from queue import Queue
class Settings(object):
OPENSUBTITLES_SERVER = 'http://api.opensubtitles.org/xml-rpc'
USER_AGENT = 'OSTestUserAgentTemp'
LANGUAGE = 'en'
class File(object):
def __init__(self, path):
self.path = path
self.size = str(os.path.getsize(path))
def get_hash(self):
'''Original from: http://goo.gl/qqfM0
'''
longlongformat = 'q' # long long
bytesize = struct.calcsize(longlongformat)
try:
f = open(self.path, "rb")
except(IOError):
return "IOError"
hash = int(self.size)
if int(self.size) < 65536 * 2:
return "SizeError"
for x in range(65536 // bytesize):
buffer = f.read(bytesize)
(l_value, ) = struct.unpack(longlongformat, buffer)
hash += l_value
hash = hash & 0xFFFFFFFFFFFFFFFF # to remain as 64bit number
f.seek(max(0, int(self.size) - 65536), 0)
for x in range(65536 // bytesize):
buffer = f.read(bytesize)
(l_value, ) = struct.unpack(longlongformat, buffer)
hash += l_value
hash = hash & 0xFFFFFFFFFFFFFFFF
f.close()
returnedhash = "%016x" % hash
return str(returnedhash)
class OpenSubtitles(object):
'''OpenSubtitles API wrapper.
Please check the official API documentation at:
http://trac.opensubtitles.org/projects/opensubtitles/wiki/XMLRPC
'''
def __init__(self, language=None):
self.xmlrpc = ServerProxy(Settings.OPENSUBTITLES_SERVER,
allow_none=True)
self.language = language or Settings.LANGUAGE
self.token = None
def _get_from_data_or_none(self, key):
'''Return the key getted from data if the status is 200,
otherwise return None.
'''
status = self.data.get('status').split()[0]
return self.data.get(key) if '200' == status else None
def login(self, username='', password=''):
'''Returns token is login is ok, otherwise None.
'''
self.data = self.xmlrpc.LogIn(username, password,
self.language, Settings.USER_AGENT)
token = self._get_from_data_or_none('token')
if token:
self.token = token
return token
def logout(self):
'''Returns True is logout is ok, otherwise None.
'''
data = self.xmlrpc.LogOut(self.token)
return '200' in data.get('status')
def search_subtitles(self, params, pathfile):
'''Returns a list with the subtitles info.
'''
self.data = self.xmlrpc.SearchSubtitles(self.token, params)
resp = self._get_from_data_or_none('data')
if resp == None:
return False
subtitles = []
for result in resp:
if int(result['SubBad']) != 1:
subtitles.append({'subid': result['IDSubtitleFile'],
'hash': result['MovieHash']})
if len(subtitles)==0:
return False
sub = self.download_subtitles(subtitles[0])
if not sub:
return False
filename = pathfile + ".srt"
file = open(filename, "wb")
file.write(sub)
file.close()
return True
def download_subtitles(self, subtitle):
resp = self.xmlrpc.DownloadSubtitles(self.token, [subtitle['subid']])
if resp['status'].upper() != '200 OK':
return False
decoded = base64.standard_b64decode(resp['data'][0]['data'].encode('ascii'))
decompressed = zlib.decompress(decoded, 15 + 32)
return decompressed
def download_subtitle(self, name, path):
try:
fullpath = os.path.join(path, name)
f = File(fullpath)
hash = f.get_hash()
size = f.size
out = self.search_subtitles([{'sublanguageid': 'eng', 'moviehash': hash, 'moviebytesize': size}],\
os.path.splitext(fullpath)[0])
return out
except Exception as e:
print str(e)
return False
class Subtitle:
def __init__(self, name, path, q):
self.path = path
self.name = name
self.opsub = OpenSubtitles()
try:
self.opsub.login()
q.put(('True',))
except:
print "Not connected to Internet."
q.put(('False', "Not connected to Internet."))
def download(self, q):
try:
val = self.opsub.download_subtitle(self.name, self.path)
self.opsub.logout()
except:
print "Unable to connect to Internet."
val = False
q.put(val)
if __name__ == '__main__':
path = "C:\Users\Tushar\Downloads\python-opensubtitles-master\python-opensubtitles-master\pythonopensubtitles"
name = "Deadpool.2016.720p.BluRay.x264.YIFY[God.Of.Atheists].mp4"
sub = Subtitle(name, path)
print "Downloading Subtitle", sub.download()