[1efa53]: script.xbmc.subtitles / resources / lib / gui.py Maximize Restore History

Download this file

gui.py    416 lines (311 with data), 16.5 kB

  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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import sys
import os
import xbmc
import xbmcgui
from utilities import *
import urllib
import unzip
import unicodedata
from os.path import join
import shutil
import glob
_ = sys.modules[ "__main__" ].__language__
__scriptname__ = sys.modules[ "__main__" ].__scriptname__
__version__ = sys.modules[ "__main__" ].__version__
__settings__ = sys.modules[ "__main__" ].__settings__
STATUS_LABEL = 100
LOADING_IMAGE = 110
SUBTITLES_LIST = 120
SERVICE_DIR = os.path.join(os.getcwd(), "resources", "lib", "services")
class GUI( xbmcgui.WindowXMLDialog ):
def __init__( self, *args, **kwargs ):
pass
def set_allparam(self):
temp = False
rar = False
movieFullPath = urllib.unquote(xbmc.Player().getPlayingFile())
path = __settings__.getSetting( "subfolder" ) == "true" # True for movie folder
sub_folder = xbmc.translatePath(__settings__.getSetting( "subfolderpath" ))
if (movieFullPath.find("http://") > -1 ):
temp = True
if (movieFullPath.find("rar://") > -1 ) and path:
rar = True
movieFullPath = sub_folder = movieFullPath.replace("rar://","")
sub_folder = os.path.dirname(os.path.dirname( sub_folder ))
if not path and not rar:
if len(sub_folder) < 1 :
sub_folder = os.path.dirname( movieFullPath )
if path and not rar:
if sub_folder.find("smb://") > -1:
if temp:
dialog = xbmcgui.Dialog()
sub_folder = dialog.browse( 0, "Choose Subtitle folder", "files")
else:
sub_folder = os.path.dirname( movieFullPath )
else:
sub_folder = os.path.dirname( movieFullPath )
self.list = []
self.debug = ( __settings__.getSetting( "debug" ) == "true" ) # debug?
self.year = xbmc.getInfoLabel("VideoPlayer.Year") # Year
self.season = str(xbmc.getInfoLabel("VideoPlayer.Season")) # Season
self.episode = str(xbmc.getInfoLabel("VideoPlayer.Episode"))# Episode
if self.episode.lower().find("s") > -1: # Check if season is "Special"
self.season = "0" #
self.episode.replace("s","") #
self.episode.replace("S","") #
self.tvshow = xbmc.getInfoLabel("VideoPlayer.TVshowtitle") # Show
self.title = unicodedata.normalize('NFKD',
unicode(unicode(xbmc.getInfoLabel
("VideoPlayer.Title"), 'utf-8'))
).encode('ascii','ignore') # Title
if self.tvshow == "":
if str(self.year) == "":
title, season, episode = regex_tvshow(False, self.title)
if episode != "":
self.season = str(int(season))
self.episode = str(int(episode))
self.tvshow = title
else:
self.title, self.year = xbmc.getCleanMovieTitle( self.title )
else:
self.title = self.title
else:
self.year = ""
self.language_1 = toScriptLang(__settings__.getSetting( "Language1" )) # Full language 1
self.language_2 = toScriptLang(__settings__.getSetting( "Language2" )) # Full language 2
self.language_3 = toScriptLang(__settings__.getSetting( "Language3" )) # Full language 2
self.sub_folder = sub_folder # Subtitle download folder
self.file_original_path = urllib.unquote ( movieFullPath ) # Movie Path
self.set_temp = temp
self.mansearch = __settings__.getSetting( "searchstr" ) == "true" # Manual search string??
self.rar = rar # rar archive?
if (__settings__.getSetting( "fil_name" ) == "true"): # Display Movie name or search string
self.file_name = os.path.basename( movieFullPath )
else:
if (len(str(self.year)) < 1 ) :
print self.year
self.file_name = self.title
if (len(self.tvshow) > 0):
self.file_name = "%s S%.2dE%.2d" % (self.tvshow, int(self.season), int(self.episode) )
else:
self.file_name = "%s (%s)" % (self.title, str(self.year),)
if (__settings__.getSetting( "par_folder" ) == "true"):
self.man_search_label = _( 747 )
self.parent_folder_search = True
else:
self.man_search_label = _( 612 )
self.parent_folder_search = False
self.tmp_sub_dir = xbmc.translatePath("special://temp/sub_tmp")
if not self.tmp_sub_dir.endswith(':') and not os.path.exists(self.tmp_sub_dir):
os.mkdir(self.tmp_sub_dir)
else:
self.rem_files(self.tmp_sub_dir)
if not glob.glob ("%s.*.*" %(os.path.join(self.sub_folder,os.path.splitext(os.path.basename( movieFullPath ))[0],))):
self.getControl( 111 ).setVisible( False )
#### ---------------------------- Set Service ----------------------------###
def_service = __settings__.getSetting( "defservice")
service_list = []
standard_service_list = ['OpenSubtitles', 'Podnapisi', 'Sublight']
service = ""
for name in os.listdir(SERVICE_DIR):
if not (name.startswith('.')) and not (name.startswith('_')):
service_list.append(name)
for serv in standard_service_list:
if not __settings__.getSetting( serv ) == "true" :
service_list.remove( serv )
else:
service = serv
if service_list.count(def_service) > 0:
service = def_service
if len(service_list) > 0:
if len(service) < 1:
self.service = service_list[0]
else:
self.service = service
self.service_list = service_list
if self.debug : ## Debug?
LOG( LOG_INFO, "%s" ,"""
Manual Search : [%s]
Default Service : [%s]
Services : %s
Temp?: [%s]
File Path: [%s]
Year: [%s]
Tv Show Title: [%s]
Tv Show Season: [%s]
Tv Show Episode: [%s]
Movie/Episode Title: [%s]
Subtitle Folder: [%s]
Languages: [%s] [%s] [%s]
Parent Folder Search: [%s]"""
% (self.mansearch, self.service, service_list,
self.set_temp, self.file_original_path, str(self.year), self.tvshow, self.season, self.episode,
self.title, self.sub_folder, self.language_1, self.language_2, self.language_3,self.parent_folder_search ) )
self.controlId = -1
self.shufle = 0
self.subtitles_list = []
self.Search_Subtitles()
else:
self.getControl( STATUS_LABEL ).setLabel( "No Services Have been selected" )
xbmc.sleep(2000)
self.exit_script()
#### ---------------------------- On Init ----------------------------###
def onInit( self ):
self.set_allparam()
###-------------------------- Search Subtitles -------------################
def Search_Subtitles( self ):
self.getControl( SUBTITLES_LIST ).reset()
self.getControl( LOADING_IMAGE ).setImage( xbmc.translatePath( os.path.join( SERVICE_DIR, self.service, "logo.png") ) )
self.getControl( STATUS_LABEL ).setLabel( _( 635 ) )
exec ( "from services.%s import service as Service" % (self.service))
self.Service = Service
self.getControl( STATUS_LABEL ).setLabel( _( 646 ) )
self.subtitles_list, self.session_id = self.Service.search_subtitles( self.file_original_path, self.title, self.tvshow, self.year, self.season, self.episode, self.debug, self.set_temp, self.rar, self.language_1, self.language_2, self.language_3 )
self.getControl( STATUS_LABEL ).setLabel( _( 642 ) % ( "...", ) )
if not self.subtitles_list:
self.getControl( STATUS_LABEL ).setLabel( "No Subtitles Found!" )
self.list_services()
else:
self.list_services()
subscounter = 0
for item in self.subtitles_list:
listitem = xbmcgui.ListItem( label=item["language_name"], label2=item["filename"], iconImage=item["rating"], thumbnailImage=item["language_flag"] )
if item["sync"]:
listitem.setProperty( "sync", "true" )
else:
listitem.setProperty( "sync", "false" )
self.list.append(subscounter)
subscounter = subscounter + 1
self.getControl( SUBTITLES_LIST ).addItem( listitem )
self.getControl( STATUS_LABEL ).setLabel( '%i %s '"' %s '"'' % (len ( self.subtitles_list ), _( 744 ), self.file_name,) )
self.setFocus( self.getControl( SUBTITLES_LIST ) )
self.getControl( SUBTITLES_LIST ).selectItem( 0 )
###-------------------------- Download Subtitles -------------################
def Download_Subtitles( self, pos ):
self.getControl( STATUS_LABEL ).setLabel( _( 649 ) )
zip_subs = "special://temp/sub_tmp/zipsubs.zip"
zipped, language, file = self.Service.download_subtitles(self.subtitles_list, pos, zip_subs, self.tmp_sub_dir, self.sub_folder,self.session_id)
sub_lang = str(toOpenSubtitles_two(language))
if zipped :
self.Extract_Subtitles(zip_subs,sub_lang)
else:
sub_ext = os.path.splitext( file )[1]
sub_name = os.path.splitext( os.path.basename( self.file_original_path ) )[0]
file_name = "%s.%s%s" % ( sub_name, sub_lang, sub_ext )
file_path = os.path.join(self.sub_folder, file_name)
shutil.copy(file, file_path)
xbmc.Player().setSubtitles(file_path)
self.rem_files(self.tmp_sub_dir)
self.exit_script()
###-------------------------- Extract, Rename & Activate Subtitles -------------################
def Extract_Subtitles( self, zip_subs, subtitle_lang ):
un = unzip.unzip()
files = un.get_file_list( zip_subs )
sub_filename = os.path.basename( self.file_original_path )
if len(files) < 1 :
self.getControl( STATUS_LABEL ).setLabel( _( 654 ) )
self.list_services()
else :
self.getControl( STATUS_LABEL ).setLabel( _( 652 ) )
un.extract( zip_subs, self.tmp_sub_dir )
subtitle_set = False
for zip_entry in files:
sub_ext = os.path.splitext( zip_entry )[1]
sub_name = os.path.splitext( sub_filename )[0]
file_name = "%s.%s%s" % ( sub_name, subtitle_lang, sub_ext )
file_path = os.path.join(self.sub_folder, file_name)
subtitle_file = os.path.join(self.tmp_sub_dir, zip_entry)
try:
shutil.copy(subtitle_file, file_path)
subtitle_set = True
except :
import filecmp
try:
if filecmp.cmp(subtitle_file, file_path):
subtitle_set = True
except:
dialog = xbmcgui.Dialog()
selected = dialog.yesno( __scriptname__ , _( 748 ), _( 749 ), _( 750 ) )
if selected == 1:
__settings__.openSettings()
break
self.rem_files((xbmc.translatePath(self.tmp_sub_dir)))
if subtitle_set :
xbmc.Player().setSubtitles(file_path)
self.exit_script()
else:
self.getControl( STATUS_LABEL ).setLabel( _( 654 ) )
self.list_services()
###-------------------------- Reset Sub List -------------################
def list_services( self ):
service_list = self.service_list
if self.shufle == 0:
service_list.reverse()
self.shufle = 1
else:
self.shufle = 0
self.getControl( SUBTITLES_LIST ).reset()
label = ""
self.list = []
for serv in service_list:
if serv != self.service:
label2 = "[COLOR=FFFF0000]%s%s[/COLOR]" %(_( 610 ), serv,)
listitem = xbmcgui.ListItem( label,label2 )
self.list.append(serv)
self.getControl( SUBTITLES_LIST ).addItem( listitem )
if self.mansearch :
label2 = "[COLOR=FF00FF00]%s[/COLOR]" % ( self.man_search_label )
listitem = xbmcgui.ListItem( label,label2 )
self.list.append("Man")
self.getControl( SUBTITLES_LIST ).addItem( listitem )
###-------------------------- Manual search Keyboard -------------################
def keyboard(self):
dir, self.year = xbmc.getCleanMovieTitle(os.path.split(os.path.split(self.file_original_path)[0])[1])
if self.rar:
tmp_dir = os.path.split(os.path.split(os.path.split(self.file_original_path)[0])[0])[1]
dir, self.year = xbmc.getCleanMovieTitle( tmp_dir )
if not self.parent_folder_search:
kb = xbmc.Keyboard("%s ()" % (dir,), _( 751 ), False)
text = self.file_name
kb.doModal()
if (kb.isConfirmed()): text, self.year = xbmc.getCleanMovieTitle(kb.getText())
self.title = text
else:
self.title = dir
if self.debug : LOG( LOG_INFO, "Manual/Keyboard Entry: Title:[%s], Year: [%s]" , self.title, self.year )
if self.year != "" :
self.file_name = "%s (%s)" % (self.file_name, str(self.year),)
else:
self.file_name = self.title
self.tvshow = ""
self.Search_Subtitles()
###-------------------------- Exit script -------------################
def exit_script( self, restart=False ):
self.close()
###-------------------------- Click -------------################
def onClick( self, controlId ):
selection = str(self.list[self.getControl( SUBTITLES_LIST ).getSelectedPosition()])
if self.debug : LOG( LOG_INFO, "In 'On click' selected : [%s]" , ( selection ) )
if selection.isdigit():
if self.debug : LOG( LOG_INFO, "Selected : [%s]" , ( selection ) )
self.Download_Subtitles( int(selection) )
else:
if selection == "Man":
self.keyboard()
else:
self.service = selection
self.Search_Subtitles()
###-------------------------- Remove temp files -------------################
def rem_files( self, directory):
for root, dirs, files in os.walk(directory, topdown=False):
for items in dirs:
shutil.rmtree(join(root, items), ignore_errors=True, onerror=None)
for name in files:
os.remove(join(root, name))
###-------------------------- On Focus -------------################
def onFocus( self, controlId ):
self.controlId = controlId
###-------------------------- "Esc" , "Back" button -------------################
def onAction( self, action ):
if ( action.getButtonCode() in CANCEL_DIALOG ):
self.exit_script()