def test_save_subtitles(self): videos = [EPISODES[0], EPISODES[1]] for video in videos: video.name = os.path.join(TEST_DIR, os.path.split(video.name)[1]) languages = {Language('eng'), Language('fra')} subtitles = list_subtitles(videos, languages) # make a list of subtitles to download (one per language per video) subtitles_to_download = [] for video, video_subtitles in subtitles.items(): video_subtitle_languages = set() for video_subtitle in video_subtitles: if video_subtitle.language in video_subtitle_languages: continue subtitles_to_download.append(video_subtitle) video_subtitle_languages.add(video_subtitle.language) if video_subtitle_languages == languages: break self.assertEqual(len(subtitles_to_download), 4) # download download_subtitles(subtitles_to_download) save_subtitles(subtitles) for video in videos: self.assertTrue(os.path.exists(os.path.splitext(video.name)[0] + '.en.srt')) self.assertTrue(os.path.exists(os.path.splitext(video.name)[0] + '.fr.srt'))
def test_save_subtitles(self): videos = [EPISODES[0], EPISODES[1]] for video in videos: video.name = os.path.join(TEST_DIR, os.path.split(video.name)[1]) languages = {Language('eng'), Language('fra')} subtitles = list_subtitles(videos, languages) # make a list of subtitles to download (one per language per video) subtitles_to_download = [] for video, video_subtitles in subtitles.items(): video_subtitle_languages = set() for video_subtitle in video_subtitles: if video_subtitle.language in video_subtitle_languages: continue subtitles_to_download.append(video_subtitle) video_subtitle_languages.add(video_subtitle.language) if video_subtitle_languages == languages: break self.assertEqual(len(subtitles_to_download), 4) # download download_subtitles(subtitles_to_download) save_subtitles(subtitles) for video in videos: self.assertTrue( os.path.exists(os.path.splitext(video.name)[0] + '.en.srt')) self.assertTrue( os.path.exists(os.path.splitext(video.name)[0] + '.fr.srt'))
def test_download_subtitles_multi_noforce(self): results_first = download_subtitles(test_dir, languages=['en', 'fr'], cache_dir=cache_dir, multi=True, force=False, services=['thesubdb']) results = download_subtitles(test_dir, languages=['en', 'fr'], cache_dir=cache_dir, multi=True, force=False, services=['thesubdb']) self.assertTrue(len(results) == 0) for _, subtitles in results_first.iteritems(): for subtitle in subtitles: os.remove(subtitle.path)
def test_download_subtitles(self): videos = [EPISODES[0]] for video in videos: video.name = os.path.join(TEST_DIR, os.path.split(video.name)[1]) languages = {Language('eng')} subtitles = list_subtitles(videos, languages) download_subtitles(subtitles[videos[0]][:5]) self.assertGreaterEqual(len([s for s in subtitles[videos[0]] if s.content is not None]), 4)
def test_download_subtitles_single(self): videos = [EPISODES[0], EPISODES[1]] for video in videos: video.name = os.path.join(TEST_DIR, video.name.split(os.sep)[-1]) languages = {Language("eng"), Language("fra")} subtitles = list_subtitles(videos, languages) download_subtitles(subtitles, single=True) for video in videos: self.assertTrue(os.path.exists(os.path.splitext(video.name)[0] + ".srt"))
def test_download_subtitles(self): videos = [EPISODES[0]] for video in videos: video.name = os.path.join(TEST_DIR, os.path.split(video.name)[1]) languages = {Language('eng')} subtitles = list_subtitles(videos, languages) download_subtitles(subtitles[videos[0]][:5]) self.assertGreaterEqual( len([s for s in subtitles[videos[0]] if s.content is not None]), 4)
def test_download_subtitles_single(self): videos = [EPISODES[0], EPISODES[1]] for video in videos: video.name = os.path.join(TEST_DIR, video.name.split(os.sep)[-1]) languages = {Language('eng'), Language('fra')} subtitles = list_subtitles(videos, languages) download_subtitles(subtitles, single=True) for video in videos: self.assertTrue( os.path.exists(os.path.splitext(video.name)[0] + '.srt'))
def test_download_subtitles_noforce(self): results_first = download_subtitles(test_dir, languages=['en', 'fr'], cache_dir=cache_dir, force=False, services=['thesubdb']) results = download_subtitles(test_dir, languages=['en', 'fr'], cache_dir=cache_dir, force=False, services=['thesubdb']) self.assertTrue(len(results) == 0) for _, subtitles in results_first.iteritems(): for subtitle in subtitles: os.remove(subtitle.path)
def searchSingle(self, group): if self.isDisabled(): return try: available_languages = sum(group["subtitle_language"].values(), []) downloaded = [] files = [toUnicode(x) for x in group["files"]["movie"]] log.debug("Searching for subtitles for: %s", files) for lang in self.getLanguages(): if lang not in available_languages: download = subliminal.download_subtitles( files, multi=True, force=self.conf("force"), languages=[lang], services=self.services, cache_dir=Env.get("cache_dir"), ) for subtitle in download: downloaded.extend(download[subtitle]) for d_sub in downloaded: log.info("Found subtitle (%s): %s", (d_sub.language.alpha2, files)) group["files"]["subtitle"].append(sp(d_sub.path)) group["before_rename"].append(sp(d_sub.path)) group["subtitle_language"][sp(d_sub.path)] = [d_sub.language.alpha2] return True except: log.error("Failed searching for subtitle: %s", (traceback.format_exc())) return False
def searchSingle(self, group): if self.isDisabled(): return try: available_languages = sum(group['subtitle_language'].values(), []) downloaded = [] files = [toUnicode(x) for x in group['files']['movie']] log.debug('Searching for subtitles for: %s', files) for lang in self.getLanguages(): if lang not in available_languages: download = subliminal.download_subtitles(files, multi = True, force = self.conf('force'), languages = [lang], services = self.services, cache_dir = Env.get('cache_dir')) for subtitle in download: downloaded.extend(download[subtitle]) for d_sub in downloaded: log.info('Found subtitle (%s): %s', (d_sub.language.alpha2, files)) group['files']['subtitle'].append(sp(d_sub.path)) group['before_rename'].append(sp(d_sub.path)) group['subtitle_language'][sp(d_sub.path)] = [d_sub.language.alpha2] return True except: log.error('Failed searching for subtitle: %s', (traceback.format_exc())) return False
def searchSingle(self, group): if self.isDisabled(): return try: available_languages = sum(group['subtitle_language'].itervalues(), []) downloaded = [] files = [toUnicode(x) for x in group['files']['movie']] log.debug('Searching for subtitles for: %s', files) for lang in self.getLanguages(): if lang not in available_languages: download = subliminal.download_subtitles(files, multi = True, force = False, languages = [lang], services = self.services, cache_dir = Env.get('cache_dir')) for subtitle in download: downloaded.extend(download[subtitle]) for d_sub in downloaded: log.info('Found subtitle (%s): %s', (d_sub.language.alpha2, files)) group['files']['subtitle'].add(d_sub.path) group['subtitle_language'][d_sub.path] = [d_sub.language.alpha2] return True except: log.error('Failed searching for subtitle: %s', (traceback.format_exc())) return False
def test_download_subtitles(mock_providers): subtitles = [ Addic7edSubtitle(Language('eng'), True, None, 'The Big Bang Theory', 7, 5, 'The Workplace Proximity', 2007, 'DIMENSION', None), TheSubDBSubtitle(Language('eng'), 'ad32876133355929d814457537e12dc2'), TVsubtitlesSubtitle(Language('por'), None, 261077, 'Game of Thrones', 3, 10, None, '1080p.BluRay', 'DEMAND') ] download_subtitles(subtitles) # test providers assert provider_manager['addic7ed'].plugin.download_subtitle.called assert not provider_manager['opensubtitles'].plugin.download_subtitle.called assert not provider_manager['podnapisi'].plugin.download_subtitle.called assert provider_manager['thesubdb'].plugin.download_subtitle.called assert provider_manager['tvsubtitles'].plugin.download_subtitle.called
def searchSingle(self, group): if self.isDisabled(): return try: available_languages = sum(group['subtitle_language'].itervalues(), []) downloaded = [] files = [toUnicode(x) for x in group['files']['movie']] for lang in self.getLanguages(): if lang not in available_languages: download = subliminal.download_subtitles( files, multi=True, force=False, languages=[lang], services=self.services, cache_dir=Env.get('cache_dir')) for subtitle in download: downloaded.extend(download[subtitle]) for d_sub in downloaded: group['files']['subtitle'].add(d_sub.path) group['subtitle_language'][d_sub.path] = [ d_sub.language.alpha2 ] return True except: log.error('Failed searching for subtitle: %s', (traceback.format_exc())) return False
def test_download_subtitles_languages(self): results = download_subtitles('Dexter/Season 04/S04E08 - Road Kill - 720p BluRay.mkv', languages=['en'], cache_dir=cache_dir, multi=True, force=False, services=['subtitulos', 'tvsubtitles']) self.assertTrue(len(results) == 1) for _, subtitles in results.iteritems(): self.assertTrue(len(subtitles) == 1) for subtitle in subtitles: os.remove(subtitle.path)
def test_download_subtitles(mock_providers): subtitles = [ Addic7edSubtitle(Language('eng'), True, None, 'The Big Bang Theory', 7, 5, 'The Workplace Proximity', 2007, 'DIMENSION', None), TheSubDBSubtitle(Language('eng'), 'ad32876133355929d814457537e12dc2'), TVsubtitlesSubtitle(Language('por'), None, 261077, 'Game of Thrones', 3, 10, None, '1080p.BluRay', 'DEMAND') ] download_subtitles(subtitles) # test providers assert provider_manager['addic7ed'].plugin.download_subtitle.called assert not provider_manager['opensubtitles'].plugin.download_subtitle.called assert not provider_manager['podnapisi'].plugin.download_subtitle.called assert provider_manager['thesubdb'].plugin.download_subtitle.called assert provider_manager['tvsubtitles'].plugin.download_subtitle.called assert not provider_manager['subscenter'].plugin.download_subtitle.called
def test_download_subtitles_multi(self): results = download_subtitles(test_video, languages=['en', 'fr'], cache_dir=cache_dir, multi=True) self.assertTrue(len(results) == 1) for video, subtitles in results.iteritems(): self.assertTrue(video.release == test_video) self.assertTrue(len(subtitles) == 2) for subtitle in subtitles: self.assertTrue(os.path.exists(subtitle.path)) os.remove(subtitle.path)
def test_download_subtitles(self): results = download_subtitles(test_video, languages=['en', 'fr'], cache_dir=cache_dir) self.assertTrue(len(results) == 1) for video, subtitles in results.iteritems(): self.assertTrue(video.release == test_video) self.assertTrue(len(subtitles) == 1) for subtitle in subtitles: self.assertTrue(subtitle.path == os.path.splitext(os.path.basename(test_video))[0] + '.srt') self.assertTrue(os.path.exists(subtitle.path)) os.remove(subtitle.path)
def _search_subtitles(video, lang, best_only): log.debug("Searching for subtitles") # Determine language language = babelfish.Language.fromietf(lang) if not language: log.error("Invalid language '%s' specified" % lang) return # Determine if language alpha2 code suffix is needed in srt file name (f.e. <episode_name>.nl.srt) single = False if lang == autosubliminal.DEFAULTLANGUAGE and not autosubliminal.DEFAULTLANGUAGESUFFIX: single = True # Get min match score if isinstance(video, Episode): min_score = autosubliminal.SHOWMINMATCHSCORE elif isinstance(video, Movie): min_score = autosubliminal.MOVIEMINMATCHSCORE else: log.error("Invalid video found '%s'" % video) return # Search for subtitles videos = {video} languages = {language} if best_only: # Download the best subtitle with min_score (without saving it in to file) subtitles = subliminal.download_best_subtitles( videos, languages, min_score=min_score, hearing_impaired=utils.include_hearing_impaired(), only_one=True, providers=autosubliminal.SUBLIMINALPROVIDERLIST, provider_configs=autosubliminal.SUBLIMINALPROVIDERCONFIGS) else: # Download all subtitles with default min score (without saving it to file) subtitles = subliminal.list_subtitles( videos, languages, providers=autosubliminal.SUBLIMINALPROVIDERLIST) subliminal.download_subtitles(subtitles[video]) return subtitles, language, single
def test_download_subtitles(self): results = download_subtitles(test_video, languages=['en', 'fr'], cache_dir=cache_dir) self.assertTrue(len(results) == 1) for video, subtitles in results.iteritems(): self.assertTrue(video.release == test_video) self.assertTrue(len(subtitles) == 1) for subtitle in subtitles: self.assertTrue(os.path.exists(subtitle.path)) os.remove(subtitle.path)
def download(self, path, subtitle_id, temp_folder): encoding = addon.getSetting('subliminal.encoding') option = SubtitleOption() subtitle = option.get_subtitle(subtitle_id) xbmc.log('Cached: %s' % subtitle, level=xbmc.LOGDEBUG) video = self.get_video(path) download_subtitles([subtitle]) save_subtitles(video, [subtitle], directory=temp_folder, encoding=encoding if encoding else None) subtitle_path = xbmc.translatePath(get_subtitle_path(video.name, subtitle.language)).decode('utf-8') xbmc.log('temp_folder: %s' % temp_folder, level=xbmc.LOGDEBUG) xbmc.log('subtitle_path: %s' % subtitle_path, level=xbmc.LOGDEBUG) temp_path = os.path.join(temp_folder, os.path.basename(subtitle_path)) xbmc.log('temp_path: %s' % temp_path, level=xbmc.LOGDEBUG) self.post_process(temp_path, subtitle.language, encoding) sub_basename = os.path.basename(subtitle_path) sub_url = temp_path xbmc.log('Subtitle url %s' % sub_url, xbmc.LOGDEBUG) listitem = xbmcgui.ListItem(label2=sub_basename) xbmcplugin.addDirectoryItem(handle=self.handle, url=sub_url, listitem=listitem, isFolder=False)
def download(id): """ View function for downloading precise subtitles """ try: name = session.get('name') language = session.get('language') video = find_video(name) subs = list_best_subtitles(video, language) sub = subs[id][0] download_subtitles([sub]) response = make_response(sub.content) response.headers["Content-Disposition"] = "attachment; filename="+video.name+".srt" return response except IndexError: flash('Download impossible, no subtitles found', 'danger') return redirect(url_for('index')) except ValueError: flash('You have to enter a video name before attempting to download a subtitle', 'danger') return redirect(url_for('index'))
def _search_subtitles(video, lang, best_only): log.debug("Searching for subtitles") # Determine language language = babelfish.Language.fromietf(lang) if not language: log.error("Invalid language '%s' specified" % lang) return # Determine if language alpha2 code suffix is needed in srt file name (f.e. <episode_name>.nl.srt) single = False if lang == autosubliminal.DEFAULTLANGUAGE and not autosubliminal.DEFAULTLANGUAGESUFFIX: single = True # Get min match score if isinstance(video, Episode): min_score = autosubliminal.SHOWMINMATCHSCORE elif isinstance(video, Movie): min_score = autosubliminal.MOVIEMINMATCHSCORE else: log.error("Invalid video found '%s'" % video) return # Search for subtitles videos = {video} languages = {language} if best_only: # Download the best subtitle with min_score (without saving it in to file) subtitles = subliminal.download_best_subtitles(videos, languages, min_score=min_score, hearing_impaired=utils.include_hearing_impaired(), only_one=True, providers=autosubliminal.SUBLIMINALPROVIDERLIST, provider_configs=autosubliminal.SUBLIMINALPROVIDERCONFIGS) else: # Download all subtitles with default min score (without saving it to file) subtitles = subliminal.list_subtitles(videos, languages, providers=autosubliminal.SUBLIMINALPROVIDERLIST) subliminal.download_subtitles(subtitles[video]) return subtitles, language, single
def test_download_subtitles_languages(self): results = download_subtitles( 'Dexter/Season 04/S04E08 - Road Kill - 720p BluRay.mkv', languages=['en'], cache_dir=cache_dir, multi=True, force=False, services=['subtitulos', 'tvsubtitles']) self.assertTrue(len(results) == 1) for _, subtitles in results.iteritems(): self.assertTrue(len(subtitles) == 1) for subtitle in subtitles: os.remove(subtitle.path)
def searchSingle(self, group): if self.isDisabled(): return available_languages = sum(group['subtitle_language'].itervalues(), []) downloaded = [] for lang in self.getLanguages(): if lang not in available_languages: download = subliminal.download_subtitles(group['files']['movie'], multi = True, force = False, languages = [lang], services = self.services, cache_dir = Env.get('cache_dir')) downloaded.extend(download) for d_sub in downloaded: group['files']['subtitle'].add(d_sub.path) group['subtitle_language'][d_sub.path] = [d_sub.language]
SERVICES = ['addic7ed', 'bierdopje', 'opensubtitles'] CACHE_DIR = "C:\\change\\your\\path_pls\\" #=========== logging.basicConfig(filename=LOGFILE, format="%(asctime)s -\ %(levelname)s:%(message)s", level=logging.DEBUG) if len(sys.argv) < 2: logging.info('Too few arguments. Please let SickBeard call this script.') sys.exit() fileName = os.path.basename(sys.argv[2]) filePath = os.path.split(sys.argv[1])[0] + "\\" + fileName #processed Filepath proc_filePath = sys.argv[1] #processed Filepath replaced extension with srt proc_filePath_srt = proc_filePath[:len(proc_filePath)-3] + "srt" logging.info("got filename: " + filePath) logging.info("Processed srt filepath: " + proc_filePath_srt) logging.info("initiating subliminal") logging.info(subliminal.download_subtitles(filePath, LANGUAGES, SERVICES, cache_dir=CACHE_DIR)) #replace video filename extension with srt, srt file is in SickBeard root folder, move it to the processed TV folder with processed filename fileName_srt = fileName[:-3] + "srt" logging.info("Moving srt file: " + fileName_srt + " to folder: " + proc_filePath_srt) shutil.move(fileName_srt, proc_filePath_srt)
def downloadSubtitles(self): cwd = os.path.abspath(projectDir + '/' + SubtitlePath) os.chdir(cwd) sub = subliminal.download_subtitles(os.path.basename(self.filePath), [SubtitleLanguageISO], cache_dir="/tmp", services=self.services)
def run(self): # TODO: Put that in the __init__ before starting the thread? if not SUBLIMINAL_SUPPORT or not sickbeard.USE_SUBTITLES: logger.log(u"No subtitles support of subtitles support disabled", logger.DEBUG) return if len(sickbeard.subtitles.getEnabledPluginList()) < 2: logger.log( u"Not enough plugins selected. At least 2 plugins are required to search subtitles in the background", logger.ERROR, ) return logger.log(u"Checking for subtitles", logger.MESSAGE) # get episodes on which we want subtitles # criteria is: # - show subtitles = 1 # - episode subtitles != config wanted languages or SINGLE (depends on config multi) # - search count < 2 and diff(airdate, now) > 1 week : now -> 1d # - search count < 7 and diff(airdate, now) <= 1 week : now -> 4h -> 8h -> 16h -> 1d -> 1d -> 1d myDB = db.DBConnection() today = datetime.date.today().toordinal() # you have 5 minutes to understand that one. Good luck sqlResults = myDB.select( "SELECT s.show_name, e.showid, e.season, e.episode, e.subtitles_searchcount AS searchcount, e.subtitles_lastsearch AS lastsearch, e.location, (? - e.airdate) AS airdate_daydiff FROM tv_episodes AS e INNER JOIN tv_shows AS s ON (e.showid = s.tvdb_id) WHERE s.subtitles = 1 AND e.subtitles NOT LIKE (?) AND ((e.subtitles_searchcount <= 2 AND (? - e.airdate) > 7) OR (e.subtitles_searchcount <= 7 AND (? - e.airdate) <= 7)) AND (e.status IN (" + ",".join([str(x) for x in Quality.DOWNLOADED + [ARCHIVED]]) + ") OR (e.status IN (" + ",".join([str(x) for x in Quality.SNATCHED + Quality.SNATCHED_PROPER]) + ') AND e.location != ""))', [today, wantedLanguages(True), today, today], ) locations = [] toRefresh = [] rules = self._getRules() now = datetime.datetime.now() for epToSub in sqlResults: if not ek.ek(os.path.isfile, epToSub["location"]): logger.log( u"Episode file does not exist, cannot download subtitles for episode %dx%d of show %s" % (epToSub["season"], epToSub["episode"], epToSub["show_name"]), logger.DEBUG, ) continue try: lastsearch = datetime.datetime.strptime(epToSub["lastsearch"], "%Y-%m-%d %H:%M:%S") except: lastsearch = datetime.datetime.strptime(epToSub["lastsearch"], "%Y-%m-%d %H:%M:%S.%f") # Old shows rule if ( epToSub["airdate_daydiff"] > 7 and epToSub["searchcount"] < 2 and (now - lastsearch) > datetime.timedelta(hours=rules["old"][epToSub["searchcount"]]) ): logger.log( u"Downloading subtitles for episode %dx%d of show %s" % (epToSub["season"], epToSub["episode"], epToSub["show_name"]), logger.DEBUG, ) locations.append(epToSub["location"].encode("ascii", "ignore")) toRefresh.append((epToSub["showid"], epToSub["season"], epToSub["episode"])) continue # Recent shows rule if ( epToSub["airdate_daydiff"] <= 7 and epToSub["searchcount"] < 7 and (now - lastsearch) > datetime.timedelta(hours=rules["new"][epToSub["searchcount"]]) ): logger.log( u"Downloading subtitles for episode %dx%d of show %s" % (epToSub["season"], epToSub["episode"], epToSub["show_name"]), logger.DEBUG, ) locations.append(epToSub["location"].encode("ascii", "ignore")) toRefresh.append((epToSub["showid"], epToSub["season"], epToSub["episode"])) continue # Not matching my rules # logger.log('Do not match criteria to get downloaded: %s - %dx%d' % (epToSub['showid'], epToSub['season'], epToSub['episode']), logger.DEBUG) # stop here if we don't have subtitles to download if not locations: logger.log(u"No subtitles to download", logger.MESSAGE) return # download subtitles subtitles = subliminal.download_subtitles( paths=locations, languages=sickbeard.SUBTITLES_LANGUAGES, services=sickbeard.subtitles.getEnabledPluginList(), force=False, multi=sickbeard.SUBTITLES_MULTI, cache_dir=sickbeard.CACHE_DIR, max_depth=3, scan_filter=None, order=None, ) for subtitle in subtitles: helpers.chmodAsParent(subtitle.path) if subtitles: logger.log(u"Downloaded %d subtitles" % len(subtitles), logger.MESSAGE) else: logger.log(u"No subtitles found", logger.MESSAGE) # refresh each show self._refreshShows(toRefresh, now)
format="%(asctime)s -\ %(levelname)s:%(message)s", level=logging.DEBUG) if len(sys.argv) < 2: logging.info('Too few arguments. Please let SickBeard call this script.') sys.exit() fileName = os.path.basename(sys.argv[2]) filePath = os.path.split(sys.argv[1])[0] + "\\" + fileName #processed Filepath proc_filePath = sys.argv[1] #processed Filepath replaced extension with srt proc_filePath_srt = proc_filePath[:len(proc_filePath) - 3] + "srt" logging.info("got filename: " + filePath) logging.info("Processed srt filepath: " + proc_filePath_srt) logging.info("initiating subliminal") logging.info( subliminal.download_subtitles(filePath, LANGUAGES, SERVICES, cache_dir=CACHE_DIR)) #replace video filename extension with srt, srt file is in SickBeard root folder, move it to the processed TV folder with processed filename fileName_srt = fileName[:-3] + "srt" logging.info("Moving srt file: " + fileName_srt + " to folder: " + proc_filePath_srt) shutil.move(fileName_srt, proc_filePath_srt)
def manual_download_subtitle(path, language, hi, subtitle, provider, providers_auth, sceneName, media_type): logging.debug('BAZARR Manually downloading subtitles for this file: ' + path) if hi == "True": hi = True else: hi = False subtitle = pickle.loads(codecs.decode(subtitle.encode(), "base64")) if media_type == 'series': type_of_score = 360 elif media_type == 'movie': type_of_score = 120 use_scenename = settings.general.getboolean('use_scenename') use_postprocessing = settings.general.getboolean('use_postprocessing') postprocessing_cmd = settings.general.postprocessing_cmd language = alpha3_from_alpha2(language) if language == 'pob': lang_obj = Language('por', 'BR') else: lang_obj = Language(language) try: if sceneName is None or use_scenename is False: used_sceneName = False video = scan_video(path) else: used_sceneName = True video = Video.fromname(sceneName) except Exception as e: logging.exception("BAZARR Error trying to get video information for this file: " + path) pass else: try: download_subtitles([subtitle], providers=provider, provider_configs=providers_auth) logging.debug('BAZARR Subtitles file downloaded for this file:' + path) except Exception as e: logging.exception('BAZARR Error downloading subtitles for this file ' + path) return None else: single = settings.general.getboolean('single_language') try: score = round(float(compute_score(subtitle, video, hearing_impaired=hi)) / type_of_score * 100, 2) if used_sceneName == True: video = scan_video(path) if single is True: result = save_subtitles(video, [subtitle], single=True, encoding='utf-8') else: result = save_subtitles(video, [subtitle], encoding='utf-8') except Exception as e: logging.exception('BAZARR Error saving subtitles file to disk for this file:' + path) return None else: if len(result) > 0: downloaded_provider = result[0].provider_name downloaded_language = language_from_alpha3(result[0].language.alpha3) downloaded_language_code2 = alpha2_from_alpha3(result[0].language.alpha3) downloaded_language_code3 = result[0].language.alpha3 downloaded_path = get_subtitle_path(path, downloaded_language_code2) logging.debug('BAZARR Subtitles file saved to disk: ' + downloaded_path) message = downloaded_language + " subtitles downloaded from " + downloaded_provider + " with a score of " + unicode(score) + "% using manual search." if use_postprocessing is True: command = pp_replace(postprocessing_cmd, path, downloaded_path, downloaded_language, downloaded_language_code2, downloaded_language_code3) try: if os.name == 'nt': codepage = subprocess.Popen("chcp", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # wait for the process to terminate out_codepage, err_codepage = codepage.communicate() encoding = out_codepage.split(':')[-1].strip() process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # wait for the process to terminate out, err = process.communicate() if os.name == 'nt': out = out.decode(encoding) except: if out == "": logging.error('BAZARR Post-processing result for file ' + path + ' : Nothing returned from command execution') else: logging.error('BAZARR Post-processing result for file ' + path + ' : ' + out) else: if out == "": logging.info('BAZARR Post-processing result for file ' + path + ' : Nothing returned from command execution') else: logging.info('BAZARR Post-processing result for file ' + path + ' : ' + out) return message else: logging.error("BAZARR Tried to manually download a subtitles for file: " + path + " but we weren't able to do (probably throttled by " + str(subtitle.provider_name) + ". Please retry later or select a subtitles from another provider.") return None logging.debug('BAZARR Ended manually downloading subtitles for file: ' + path)
# configure the path to scan pathToScan = config['DEFAULT']['pathToScan'] # scan for videos newer than 2 weeks and their existing subtitles in a folder videos = scan_videos(pathToScan, age=timedelta(days=30)) logger.info('Analyse video % s ' % (videos)) # Download all shooters shooter_providers = ['shooter'] shooter_subtitles = list_subtitles(videos, {Language('zho')}, providers=shooter_providers) for movie, subtitles in shooter_subtitles.items(): try: download_subtitles(subtitles) for subtitle in subtitles: if subtitle.content is None: logger.error('Skipping subtitle %r: no content' % subtitle) continue # create subtitle path subtitle_path = get_subtitle_path(movie.name, subtitle.language) filename_language, file_extension = os.path.splitext(subtitle_path) filename, language = os.path.splitext(filename_language) subtitle_path = "%s.shooter-%s%s%s" % ( filename, str( subtitles.index(subtitle)), language, file_extension) # save content as is or in the specified encoding with io.open(subtitle_path, 'wb') as f: f.write(subtitle.content)
def download_subtitle(path, language, hi, providers, providers_auth, sceneName, media_type): if hi == "True": hi = True else: hi = False language_set = set() if language == 'pob': language_set.add(Language('por', 'BR')) else: language_set.add(Language(language)) use_scenename = get_general_settings()[9] minimum_score = get_general_settings()[8] minimum_score_movie = get_general_settings()[22] use_postprocessing = get_general_settings()[10] postprocessing_cmd = get_general_settings()[11] try: if sceneName == "None" or use_scenename is False: used_sceneName = False video = scan_video(path) else: used_sceneName = True video = Video.fromname(sceneName) except Exception as e: logging.exception("Error trying to get video information for this file: " + path) else: if media_type == "movie": max_score = 120.0 elif media_type == "series": max_score = 360.0 try: with AsyncProviderPool(max_workers=None, providers=providers, provider_configs=providers_auth) as p: subtitles = p.list_subtitles(video, language_set) except Exception as e: logging.exception("Error trying to get subtitle list from provider") else: subtitles_list = [] sorted_subtitles = sorted([(s, compute_score(s, video, hearing_impaired=hi)) for s in subtitles], key=operator.itemgetter(1), reverse=True) for s, preliminary_score in sorted_subtitles: if media_type == "movie": if (preliminary_score / max_score * 100) < int(minimum_score_movie): continue matched = set(s.get_matches(video)) if hi == s.hearing_impaired: matched.add('hearing_impaired') not_matched = set(score.movie_scores.keys()) - matched required = set(['title']) if any(elem in required for elem in not_matched): continue elif media_type == "series": if (preliminary_score / max_score * 100) < int(minimum_score): continue matched = set(s.get_matches(video)) if hi == s.hearing_impaired: matched.add('hearing_impaired') not_matched = set(score.episode_scores.keys()) - matched required = set(['series', 'season', 'episode']) if any(elem in required for elem in not_matched): continue subtitles_list.append(s) if len(subtitles_list) > 0: best_subtitle = subtitles_list[0] download_subtitles([best_subtitle], providers=providers, provider_configs=providers_auth) try: calculated_score = round(float(compute_score(best_subtitle, video, hearing_impaired=hi)) / max_score * 100, 2) if used_sceneName == True: video = scan_video(path) single = get_general_settings()[7] if single is True: result = save_subtitles(video, [best_subtitle], single=True, encoding='utf-8') else: result = save_subtitles(video, [best_subtitle], encoding='utf-8') except Exception as e: logging.exception('Error saving subtitles file to disk.') return None else: if len(result) > 0: downloaded_provider = result[0].provider_name downloaded_language = language_from_alpha3(result[0].language.alpha3) downloaded_language_code2 = alpha2_from_alpha3(result[0].language.alpha3) downloaded_language_code3 = result[0].language.alpha3 downloaded_path = get_subtitle_path(path, language=language_set) if used_sceneName == True: message = downloaded_language + " subtitles downloaded from " + downloaded_provider + " with a score of " + unicode(calculated_score) + "% using this scene name: " + sceneName else: message = downloaded_language + " subtitles downloaded from " + downloaded_provider + " with a score of " + unicode(calculated_score) + "% using filename guessing." if use_postprocessing is True: command = pp_replace(postprocessing_cmd, path, downloaded_path, downloaded_language, downloaded_language_code2, downloaded_language_code3) try: if os.name == 'nt': codepage = subprocess.Popen("chcp", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # wait for the process to terminate out_codepage, err_codepage = codepage.communicate() encoding = out_codepage.split(':')[-1].strip() process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # wait for the process to terminate out, err = process.communicate() if os.name == 'nt': out = out.decode(encoding) except: if out == "": logging.error('Post-processing result for file ' + path + ' : Nothing returned from command execution') else: logging.error('Post-processing result for file ' + path + ' : ' + out) else: if out == "": logging.info('Post-processing result for file ' + path + ' : Nothing returned from command execution') else: logging.info('Post-processing result for file ' + path + ' : ' + out) return message else: return None else: return None
def manual_download_subtitle(path, language, hi, subtitle, provider, providers_auth, sceneName, media_type): if hi == "True": hi = True else: hi = False subtitle = pickle.loads(codecs.decode(subtitle.encode(), "base64")) if media_type == 'series': type_of_score = 360 elif media_type == 'movie': type_of_score = 120 use_scenename = get_general_settings()[9] use_postprocessing = get_general_settings()[10] postprocessing_cmd = get_general_settings()[11] language = alpha3_from_alpha2(language) if language == 'pob': lang_obj = Language('por', 'BR') else: lang_obj = Language(language) try: if sceneName is None or use_scenename is False: used_sceneName = False video = scan_video(path) else: used_sceneName = True video = Video.fromname(sceneName) except Exception as e: logging.exception('Error trying to extract information from this filename: ' + path) return None else: try: best_subtitle = subtitle download_subtitles([best_subtitle], providers=provider, provider_configs=providers_auth) except Exception as e: logging.exception('Error downloading subtitles for ' + path) return None else: single = get_general_settings()[7] try: score = round(float(compute_score(best_subtitle, video, hearing_impaired=hi)) / type_of_score * 100, 2) if used_sceneName == True: video = scan_video(path) if single is True: result = save_subtitles(video, [best_subtitle], single=True, encoding='utf-8') else: result = save_subtitles(video, [best_subtitle], encoding='utf-8') except Exception as e: logging.exception('Error saving subtitles file to disk.') return None else: if len(result) > 0: downloaded_provider = result[0].provider_name downloaded_language = language_from_alpha3(result[0].language.alpha3) downloaded_language_code2 = alpha2_from_alpha3(result[0].language.alpha3) downloaded_language_code3 = result[0].language.alpha3 downloaded_path = get_subtitle_path(path, language=lang_obj) message = downloaded_language + " subtitles downloaded from " + downloaded_provider + " with a score of " + unicode(score) + "% using manual search." if use_postprocessing is True: command = pp_replace(postprocessing_cmd, path, downloaded_path, downloaded_language, downloaded_language_code2, downloaded_language_code3) try: if os.name == 'nt': codepage = subprocess.Popen("chcp", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # wait for the process to terminate out_codepage, err_codepage = codepage.communicate() encoding = out_codepage.split(':')[-1].strip() process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # wait for the process to terminate out, err = process.communicate() if os.name == 'nt': out = out.decode(encoding) except: if out == "": logging.error('Post-processing result for file ' + path + ' : Nothing returned from command execution') else: logging.error('Post-processing result for file ' + path + ' : ' + out) else: if out == "": logging.info('Post-processing result for file ' + path + ' : Nothing returned from command execution') else: logging.info('Post-processing result for file ' + path + ' : ' + out) return message else: return None
def download_single_subtitle(subtitle): download_subtitles([subtitle])
def download(filename, results, languages, config, providers=None): '''Downloads the best matches for each of the input languages found Parameters: filename (str): Search subtitles for a given file on the provided languages results (dict): A dictionary mapping the languages asked in the input with subtitles found on different providers as a result from calling :py:func:`search_subtitles`. languages (list): Defines the languages of your preference. Each language should be an object of type :py:class:`babelfish.Language`. Subtitles for these languages will be downloaded and organized following an 2-character english-based language encoding convention (ISO 639-3), possibily with the contry code attached (e.g. "pt-BR", if one is available. config (dict): A dictionary where the keys represent the various providers available and the values correspond to dictionaries with keyword-argument parameters that will be used on their constructor providers (:py:class:`list`, optional): A list of strings determining providers to use for the query. If not set, then use all available providers. ''' def _check_or_reset(s): '''Checks if a subtitle is of type SRT, otherwise, resets it''' try: if s.content is None: return # get a string representation of subtitles srt = None r = chardet.detect(s.content) enc = r['encoding'] if enc is not None: logger.info('Decoding subtitles from `%s\'', enc) content = s.content.decode(encoding=enc) else: logger.warn('Cannot detect subtitle encoding - ignoring errors') content = s.content.decode(errors='ignore') # checks decoding srt = pysrt.SubRipFile.from_string(content) if not srt: logger.warn('Discarding contents of subtitle: not parseable') s.content = None return # if everything checks, re-write subtitles in utf-8 srt.clean_indexes() srt.eol = os.linesep buf = six.StringIO() srt.write_into(buf) s.content = buf.getvalue().encode(encoding='UTF-8') s.encoding = 'utf-8' except Exception as e: logger.warn('Discarding contents of subtitle: %s', e) s.content = None to_download = [] lang_download = [] for lang in languages: if not results[lang]: logger.error('Did not find any subtitle for language `%s\'', lang) continue lang_download.append(lang) logger.info('Scheduling download subtitle for language `%s%s\' ' \ 'from `%s\' (score: %d)', lang.alpha2, '-%s' % lang.country.alpha2.lower() if lang.country else '', results[lang][0][1].provider_name, results[lang][0][0]) to_download.append(results[lang].pop(0)[1]) # if you get at this point, we can download the subtitle logger.info('Downloading subtitles...') # checks if the download was successful, otherwise, tries the next sub while not all([z.content for z in to_download]): subliminal.download_subtitles(to_download, subliminal.core.ProviderPool, providers=providers, provider_configs=config) # checks the subtitle is in SRT format for k in to_download: _check_or_reset(k) for k, (lang, dl) in enumerate(zip(lang_download, to_download)): if not dl.content: logger.warn('Contents for subtitle for language `%s%s\' where not ' \ 'downloaded from `%s\'', lang.alpha2, '-%s' % lang.country.alpha2.lower() if lang.country else '', dl.provider_name) if results[lang]: #there are still some to consider to_try = results[lang].pop(0) logger.info('Trying next subtitle in list, with score=%d', to_try[0]) to_download[k] = to_try[1] # stores the subtitles side-by-side with the movie logger.info('Saving subtitles in UTF-8 encoding...') video = _get_video(filename) subliminal.save_subtitles(video, to_download, encoding='UTF-8')