forked from burekas7/service.subtitles.thewiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.py
266 lines (239 loc) · 9.33 KB
/
service.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
from os import path
from requests import get
from json import loads, load
from shutil import copyfileobj, rmtree
from time import time
from unicodedata import normalize
from urllib import urlretrieve, unquote_plus, unquote, urlopen, quote
from xbmcaddon import Addon
from xbmcplugin import endOfDirectory, addDirectoryItem
from xbmcgui import ListItem, Dialog
from xbmcvfs import listdir, exists, mkdirs
from xbmc import translatePath, executebuiltin, getInfoLabel, executeJSONRPC, Player, sleep, log
from re import sub
import sys
reload(sys)
sys.setdefaultencoding('utf8')
__addon__ = Addon()
__scriptid__ = __addon__.getAddonInfo('id')
__version__ = __addon__.getAddonInfo('version')
__temp__ = unicode(translatePath(__addon__.getAddonInfo('profile')), 'utf-8')
__subs__ = unicode(translatePath(path.join(__temp__, 'subs')), 'utf-8')
def convert_to_utf(file):
try:
with codecs.open(file, "r", "cp1255") as f:
srt_data = f.read()
with codecs.open(file, 'w', 'utf-8') as output:
output.write(srt_data)
except: pass
def normalizeString(str):
return normalize('NFKD', unicode(unicode(str, 'utf-8'))).encode('utf-8', 'ignore')
def download(id):
try:
rmtree(__subs__)
except: pass
mkdirs(__subs__)
subtitle_list = []
exts = [".srt", ".sub"]
archive_file = path.join(__temp__, 'thewiz.sub.'+id+'.zip')
if not path.exists(archive_file):
urlretrieve("http://subs.thewiz.info/zip/"+id+".zip", archive_file)
executebuiltin(('XBMC.Extract("%s","%s")' % (archive_file, __subs__)).encode('utf-8'), True)
for file_ in listdir(__subs__)[1]:
ufile = file_.decode('utf-8')
file_ = path.join(__subs__, ufile)
if path.splitext(ufile)[1] in exts:
convert_to_utf(file_)
subtitle_list.append(file_)
return subtitle_list
def getParams(arg):
param=[]
paramstring=arg
if len(paramstring)>=2:
params=arg
cleanedparams=params.replace('?','')
if (params[len(params)-1]=='/'):
params=params[0:len(params)-2]
pairsofparams=cleanedparams.split('&')
param={}
for i in range(len(pairsofparams)):
splitparams={}
splitparams=pairsofparams[i].split('=')
if (len(splitparams))==2:
param[splitparams[0]]=splitparams[1]
return param
def getParam(name,params):
try:
return unquote_plus(params[name])
except: pass
def GetJson(imdb,season=0,episode=0,version=0):
filename = 'thewiz.imdb.%s.%s.%s.json'%(imdb,season,episode)
url = "http://subs.thewiz.info/search.id.php?imdb=%s&season=%s&episode=%s&version=%s"%(imdb,season,episode,version)
MyLog("GetJson:%s"%url)
json_object = Caching(filename,url)
subs_rate = []
if json_object<>0:
for item_data in json_object:
listitem = ListItem(label= "Hebrew",label2= item_data["versioname"],thumbnailImage="he",iconImage="%s"%(item_data["score"]/2))
if int(item_data["score"])>8:
listitem.setProperty("sync", "true")
url = "plugin://%s/?action=download&versioname=%s&id=%s" % (__scriptid__, item_data["versioname"], item_data["id"])
addDirectoryItem(handle=int(sys.argv[1]),url=url,listitem=listitem,isFolder=False)
def SearchMovie(query,year):
filename = 'thewiz.search.movie.%s.%s.json'%(normalizeString(query),year)
if year>0:
url = "http://api.tmdb.org/3/search/movie?api_key=f7f51775877e0bb6703520952b3c7840&query=%s&year=%s&language=he"%(quote(query),year)
else:
url = "http://api.tmdb.org/3/search/movie?api_key=f7f51775877e0bb6703520952b3c7840&query=%s&language=he"%(quote(query))
json = Caching(filename,url)
try:
tmdb_id = int(json["results"][0]["id"])
except:
return 0
pass
filename = 'thewiz.tmdb.%s.json'%(tmdb_id)
url = "http://api.tmdb.org/3/movie/%s?api_key=f7f51775877e0bb6703520952b3c7840&language=en"%(tmdb_id)
json = Caching(filename,url)
try:
imdb_id = json["imdb_id"]
except:
return 0
pass
return imdb_id
def Caching(filename,url):
json_file = path.join(__temp__, filename)
if not path.exists(json_file) or not path.getsize(json_file)>20 or (time()-path.getmtime(json_file)>30*60):
urlretrieve(url, json_file)
if path.exists(json_file) and path.getsize(json_file)>20:
with open(json_file) as json_data:
json_object = load(json_data)
return json_object
else:
return 0
def ManualSearch(title):
filename = 'thewiz.search.filename.%s.json'%(quote(title))
url = "http://subs.thewiz.info/search.manual.php?filename=%s"%(normalizeString(title))
try:
json = Caching(filename,url)
if json["type"]=="episode":
imdb_id = urlopen("http://subs.thewiz.info/search.tv.php?name="+quote(json['title'])).read()
if imdb_id<>'' and imdb_id<>0:
GetJson(str(imdb_id),json['season'],json['episode'],normalizeString(title))
elif json["type"]=="movie":
if "year" in json:
imdb_id = SearchMovie(str(json['title']),json['year'])
else:
imdb_id = SearchMovie(str(json['title']),0)
if imdb_id:
GetJson(str(imdb_id),0,0,normalizeString(title))
except: pass
def MyLog(msg):
log((u"##**## [%s] %s" % ("TheWiz Subs", msg,)).encode('utf-8'), level=xbmc.LOGDEBUG)
if not exists(__temp__):
mkdirs(__temp__)
action=None
if len(sys.argv) >= 2:
params = getParams(sys.argv[2])
action = getParam("action", params)
MyLog("Version:%s"%__version__)
MyLog("Action:%s"%action)
if action=='search':
item = {}
MyLog("isPlaying:%s"%Player().isPlaying())
if Player().isPlaying():
item['year'] = getInfoLabel("VideoPlayer.Year") # Year
item['season'] = str(getInfoLabel("VideoPlayer.Season")) # Season
if item['season']=='' or item['season']<1:
item['season'] = 0
item['episode'] = str(getInfoLabel("VideoPlayer.Episode")) # Episode
if item['episode']=='' or item['episode']<1:
item['episode'] = 0
if item['episode']==0:
item['title'] = normalizeString(getInfoLabel("VideoPlayer.Title")) # no original title, get just Title
else:
item['title'] = normalizeString(getInfoLabel("VideoPlayer.TVshowtitle")) # Show
if item['title'] == "":
item['title'] = normalizeString(getInfoLabel("VideoPlayer.OriginalTitle")) # try to get original title
item['file_original_path'] = unquote(unicode(Player().getPlayingFile(), 'utf-8')) # Full path of a playing file
item['file_original_path'] = item['file_original_path'].split("?")
item['file_original_path'] = path.basename(item['file_original_path'][0])[:-4]
else: # Take item params from window when kodi is not playing
labelType = xbmc.getInfoLabel("ListItem.DBTYPE") #movie/tvshow/season/episode
labelIMDB = xbmc.getInfoLabel("ListItem.IMDBNumber")
item['year'] = xbmc.getInfoLabel("ListItem.Year")
item['season'] = xbmc.getInfoLabel("ListItem.Season")
item['episode'] = xbmc.getInfoLabel("ListItem.Episode")
item['file_original_path'] = ""
if labelType == 'movie':
item['title'] = xbmc.getInfoLabel("ListItem.OriginalTitle")
elif labelType == 'episode':
item['title'] = xbmc.getInfoLabel("ListItem.TVShowTitle")
else:
item['title'] = "SearchFor..." # In order to show "No Subtitles Found" result.
MyLog("item:%s"%item)
imdb_id = 0
try:
if Player().isPlaying(): # Enable using subtitles search dialog when kodi is not playing
playerid_query = '{"jsonrpc": "2.0", "method": "Player.GetActivePlayers", "id": 1}'
playerid = loads(executeJSONRPC(playerid_query))['result'][0]['playerid']
imdb_id_query = '{"jsonrpc": "2.0", "method": "Player.GetItem", "params": {"playerid": ' + str(playerid) + ', "properties": ["imdbnumber"]}, "id": 1}'
imdb_id = loads(executeJSONRPC (imdb_id_query))['result']['item']['imdbnumber']
MyLog("imdb JSONPC:%s"%imdb_id)
else:
if labelIMDB:
imdb_id = labelIMDB
else:
if labelType == 'movie':
imdb_id = "ThisIsMovie" #Search the movie by item['title'] for imdb_id
elif labelType == 'episode':
imdb_id = "ThisIsEpisode" #Search by item['title'] for tvdb_id
else:
imdb_id = "tt0" # In order to show "No Subtitles Found" result => Doesn't recognize movie/episode
except: pass
if imdb_id[:2]=="tt": #Simple IMDB_ID
GetJson(imdb_id,item['season'],item['episode'],item['file_original_path'])
else:
# Search TV Show by Title
if item['season'] or item['episode']:
try:
imdb_id = urlopen("http://subs.thewiz.info/search.tv.php?name="+quote(item['title'])).read()
MyLog("Search TV IMDB:%s [%s]"%(imdb_id,item['title']))
if imdb_id<>'' and imdb_id<>0:
GetJson(str(imdb_id),item['season'],item['episode'],item['file_original_path'])
except: pass
# Search Movie by Title+Year
else:
try:
imdb_id = SearchMovie(query=item['title'],year=item['year'])
MyLog("Search IMDB:%s"%imdb_id)
if not imdb_id[:2]=="tt":
imdb_id = SearchMovie(query=item['title'],year=(int(item['year'])-1))
MyLog("Search IMDB(2):%s"%imdb_id)
if imdb_id[:2]=="tt":
GetJson(imdb_id,0,0,item['file_original_path'])
except: pass
# Search Local File
if not imdb_id:
ManualSearch(item['title'])
endOfDirectory(int(sys.argv[1]))
if __addon__.getSetting("Debug") == "true":
if imdb_id[:2]=="tt":
Dialog().ok("Debug "+__version__,str(item),"imdb: "+str(imdb_id))
else:
Dialog().ok("Debug "+__version__,str(item),"NO IDS")
elif action == 'manualsearch':
searchstring = getParam("searchstring", params)
ManualSearch(searchstring)
endOfDirectory(int(sys.argv[1]))
elif action == 'download':
id = getParam("id", params)
MyLog("Download ID:%s"%id)
subs = download(id)
for sub in subs:
listitem = ListItem(label=sub)
addDirectoryItem(handle=int(sys.argv[1]), url=sub, listitem=listitem,isFolder=False)
endOfDirectory(int(sys.argv[1]))
elif action=='clean':
try:
rmtree(__temp__)
except: pass