def search_subtitles(file_original_path, title, tvshow, year, season, episode, set_temp, rar, lang1, lang2, lang3, stack): #standard input # Build an adequate string according to media type if tvshow: search_string = "%s S%02dE%02d" % (tvshow, int(season), int(episode)) else: search_string = title subtitles_list = [] msg = "" downloader = TorecSubtitlesDownloader() metadata = downloader.getSubtitleMetaData(search_string) if metadata != None: for option in metadata.options: subtitles_list.append({ 'page_id': metadata.id, 'filename': option.name, 'language_flag': "flags/he.gif", 'language_name': "Hebrew", 'subtitle_id': option.id, 'sync': False, 'rating': "0", }) return subtitles_list, "", msg
def download_subtitles (subtitles_list, pos, zip_subs, tmp_sub_dir, sub_folder, session_id): #standard input page_id = subtitles_list[pos]["page_id"] subtitle_id = subtitles_list[pos]["subtitle_id"] icon = os.path.join(__cwd__,"icon.png") delay = 20 download_wait = delay downloader = TorecSubtitlesDownloader() # Wait the minimal time needed for retrieving the download link for i in range (int(download_wait)): downloadLink = downloader.getDownloadLink(page_id, subtitle_id, False) if (downloadLink != None): break line2 = "download will start in %i seconds" % (delay,) xbmc.executebuiltin("XBMC.Notification(%s,%s,1000,%s)" % (__scriptname__,line2,icon)) delay -= 1 time.sleep(1) log(__name__ ,"Downloading subtitles from '%s'" % downloadLink) (subtitleData, subtitleName) = downloader.download(downloadLink) log(__name__ ,"Saving subtitles to '%s'" % zip_subs) downloader.saveData(zip_subs, subtitleData, False) return True,"Hebrew", "" #standard output
def search(item): best_match_id = None downloader = TorecSubtitlesDownloader() try: search_string = build_search_string(item) search_data = downloader.getSubtitleMetaData(search_string) log(__name__, "search_data=%s" % str(search_data)) if search_data != None: best_match_id = downloader.getBestMatchID( os.path.basename(item['file_original_path']), search_data) except: log(__name__, "failed to connect to service for subtitle search") xbmc.executebuiltin( (u'Notification(%s,%s)' % (__scriptname__, __language__(32001))).encode('utf-8')) return list_items = [] if search_data != None: for item_data in search_data.options: listitem = xbmcgui.ListItem( label="Hebrew", label2=item_data.name, iconImage="0", thumbnailImage="he", ) url = "plugin://%s/?action=download&page_id=%s&subtitle_id=%s&filename=%s" % ( __scriptid__, search_data.id, item_data.id, item_data.name) if best_match_id != None and item_data.id == best_match_id: listitem.setProperty("sync", "true") list_items.insert(0, ( url, listitem, False, )) else: list_items.append(( url, listitem, False, )) xbmcplugin.addDirectoryItems(handle=int(sys.argv[1]), items=list_items)
def download(page_id, subtitle_id,filename, stack=False): files = glob.glob(os.path.join(__temp__, "*.srt")) for f in files: log(__name__, "deleting %s" % f) os.remove(f) subtitle_list = [] exts = [".srt", ".sub"] delay = 20 download_wait = delay downloader = TorecSubtitlesDownloader() start_time = time.time() try: # Wait the minimal time needed for retrieving the download link for i in range (int(download_wait)): result = downloader.getDownloadLink(page_id, subtitle_id, False) if (result != None): break log(__name__ ,"download will start in %i seconds" % (delay,)) delay -= 1 time.sleep(1) except: log( __name__, "failed to connect to service for subtitle download") return subtitle_list if result != None: log(__name__ ,"Downloading subtitles from '%s'" % result) (subtitleData, subtitleName) = downloader.download(result) zip = os.path.join(__temp__, "Torec.zip") with open(zip, "wb") as subFile: subFile.write(subtitleData) xbmc.executebuiltin(('XBMC.Extract("%s","%s")' % (zip,__temp__,)).encode('utf-8'), True) for file in xbmcvfs.listdir(__temp__)[1]: log(__name__, "file=%s" % file) file = os.path.join(__temp__, file) if (os.path.splitext(file)[1] in exts): convert_to_utf(file) subtitle_list.append(file) log(__name__, "Overall download took %f" % (time.time() - start_time)) return subtitle_list
def search(item): best_match_id = None downloader = TorecSubtitlesDownloader() search_data = None start_time = time.time() try: search_start_time = time.time() search_string = build_search_string(item) search_data = downloader.search(search_string) log(__name__, "search took %f" % (time.time() - search_start_time)) except: log( __name__, "failed to connect to service for subtitle search") xbmc.executebuiltin((u'Notification(%s,%s)' % (__scriptname__ , __language__(32001))).encode('utf-8')) return list_items = [] if search_data: best_match_id = downloader.get_best_match_id(os.path.basename(item['file_original_path']), search_data) for item_data in search_data.options: listitem = xbmcgui.ListItem(label = "Hebrew", label2 = item_data.name, iconImage = "0", thumbnailImage = "he", ) url = "plugin://%s/?action=download&page_id=%s&subtitle_id=%s&filename=%s" % (__scriptid__, search_data.id, item_data.id, item_data.name ) if best_match_id != None and item_data.id == best_match_id: log(__name__, "Found most relevant option to be : %s" % item_data.name) listitem.setProperty("sync", "true") list_items.insert(0, (url, listitem, False,)) else: list_items.append((url, listitem, False,)) xbmcplugin.addDirectoryItems(handle=int(sys.argv[1]), items=list_items) log(__name__, "Overall search took %f" % (time.time() - start_time))
def download_subtitles(subtitles_list, pos, zip_subs, tmp_sub_dir, sub_folder, session_id): #standard input page_id = subtitles_list[pos]["page_id"] subtitle_id = subtitles_list[pos]["subtitle_id"] icon = os.path.join(__cwd__, "icon.png") delay = 20 download_wait = delay downloader = TorecSubtitlesDownloader() # Wait the minimal time needed for retrieving the download link for i in range(int(download_wait)): downloadLink = downloader.getDownloadLink(page_id, subtitle_id, False) if (downloadLink != None): break line2 = "download will start in %i seconds" % (delay, ) xbmc.executebuiltin("XBMC.Notification(%s,%s,1000,%s)" % (__scriptname__, line2, icon)) delay -= 1 time.sleep(1) log(__name__, "Downloading subtitles from '%s'" % downloadLink) (subtitleData, subtitleName) = downloader.download(downloadLink) log(__name__, "Saving subtitles to '%s'" % zip_subs) downloader.saveData(zip_subs, subtitleData, False) return True, "Hebrew", "" #standard output
def download(sub_id, option_id, filename, stack=False): result = None subtitle_list = [] exts = [".srt", ".sub"] downloader = TorecSubtitlesDownloader() start_time = time.time() delete_old_subs() try: result = downloader.get_download_link(sub_id, option_id) except Exception as e: log(__name__,"failed to connect to service for subtitle download %s" % e) return subtitle_list if result is not None: log(__name__, "Downloading subtitles from '%s'" % result) (subtitle_data, subtitle_name) = downloader.download(result) (file_name, file_ext) = os.path.splitext(subtitle_name) archive_file = os.path.join(__temp__, "Torec%s" % file_ext) with open(archive_file, "wb") as subFile: subFile.write(subtitle_data) xbmc.executebuiltin( ('XBMC.Extract("%s","%s")' % (archive_file, __temp__,) ).encode('utf-8'), True) for file_ in xbmcvfs.listdir(__temp__)[1]: ufile = file_.decode('utf-8') log(__name__, "file=%s" % ufile) file_ = os.path.join(__temp__, ufile) if os.path.splitext(ufile)[1] in exts: convert_to_utf(file_) subtitle_list.append(file_) log(__name__, "Overall download took %f" % (time.time() - start_time)) return subtitle_list
def search_subtitles(file_original_path, title, tvshow, year, season, episode, set_temp, rar, lang1, lang2, lang3, stack ): #standard input # Build an adequate string according to media type if tvshow: search_string = "%s S%02dE%02d" % (tvshow, int(season), int(episode)) else: search_string = title subtitles_list = [] msg = "" downloader = TorecSubtitlesDownloader() metadata = downloader.getSubtitleMetaData(search_string) if metadata != None: for option in metadata.options: subtitles_list.append({'page_id' : metadata.id, 'filename' : option.name, 'language_flag' : "flags/he.gif", 'language_name' : "Hebrew", 'subtitle_id' : option.id, 'sync' : False, 'rating' : "0", }) return subtitles_list, "", msg
def download(page_id, subtitle_id,filename, stack=False): subtitle_list = [] exts = [".srt", ".sub"] delay = 20 download_wait = delay downloader = TorecSubtitlesDownloader() try: # Wait the minimal time needed for retrieving the download link for i in range (int(download_wait)): result = downloader.getDownloadLink(page_id, subtitle_id, False) if (result != None): break log(__name__ ,"download will start in %i seconds" % (delay,)) delay -= 1 time.sleep(1) except: log( __name__, "failed to connect to service for subtitle download") return subtitle_list if result != None: log(__name__ ,"Downloading subtitles from '%s'" % result) (subtitleData, subtitleName) = downloader.download(result) zip = os.path.join( __temp__, "Torec.zip") downloader.saveData(zip, subtitleData) for file in xbmcvfs.listdir(__temp__)[1]: log(__name__, "file=%s" % file) file = os.path.join(__temp__, file) if (os.path.splitext( file )[1] in exts): subtitle_list.append(file) return subtitle_list
def DownloadTorecSubtitle(search_string, release, dest_dir): subtitles_list = [] downloader = TorecSubtitlesDownloader() metadata = downloader.getSubtitleMetaData(search_string) for option in metadata.options: if release in option.name: subtitles_list.append({'page_id' : metadata.id, 'filename' : option.name, 'subtitle_id' : option.id }) # If there is one match, download it if len(subtitles_list) == 1: sub = 0 else: print 'Select sub : ' for i,sub in enumerate(subtitles_list): print '%s) %s' % (i, sub['filename']) sub = int(raw_input(' > ')) page_id = subtitles_list[sub]["page_id"] subtitle_id = subtitles_list[sub]["subtitle_id"] filename = subtitles_list[sub]["filename"] downloader = TorecSubtitlesDownloader() downloadLink = downloader.getDownloadLink(page_id, subtitle_id, False) (subtitleData, subtitleName) = downloader.download(downloadLink) temp_dir = tempfile.mkdtemp() file_name = downloader.saveData('%s/%s' % (temp_dir, filename) , subtitleData, True) src = os.path.join(temp_dir,file_name) dst = os.path.join(dest_dir,file_name) log(__name__, "Moving file to %s" % dst) shutil.move(src,dst)
def download(page_id, subtitle_id, filename, stack=False): subtitle_list = [] exts = [".srt", ".sub"] delay = 20 download_wait = delay downloader = TorecSubtitlesDownloader() try: # Wait the minimal time needed for retrieving the download link for i in range(int(download_wait)): result = downloader.getDownloadLink(page_id, subtitle_id, False) if (result != None): break log(__name__, "download will start in %i seconds" % (delay, )) delay -= 1 time.sleep(1) except: log(__name__, "failed to connect to service for subtitle download") return subtitle_list if result != None: log(__name__, "Downloading subtitles from '%s'" % result) (subtitleData, subtitleName) = downloader.download(result) zip = os.path.join(__temp__, "Torec.zip") downloader.saveData(zip, subtitleData) for file in xbmcvfs.listdir(__temp__)[1]: log(__name__, "file=%s" % file) file = os.path.join(__temp__, file) if (os.path.splitext(file)[1] in exts): subtitle_list.append(file) return subtitle_list
def setUpClass(self): self.downloader = TorecSubtitlesDownloader()
class MovieTests(unittest.TestCase): @classmethod def setUpClass(self): self.downloader = TorecSubtitlesDownloader() def test_search_movie_sanity(self): item = self._create_test_valid_item() options = self.downloader.search_movie(item['title']) self.assertIsNotNone(options) self.assertEqual(len(options), 22) def test_search_inexisting_movie(self): item = { 'title': 'finding mori', } options = self.downloader.search_movie(item['title']) self.assertIsNone(options) def test_download_movie_sanity(self): item = self._create_test_valid_item() options = self.downloader.search_movie(item['title']) option = options[0] page_id = option.sub_id subtitle_id = option.option_id result = self.downloader.get_download_link(page_id, subtitle_id) subtitleData, fileName = self.downloader.download(result) self.assertIsNotNone(subtitleData) self._assert_subtitle_data(subtitleData, fileName) def _create_test_valid_item(self): return { 'title': 'finding dory', } def _assert_subtitle_data(self, subtitleData, fileName): extension = os.path.splitext(fileName)[1] temp = tempfile.NamedTemporaryFile() temp.write(subtitleData) temp.flush() if (extension == ".zip"): rf = zipfile.ZipFile(temp.name) elif (extension == ".rar"): rf = rarfile.RarFile(temp.name) else: fail("unknown extension found %s", extension) for f in rf.infolist(): data = rf.read(f.filename) temp = tempfile.NamedTemporaryFile() temp.write(data) temp.flush() self.assertIsNotNone(temp.read()) break
class TVShowTests(unittest.TestCase): @classmethod def setUpClass(self): self.downloader = TorecSubtitlesDownloader() def test_search_tvshow_sanity(self): item = self._create_test_valid_item() options = self.downloader.search_tvshow(item['tvshow'], item['season'], item['episode']) self.assertIsNotNone(options) self.assertGreaterEqual(len(options), 5) def test_search_tvshow_last_season_episode(self): item = {'tvshow': 'house of cards', 'season': '3', 'episode': '13'} options = self.downloader.search_tvshow(item['tvshow'], item['season'], item['episode']) self.assertIsNotNone(options) self.assertGreaterEqual(len(options), 1) def test_search_inexisting_tvshow(self): item = {'tvshow': 'house of lards', 'season': '3', 'episode': '1'} options = self.downloader.search_tvshow(item['tvshow'], item['season'], item['episode']) self.assertIsNone(options) def test_search_existing_tvshow_with_no_subtitles(self): item = {'tvshow': 'Cowboy Bebop', 'season': '1', 'episode': '1'} options = self.downloader.search_tvshow(item['tvshow'], item['season'], item['episode']) self.assertIsNone(options) def test_download_tvshow_sanity(self): item = self._create_test_valid_item() options = self.downloader.search_tvshow(item['tvshow'], item['season'], item['episode']) option = options[-1] page_id = option.sub_id subtitle_id = option.option_id download_link = self.downloader.get_download_link(page_id, subtitle_id) self.assertTrue(len(download_link) != 0) subtitleData, fileName = self.downloader.download(download_link) self.assertIsNotNone(subtitleData) self._assert_subtitle_data(subtitleData, fileName) def _create_test_valid_item(self): return {'tvshow': 'house of cards', 'season': '3', 'episode': '1'} def _assert_subtitle_data(self, subtitleData, fileName): extension = os.path.splitext(fileName)[1] temp = tempfile.NamedTemporaryFile() temp.write(subtitleData) temp.flush() if (extension == ".zip"): rf = zipfile.ZipFile(temp.name) elif (extension == ".rar"): rf = rarfile.RarFile(temp.name) else: fail("unknown extension found %s", extension) for f in rf.infolist(): data = rf.read(f.filename) temp = tempfile.NamedTemporaryFile() temp.write(data) temp.flush() self.assertIsNotNone(temp.read()) break
def search(item): best_match_id = None downloader = TorecSubtitlesDownloader() subtitles_options = None start_time = time.time() try: search_start_time = time.time() if (item['mansearch'] == False): if item['tvshow'] != "": subtitles_options = downloader.search_tvshow(item['tvshow'], item['season'], item['episode']) else: title, year = xbmc.getCleanMovieTitle(item['title']) subtitles_options = downloader.search_movie(title) else: item['tvshow'], item['season'], item['episode'] = check_and_parse_if_title_is_TVshow(item['title']) if (item['tvshow'] == "NotTVShow"): item['title'] = item['title'].replace("%20", "%2b") # " " to "+" title, year = xbmc.getCleanMovieTitle(item['title']) subtitles_options = downloader.search_movie(title) else: subtitles_options = downloader.search_tvshow(item['tvshow'], int(item['season']), int(item['episode'])) log(__name__, "search took %f" % (time.time() - search_start_time)) except Exception as e: log( __name__, "failed to connect to service for subtitle search %s" % e ) xbmc.executebuiltin( (u'Notification(%s,%s)' % (__scriptname__, __language__(32001)) ).encode('utf-8')) return list_items = [] if subtitles_options: best_match_option_id = downloader.get_best_match_id( os.path.basename(item['file_original_path']), subtitles_options ) for item_data in subtitles_options: listitem = xbmcgui.ListItem( label="Hebrew", label2=item_data.name, iconImage="0", thumbnailImage="he" ) url = ( "plugin://%s/?action=download&sub_id=%s&option_id=" "%s&filename=%s" % ( __scriptid__, item_data.sub_id, item_data.option_id, item_data.name ) ) if item_data.option_id == best_match_option_id: log( __name__, "Found most relevant option to be : %s" % item_data.name ) listitem.setProperty("sync", "true") list_items.insert(0, (url, listitem, False,)) else: list_items.append((url, listitem, False,)) xbmcplugin.addDirectoryItems(handle=int(sys.argv[1]), items=list_items) log(__name__, "Overall search took %f" % (time.time() - start_time))