-
Notifications
You must be signed in to change notification settings - Fork 55
/
service.py
187 lines (157 loc) · 7.67 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
# -*- coding: utf-8 -*-
import os
import shutil
import sys
import urllib
import xbmc
import xbmcaddon
import xbmcgui,xbmcplugin
import xbmcvfs
import uuid
__addon__ = xbmcaddon.Addon()
__author__ = __addon__.getAddonInfo('author')
__scriptid__ = __addon__.getAddonInfo('id')
__scriptname__ = __addon__.getAddonInfo('name')
__version__ = __addon__.getAddonInfo('version')
__language__ = __addon__.getLocalizedString
__cwd__ = xbmc.translatePath( __addon__.getAddonInfo('path') ).decode("utf-8")
__profile__ = xbmc.translatePath( __addon__.getAddonInfo('profile') ).decode("utf-8")
__resource__ = xbmc.translatePath( os.path.join( __cwd__, 'resources', 'lib' ) ).decode("utf-8")
__temp__ = xbmc.translatePath( os.path.join( __profile__, 'temp', '') ).decode("utf-8")
if xbmcvfs.exists(__temp__):
shutil.rmtree(__temp__)
xbmcvfs.mkdirs(__temp__)
sys.path.append (__resource__)
from OSUtilities import OSDBServer, log, hashFile, normalizeString
def Search( item ):
search_data = []
try:
search_data = OSDBServer().searchsubtitles(item)
except:
log( __name__, "failed to connect to service for subtitle search")
xbmcgui.Dialog().ok(__scriptname__, __language__(32001),__language__(32005))
# xbmc.executebuiltin((u'Notification(%s,%s,%s,%s/icon.png)' % (__scriptname__ , __language__(32001),"5000",__cwd__)).encode('utf-8'))
return
if search_data != None:
search_data.sort(key=lambda x: [not x['MatchedBy'] == 'moviehash',
not os.path.splitext(x['SubFileName'])[0] == os.path.splitext(os.path.basename(urllib.unquote(xbmc.Player().getPlayingFile().decode('utf-8'))))[0],
not normalizeString(xbmc.getInfoLabel("VideoPlayer.OriginalTitle")).lower() in x['SubFileName'].replace('.',' ').lower(),
not x['LanguageName'] == PreferredSub])
for item_data in search_data:
## hack to work around issue where Brazilian is not found as language in XBMC
if item_data["LanguageName"] == "Brazilian":
item_data["LanguageName"] = "Portuguese (Brazil)"
if ((item['season'] == item_data['SeriesSeason'] and
item['episode'] == item_data['SeriesEpisode']) or
(item['season'] == "" and item['episode'] == "") ## for file search, season and episode == ""
):
listitem = xbmcgui.ListItem(label = item_data["LanguageName"],
label2 = item_data["SubFileName"],
iconImage = str(int(round(float(item_data["SubRating"])/2))),
thumbnailImage = item_data["ISO639"]
)
listitem.setProperty( "sync", ("false", "true")[str(item_data["MatchedBy"]) == "moviehash"] )
listitem.setProperty( "hearing_imp", ("false", "true")[int(item_data["SubHearingImpaired"]) != 0] )
url = "plugin://%s/?action=download&link=%s&ID=%s&filename=%s&format=%s" % (__scriptid__,
item_data["ZipDownloadLink"],
item_data["IDSubtitleFile"],
item_data["SubFileName"],
item_data["SubFormat"]
)
xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=url,listitem=listitem,isFolder=False)
def Download(id,url,format,stack=False):
subtitle_list = []
exts = [".srt", ".sub", ".txt", ".smi", ".ssa", ".ass" ]
if stack: ## we only want XMLRPC download if movie is not in stack,
## you can only retreive multiple subs in zip
result = False
else:
subtitle = os.path.join(__temp__, "%s.%s" %(str(uuid.uuid4()), format))
try:
result = OSDBServer().download(id, subtitle)
except:
log( __name__, "failed to connect to service for subtitle download")
return subtitle_list
if not result:
log( __name__,"Download Using HTTP")
zip = os.path.join( __temp__, "OpenSubtitles.zip")
f = urllib.urlopen(url)
with open(zip, "wb") as subFile:
subFile.write(f.read())
subFile.close()
xbmc.sleep(500)
xbmc.executebuiltin(('XBMC.Extract("%s","%s")' % (zip,__temp__,)).encode('utf-8'), True)
for file in xbmcvfs.listdir(zip)[1]:
file = os.path.join(__temp__, file)
if (os.path.splitext( file )[1] in exts):
subtitle_list.append(file)
else:
subtitle_list.append(subtitle)
if xbmcvfs.exists(subtitle_list[0]):
return subtitle_list
def get_params(string=""):
param=[]
if string == "":
paramstring=sys.argv[2]
else:
paramstring=string
if len(paramstring)>=2:
params=paramstring
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
params = get_params()
if params['action'] == 'search' or params['action'] == 'manualsearch':
log( __name__, "action '%s' called" % params['action'])
item = {}
item['temp'] = False
item['rar'] = False
item['mansearch'] = False
item['year'] = xbmc.getInfoLabel("VideoPlayer.Year") # Year
item['season'] = str(xbmc.getInfoLabel("VideoPlayer.Season")) # Season
item['episode'] = str(xbmc.getInfoLabel("VideoPlayer.Episode")) # Episode
item['tvshow'] = normalizeString(xbmc.getInfoLabel("VideoPlayer.TVshowtitle")) # Show
item['title'] = normalizeString(xbmc.getInfoLabel("VideoPlayer.OriginalTitle"))# try to get original title
item['file_original_path'] = xbmc.Player().getPlayingFile().decode('utf-8') # Full path of a playing file
item['3let_language'] = [] #['scc','eng']
PreferredSub = params.get('preferredlanguage')
if 'searchstring' in params:
item['mansearch'] = True
item['mansearchstr'] = params['searchstring']
for lang in urllib.unquote(params['languages']).decode('utf-8').split(","):
if lang == "Portuguese (Brazil)":
lan = "pob"
elif lang == "Greek":
lan = "ell"
else:
lan = xbmc.convertLanguage(lang,xbmc.ISO_639_2)
item['3let_language'].append(lan)
if item['title'] == "":
log( __name__, "VideoPlayer.OriginalTitle not found")
item['title'] = normalizeString(xbmc.getInfoLabel("VideoPlayer.Title")) # no original title, get just Title
if item['episode'].lower().find("s") > -1: # Check if season is "Special"
item['season'] = "0" #
item['episode'] = item['episode'][-1:]
if ( item['file_original_path'].find("http") > -1 ):
item['temp'] = True
elif ( item['file_original_path'].find("rar://") > -1 ):
item['rar'] = True
item['file_original_path'] = os.path.dirname(item['file_original_path'][6:])
elif ( item['file_original_path'].find("stack://") > -1 ):
stackPath = item['file_original_path'].split(" , ")
item['file_original_path'] = stackPath[0][8:]
Search(item)
elif params['action'] == 'download':
subs = Download(params["ID"], params["link"],params["format"])
for sub in subs:
listitem = xbmcgui.ListItem(label=sub)
xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=sub,listitem=listitem,isFolder=False)
xbmcplugin.endOfDirectory(int(sys.argv[1]))