def download_subtitles_sub(self, pos): subtitle_id = self.subtitles[pos]["subtitleID"] title = self.subtitles[pos]["title"] year = SublightUtils.toInteger(self.subtitles[pos]["year"]) release = self.subtitles[pos]["release"] language = self.subtitles[pos]["language"] mediaType = self.subtitles[pos]["mediaType"] numberOfDiscs = SublightUtils.toInteger( self.subtitles[pos]["numberOfDiscs"]) self.getControl(STATUS_LABEL).setLabel(_(649)) sublightWebService = SublightUtils.SublightWebService() ticket_id, download_wait = sublightWebService.GetDownloadTicket( self.session_id, subtitle_id) if ticket_id != "": if download_wait > 0: time.sleep(float(download_wait)) subtitle_b64_data = sublightWebService.DownloadByID( self.session_id, subtitle_id, ticket_id) base64_file_path = os.path.join(self.sub_folder, "subtitle.b64") base64_file = open(base64_file_path, "wb") base64_file.write(subtitle_b64_data) base64_file.close() base64_file = open(base64_file_path, "r") zip_file_path = os.path.join(self.sub_folder, "subtitle.zip") zip_file = open(zip_file_path, "wb") base64.decode(base64_file, zip_file) base64_file.close() zip_file.close() filename = "" subtitle_format = "srt" local_path = self.sub_folder zip_filename = zip_file_path sub_filename = os.path.basename(self.file_path) form = "srt" lang = str(toOpenSubtitles_two(language)) subtitle_lang = lang subName1 = sub_filename[0:sub_filename.rfind(".")] if subName1 == "": subName1 = self.search_string.replace("+", " ") movie_files = [] number_of_discs = int(numberOfDiscs) if number_of_discs == 1: movie_files.append(sub_filename) elif number_of_discs > 1 and not self.set_temp: regexp = movie_file regexp = regexp.replace("\\", "\\\\") regexp = regexp.replace("^", "\^") regexp = regexp.replace("$", "\$") regexp = regexp.replace("+", "\+") regexp = regexp.replace("*", "\*") regexp = regexp.replace("?", "\?") regexp = regexp.replace(".", "\.") regexp = regexp.replace("|", "\|") regexp = regexp.replace("(", "\(") regexp = regexp.replace(")", "\)") regexp = regexp.replace("{", "\{") regexp = regexp.replace("}", "\}") regexp = regexp.replace("[", "\[") regexp = regexp.replace("]", "\]") regexp = re.sub("\d+", "\\d+", regexp) regex = re.compile(regexp, re.IGNORECASE) movie_dir = os.path.dirname(self.file_path) movie_file = os.path.basename(self.file_path) files = os.listdir(movie_dir) for file in files: if regex.match(self.file_path) != None: movie_files.append(self.file_path) movie_files.sort() if not zipfile.is_zipfile(zip_file_path): self.getControl(STATUS_LABEL).setLabel(_(654)) label2 = "[COLOR=FF00FF00]%s[/COLOR]" % (_(612)) listitem = xbmcgui.ListItem(label, label2) self.getControl(SUBTITLES_LIST).addItem(listitem) else: self.getControl(STATUS_LABEL).setLabel(_(652)) zip = zipfile.ZipFile(zip_file_path, "r") i = 0 for zip_entry in zip.namelist(): file_name = zip_entry i = i + 1 if i <= len(movie_files): sub_ext = os.path.splitext(file_name)[1] sub_name = os.path.splitext(movie_files[i - 1])[0] if self.set_temp: sub_name = self.search_string.replace("+", " ") file_name = "%s.%s%s" % (sub_name, subtitle_lang, sub_ext) file_path = os.path.join(self.sub_folder, file_name) ##LOG( LOG_INFO, "File Path "+ file_name ) outfile = open(file_path, "wb") outfile.write(zip.read(zip_entry)) outfile.close() zip.close() xbmc.Player().setSubtitles( xbmc.translatePath( os.path.join(os.getcwd(), 'resources', 'lib', 'dummy.srt'))) time.sleep(2) xbmc.Player().setSubtitles(file_path) os.remove(base64_file_path) os.remove(zip_file_path) self.exit_script()
def download_subtitles_sub(self, pos): subtitle_id = self.subtitles[pos]["subtitleID"] title = self.subtitles[pos]["title"] year = SublightUtils.toInteger(self.subtitles[pos]["year"]) release = self.subtitles[pos]["release"] language = self.subtitles[pos]["language"] mediaType = self.subtitles[pos]["mediaType"] numberOfDiscs = SublightUtils.toInteger(self.subtitles[pos]["numberOfDiscs"]) self.getControl(STATUS_LABEL).setLabel(_(649)) sublightWebService = SublightUtils.SublightWebService() ticket_id, download_wait = sublightWebService.GetDownloadTicket(self.session_id, subtitle_id) if ticket_id != "": if download_wait > 0: time.sleep(float(download_wait)) subtitle_b64_data = sublightWebService.DownloadByID(self.session_id, subtitle_id, ticket_id) base64_file_path = os.path.join(self.sub_folder, "subtitle.b64") base64_file = open(base64_file_path, "wb") base64_file.write(subtitle_b64_data) base64_file.close() base64_file = open(base64_file_path, "r") zip_file_path = os.path.join(self.sub_folder, "subtitle.zip") zip_file = open(zip_file_path, "wb") base64.decode(base64_file, zip_file) base64_file.close() zip_file.close() filename = "" subtitle_format = "srt" local_path = self.sub_folder zip_filename = zip_file_path sub_filename = os.path.basename(self.file_path) form = "srt" lang = str(toOpenSubtitles_two(language)) subtitle_lang = lang subName1 = sub_filename[0 : sub_filename.rfind(".")] if subName1 == "": subName1 = self.search_string.replace("+", " ") movie_files = [] number_of_discs = int(numberOfDiscs) if number_of_discs == 1: movie_files.append(sub_filename) elif number_of_discs > 1 and not self.set_temp: regexp = movie_file regexp = regexp.replace("\\", "\\\\") regexp = regexp.replace("^", "\^") regexp = regexp.replace("$", "\$") regexp = regexp.replace("+", "\+") regexp = regexp.replace("*", "\*") regexp = regexp.replace("?", "\?") regexp = regexp.replace(".", "\.") regexp = regexp.replace("|", "\|") regexp = regexp.replace("(", "\(") regexp = regexp.replace(")", "\)") regexp = regexp.replace("{", "\{") regexp = regexp.replace("}", "\}") regexp = regexp.replace("[", "\[") regexp = regexp.replace("]", "\]") regexp = re.sub("\d+", "\\d+", regexp) regex = re.compile(regexp, re.IGNORECASE) movie_dir = os.path.dirname(self.file_path) movie_file = os.path.basename(self.file_path) files = os.listdir(movie_dir) for file in files: if regex.match(self.file_path) != None: movie_files.append(self.file_path) movie_files.sort() if not zipfile.is_zipfile(zip_file_path): self.getControl(STATUS_LABEL).setLabel(_(654)) label2 = "[COLOR=FF00FF00]%s[/COLOR]" % (_(612)) listitem = xbmcgui.ListItem(label, label2) self.getControl(SUBTITLES_LIST).addItem(listitem) else: self.getControl(STATUS_LABEL).setLabel(_(652)) zip = zipfile.ZipFile(zip_file_path, "r") i = 0 for zip_entry in zip.namelist(): file_name = zip_entry i = i + 1 if i <= len(movie_files): sub_ext = os.path.splitext(file_name)[1] sub_name = os.path.splitext(movie_files[i - 1])[0] if self.set_temp: sub_name = self.search_string.replace("+", " ") file_name = "%s.%s%s" % (sub_name, subtitle_lang, sub_ext) file_path = os.path.join(self.sub_folder, file_name) ##LOG( LOG_INFO, "File Path "+ file_name ) outfile = open(file_path, "wb") outfile.write(zip.read(zip_entry)) outfile.close() zip.close() xbmc.Player().setSubtitles( xbmc.translatePath(os.path.join(os.getcwd(), "resources", "lib", "dummy.srt")) ) time.sleep(2) xbmc.Player().setSubtitles(file_path) os.remove(base64_file_path) os.remove(zip_file_path) self.exit_script()
def download_subtitles_sub(self, pos): subtitle_id = self.subtitles[pos][ "subtitleID" ] language = self.subtitles[pos][ "language" ] numberOfDiscs = SublightUtils.toInteger( self.subtitles[pos][ "numberOfDiscs" ] ) self.getControl( STATUS_LABEL ).setLabel( _( 649 ) ) sublightWebService = SublightUtils.SublightWebService() ticket_id, download_wait = sublightWebService.GetDownloadTicket(self.session_id, subtitle_id) if ticket_id != "" : if download_wait > 0 : time.sleep(float(download_wait)) xbmc.Player().setSubtitles(xbmc.translatePath( os.path.join( os.getcwd(), 'resources', 'lib', 'dummy.srt' ) ) ) subtitle_b64_data = sublightWebService.DownloadByID(self.session_id, subtitle_id, ticket_id) base64_file_path = "special://temp/subtitle.b64" base64_file = open(base64_file_path, "wb") base64_file.write( subtitle_b64_data ) base64_file.close() base64_file = open(base64_file_path, "r") zip_file_path = "special://temp/subtitle.zip" zip_file = open(zip_file_path, "wb") base64.decode(base64_file, zip_file) base64_file.close() zip_file.close() filename = "" subtitle_format = "srt" local_path = self.sub_folder zip_filename = zip_file_path sub_filename = os.path.basename( self.file_path ) form = "srt" lang = str(toOpenSubtitles_two(language)) subtitle_lang = lang subName1 = sub_filename[0:sub_filename.rfind(".")] if subName1 == "":subName1 = self.search_string.replace("+", " ") movie_files = [] number_of_discs = int(numberOfDiscs) if number_of_discs == 1 : movie_files.append(sub_filename) elif number_of_discs > 1: #and not self.set_temp: regexp = movie_file regexp = regexp.replace( "\\", "\\\\" ) regexp = regexp.replace( "^", "\^" ) regexp = regexp.replace( "$", "\$" ) regexp = regexp.replace( "+", "\+" ) regexp = regexp.replace( "*", "\*" ) regexp = regexp.replace( "?", "\?" ) regexp = regexp.replace( ".", "\." ) regexp = regexp.replace( "|", "\|" ) regexp = regexp.replace( "(", "\(" ) regexp = regexp.replace( ")", "\)" ) regexp = regexp.replace( "{", "\{" ) regexp = regexp.replace( "}", "\}" ) regexp = regexp.replace( "[", "\[" ) regexp = regexp.replace( "]", "\]" ) regexp = re.sub( "\d+", "\\d+", regexp ) regex = re.compile( regexp, re.IGNORECASE ) movie_dir = os.path.dirname (self.file_path) movie_file = os.path.basename (self.file_path) files = os.listdir( movie_dir ) for file in files : if regex.match( self.file_path ) != None: movie_files.append(self.file_path) movie_files.sort() if not zipfile.is_zipfile( zip_file_path ) : label = "" self.list = [] if self.SL : label2 = "[COLOR=FFFF0000]%s[/COLOR]" % ( _( 610 ) + " OpenSubtitles.org" ) listitem = xbmcgui.ListItem( label,label2 ) self.list.append("OS") self.getControl( SUBTITLES_LIST ).addItem( listitem ) if self.OS : label2 = "[COLOR=FFFF0000]%s[/COLOR]" % ( _( 610 ) + " Podnapisi.net" ) listitem = xbmcgui.ListItem( label,label2 ) self.list.append("PN") self.getControl( SUBTITLES_LIST ).addItem( listitem ) if self.mansearch : label2 = "[COLOR=FF00FF00]%s[/COLOR]" % ( _( 612 ) ) listitem = xbmcgui.ListItem( label,label2 ) self.list.append("MN") self.getControl( SUBTITLES_LIST ).addItem( listitem ) else : self.getControl( STATUS_LABEL ).setLabel( _( 652 ) ) zip = zipfile.ZipFile (zip_file_path, "r") i = 0 for zip_entry in zip.namelist(): file_name = zip_entry i = i + 1 if i <= len( movie_files ) : sub_ext = os.path.splitext( file_name )[1] sub_name = os.path.splitext( movie_files[i - 1] )[0] file_name = "%s.%s%s" % ( sub_name, subtitle_lang, sub_ext ) file_path = xbmc.translatePath( os.path.join(self.sub_folder, file_name) ) try: outfile = open(file_path, "wb") outfile.write( zip.read(zip_entry) ) outfile.close() xbmc.Player().setSubtitles(file_path) except: import xbmcgui dialog = xbmcgui.Dialog() selected = dialog.yesno("OpenSubtitles_OSD", "You can't save subtitle to Selected destination", "Please choose different Subtitle folder under Script Settings.", "Would you like to adjust the settings now?" ) if selected == 1: __settings__.openSettings() zip.close() os.remove(xbmc.translatePath(base64_file_path)) os.remove(xbmc.translatePath(zip_file_path)) self.exit_script()
def __init__( self ) : # # Init # IMAGES_PATH = xbmc.translatePath( os.path.join( os.getcwd(), 'resources', 'images' ) ) sublightWebService = SublightUtils.SublightWebService() # # Parse parameters... # params = dict(part.split('=') for part in sys.argv[ 2 ][ 1: ].split('&')) searchBy = params[ "search_by" ] # # Playing movie... # if searchBy == "playing" : movieFullPath = xbmc.Player().getPlayingFile() # # Browse for movie file... # else : # Get last location (get up one level if path doesn't exist anymore)... lastMoviePath = xbmcplugin.getSetting ("lastMoviePath") while lastMoviePath != "" and \ lastMoviePath != "\\" and \ not os.path.exists(lastMoviePath) : lastMoviePath = os.path.dirname(lastMoviePath) # Browse dialog... browse = xbmcgui.Dialog() movieFullPath = browse.browse(1, xbmc.getLocalizedString(30200), "video", ".avi|.mp4|.mpg|.mpeg|.wmv|.asf|.divx|.mov|.m2p|.moov|.omf|.qt|.rm|.vob|.dat|.dv|.3ivx|.mkv|.ogm", False, False, lastMoviePath) # No file selected (exit)... if not os.path.isfile(movieFullPath) : xbmcplugin.endOfDirectory( handle=int( sys.argv[ 1 ] ), succeeded=False ) return # Save last location... lastMoviePath = os.path.join(os.path.dirname(movieFullPath), "") xbmcplugin.setSetting("lastMoviePath", lastMoviePath) # Split in dir and filename... movie_dir = os.path.dirname (movieFullPath) movie_file = os.path.basename (movieFullPath) # # (By Title) Confirm movie title... # movie_title, movie_year = "", "" if searchBy == "title" : # Attempt to extract the movie title and year from the file name... movie_title, movie_year = SublightUtils.getMovieTitleAndYear( movie_file ) # # Get a list of similar titles from the webservice... # movies = sublightWebService.SuggestTitles( movie_title ) # Ask the user to choose a movie entry... if len( movies ) > 0: options = [] for movie in movies : option = "%s (%s)" % ( movie[ "title" ], movie [ "year" ] ) options.append( option ) dialog = xbmcgui.Dialog() choice = dialog.select( xbmc.getLocalizedString(30307), options ) dialog = None # User made a choice... if choice > -1 : movie = movies[ choice ]; movie_title = movie[ "title"] movie_year = movie[ "year" ] # User cancelled... else : xbmcplugin.endOfDirectory( handle=int( sys.argv[ 1 ] ), succeeded=False ) return # # No matching results... # else : # # Ask the user to confirm movie title and year... # if movie_year != "" : text = "%s (%s)" % ( movie_title, movie_year ) else : text = "%s" % ( movie_title ) keyboard = xbmc.Keyboard( text ) keyboard.doModal() if keyboard.isConfirmed() : movie_title, movie_year = SublightUtils.getMovieTitleAndYear( keyboard.getText() ) else : xbmcplugin.endOfDirectory( handle=int( sys.argv[ 1 ] ), succeeded=False ) return # # (WebService) Login... # dp = xbmcgui.DialogProgress() dp.create("Sublight" ) dp.update( 33, xbmc.getLocalizedString(30300), " ", " " ) # Get session id... session_id = sublightWebService.LogInAnonymous() # # (By video hash) Calculate video hash... # video_hash = "0000000000000000000000000000000000000000000000000000" if searchBy == "hash" : video_hash = SublightUtils.calculateVideoHash( movieFullPath, False ) # # (By playing movie) Calculate video hash... # if searchBy == "playing" : videoInfoTag = xbmc.Player().getVideoInfoTag() movie_title = videoInfoTag.getTitle() movie_year = ( videoInfoTag.getYear(), "" ) [ videoInfoTag.getYear() == 0 ] # No movie title, ask user... if movie_title == "" : keyboard = xbmc.Keyboard( "", xbmc.getLocalizedString(30107) ) keyboard.doModal() if keyboard.isConfirmed() : movie_title, movie_year = SublightUtils.getMovieTitleAndYear( keyboard.getText() ) # Calculate hash... video_hash = SublightUtils.calculateVideoHash( movieFullPath, True ) # # (WebService) Search... # if searchBy == "hash" : message = movie_file elif searchBy == "title" : message = ( "%s (%s)" % ( movie_title, movie_year ), "%s" % ( movie_title ) ) [ movie_year == "" ] elif searchBy == "playing" : message = ( "%s (%s)" % ( movie_title, movie_year ), "%s" % ( movie_file ) ) [ movie_year == "" ] dp.update( 66, xbmc.getLocalizedString(30301), message, " " ) # Searching... subtitles = [] language1 = SublightUtils.toSublightLanguage( str( int( xbmcplugin.getSetting ("language1") ) + 1 ) ) language2 = SublightUtils.toSublightLanguage( xbmcplugin.getSetting ("language2") ) language3 = SublightUtils.toSublightLanguage( xbmcplugin.getSetting ("language3") ) subtitles = sublightWebService.SearchSubtitles(session_id, video_hash, movie_title, movie_year, language1, language2, language3 ) # # No subtitles found... # if len(subtitles) == 0 : # Close dialog progress... dp.close() # Message... dialog = xbmcgui.Dialog() dialog.ok("Sublight", xbmc.getLocalizedString(30305)) dialog = None # End of directory... xbmcplugin.endOfDirectory( handle=int( sys.argv[ 1 ] ), succeeded=False ) # # Subtitles found... # else: for subtitle in subtitles: subtitle_id = subtitle[ "subtitleID" ] title = subtitle[ "title" ] year = SublightUtils.toInteger( subtitle[ "year" ] ) release = subtitle[ "release" ] language = subtitle[ "language" ] mediaType = subtitle[ "mediaType" ] numberOfDiscs = SublightUtils.toInteger( subtitle[ "numberOfDiscs" ] ) downloads = subtitle[ "downloads" ] isLinked = subtitle[ "isLinked" ] icon_flag = os.path.join( IMAGES_PATH, "flag_%s.gif" % language.lower()) # Add directory entry... label = "[%s] %s" % ( language, release ) if isLinked == "true" : label2 = "[COLOR=FF00FF00]%s downloads [%u x %s][/COLOR]" % ( downloads, numberOfDiscs, mediaType ) else: label2 = "%s downloads [%u x %s]" % ( downloads, numberOfDiscs, mediaType ) listitem = xbmcgui.ListItem( label, label2, "DefaultVideoBig.png", icon_flag ) listitem.setInfo( type="Video", infoLabels={ "Title" : label, "Genre" : label2, "Year" : year } ) url = "%s?action=download&session_id=%s&subtitle_id=%s&subtitle_lang=%s&movie_dir=%s&movie_file=%s&number_of_discs=%u" % \ ( sys.argv[ 0 ], urllib.quote_plus( session_id ), urllib.quote_plus( subtitle_id ), urllib.quote_plus( language ), urllib.quote_plus( movie_dir ), urllib.quote_plus( movie_file ), numberOfDiscs ) xbmcplugin.addDirectoryItem( handle = int(sys.argv[ 1 ]), url=url, listitem=listitem, isFolder=False) # Allow sort by Genre (for list2 labels)... xbmcplugin.addSortMethod( handle=int( sys.argv[ 1 ] ), sortMethod=xbmcplugin.SORT_METHOD_GENRE ) # End of directory... xbmcplugin.endOfDirectory( handle=int( sys.argv[ 1 ] ), succeeded=True )
def download_subtitles_sub(self, pos): subtitle_id = self.subtitles[pos]["subtitleID"] language = self.subtitles[pos]["language"] numberOfDiscs = SublightUtils.toInteger( self.subtitles[pos]["numberOfDiscs"]) self.getControl(STATUS_LABEL).setLabel(_(649)) sublightWebService = SublightUtils.SublightWebService() ticket_id, download_wait = sublightWebService.GetDownloadTicket( self.session_id, subtitle_id) if ticket_id != "": if download_wait > 0: time.sleep(float(download_wait)) xbmc.Player().setSubtitles( xbmc.translatePath( os.path.join(os.getcwd(), 'resources', 'lib', 'dummy.srt'))) subtitle_b64_data = sublightWebService.DownloadByID( self.session_id, subtitle_id, ticket_id) base64_file_path = "special://temp/subtitle.b64" base64_file = open(base64_file_path, "wb") base64_file.write(subtitle_b64_data) base64_file.close() base64_file = open(base64_file_path, "r") zip_file_path = "special://temp/subtitle.zip" zip_file = open(zip_file_path, "wb") base64.decode(base64_file, zip_file) base64_file.close() zip_file.close() filename = "" subtitle_format = "srt" local_path = self.sub_folder zip_filename = zip_file_path sub_filename = os.path.basename(self.file_path) form = "srt" lang = str(toOpenSubtitles_two(language)) subtitle_lang = lang subName1 = sub_filename[0:sub_filename.rfind(".")] if subName1 == "": subName1 = self.search_string.replace("+", " ") movie_files = [] number_of_discs = int(numberOfDiscs) if number_of_discs == 1: movie_files.append(sub_filename) elif number_of_discs > 1: #and not self.set_temp: regexp = movie_file regexp = regexp.replace("\\", "\\\\") regexp = regexp.replace("^", "\^") regexp = regexp.replace("$", "\$") regexp = regexp.replace("+", "\+") regexp = regexp.replace("*", "\*") regexp = regexp.replace("?", "\?") regexp = regexp.replace(".", "\.") regexp = regexp.replace("|", "\|") regexp = regexp.replace("(", "\(") regexp = regexp.replace(")", "\)") regexp = regexp.replace("{", "\{") regexp = regexp.replace("}", "\}") regexp = regexp.replace("[", "\[") regexp = regexp.replace("]", "\]") regexp = re.sub("\d+", "\\d+", regexp) regex = re.compile(regexp, re.IGNORECASE) movie_dir = os.path.dirname(self.file_path) movie_file = os.path.basename(self.file_path) files = os.listdir(movie_dir) for file in files: if regex.match(self.file_path) != None: movie_files.append(self.file_path) movie_files.sort() if not zipfile.is_zipfile(zip_file_path): label = "" self.list = [] if self.SL: label2 = "[COLOR=FFFF0000]%s[/COLOR]" % ( _(610) + " OpenSubtitles.org") listitem = xbmcgui.ListItem(label, label2) self.list.append("OS") self.getControl(SUBTITLES_LIST).addItem(listitem) if self.OS: label2 = "[COLOR=FFFF0000]%s[/COLOR]" % (_(610) + " Podnapisi.net") listitem = xbmcgui.ListItem(label, label2) self.list.append("PN") self.getControl(SUBTITLES_LIST).addItem(listitem) if self.mansearch: label2 = "[COLOR=FF00FF00]%s[/COLOR]" % (_(612)) listitem = xbmcgui.ListItem(label, label2) self.list.append("MN") self.getControl(SUBTITLES_LIST).addItem(listitem) else: self.getControl(STATUS_LABEL).setLabel(_(652)) zip = zipfile.ZipFile(zip_file_path, "r") i = 0 for zip_entry in zip.namelist(): file_name = zip_entry i = i + 1 if i <= len(movie_files): sub_ext = os.path.splitext(file_name)[1] sub_name = os.path.splitext(movie_files[i - 1])[0] file_name = "%s.%s%s" % (sub_name, subtitle_lang, sub_ext) file_path = xbmc.translatePath( os.path.join(self.sub_folder, file_name)) try: outfile = open(file_path, "wb") outfile.write(zip.read(zip_entry)) outfile.close() xbmc.Player().setSubtitles(file_path) except: import xbmcgui dialog = xbmcgui.Dialog() selected = dialog.yesno( "OpenSubtitles_OSD", "You can't save subtitle to Selected destination", "Please choose different Subtitle folder under Script Settings.", "Would you like to adjust the settings now?") if selected == 1: __settings__.openSettings() zip.close() os.remove(xbmc.translatePath(base64_file_path)) os.remove(xbmc.translatePath(zip_file_path)) self.exit_script()