def searchLibrary(self): # Get all active and online movies db = get_session() library = db.query(Library).all() done_status = fireEvent('status.get', 'done', single=True) for movie in library.movies: for release in movie.releases: # get releases and their movie files if release.status_id is done_status.get('id'): files = [] for file in release.files.filter( FileType.status.has(identifier='movie')).all(): files.append(file.path) # get subtitles for those files subliminal.list_subtitles(files, cache_dir=Env.get('cache_dir'), multi=True, languages=self.getLanguages(), services=self.services)
def searchLibrary(self): # Get all active and online movies db = get_session() library = db.query(Library).all() done_status = fireEvent('status.get', 'done', single=True) for movie in library.movies: for release in movie.releases: # get releases and their movie files if release.status_id is done_status.get('id'): files = [] for file in release.files.filter( FileType.status.has(identifier='movie')).all(): files.append(file.path) # get subtitles for those files subliminal.list_subtitles( files, cache_dir=Env.get('cache_dir'), multi=True, languages=self.getLanguages(), services=self.services)
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 get_subtitle_list(movie_title, source_language_code, destination_language_code): video = Video.fromname(movie_title) return list_subtitles( [video], { Language.fromalpha2(source_language_code), Language.fromalpha2(destination_language_code) }, FreePool)[video]
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 get_subtitles(fname): """ returns a list of subtitles given a path """ #search using info from the actual file subs = subliminal.list_subtitles(str(fname), ["en"], force=True, cache_dir="/tmp/") subs = subs.values()[0] #remove everything that is not a movie subs = filter(is_valid_subtitle, subs) if subs: return subs subs = subliminal.list_subtitles(str(fname.name), ["en"], force=True, cache_dir="/tmp/") subs = subs.values()[0] #remove everything that is not a movie subs = filter(is_valid_subtitle, subs) return subs
def list_best_subtitles(video, language): """ Input: object video, language 3-code identifier Output: returns a dict with subtitle and corresponding score, in decreasing score order Throws: ValueError """ subtitles = list_subtitles([video], {Language(language)}) dic = dict() for s in subtitles[video]: dic[s] = compute_score(s.get_matches(video), video) sorted_d = sorted(dic.items(), key=operator.itemgetter(1), reverse=True) return sorted_d
def test_list_subtitles_providers(episodes, mock_providers): video = episodes['bbt_s07e05'] languages = {Language('eng')} subtitles = list_subtitles({video}, languages, providers=['addic7ed']) # test providers assert provider_manager['addic7ed'].plugin.list_subtitles.called assert not provider_manager['opensubtitles'].plugin.list_subtitles.called assert not provider_manager['podnapisi'].plugin.list_subtitles.called assert not provider_manager['thesubdb'].plugin.list_subtitles.called assert not provider_manager['tvsubtitles'].plugin.list_subtitles.called # test result assert len(subtitles) == 1 assert sorted(subtitles[episodes['bbt_s07e05']]) == ['addic7ed']
def test_list_subtitles_episode_no_hash(episodes, mock_providers): video = episodes['dallas_s01e03'] languages = {Language('eng')} subtitles = list_subtitles({video}, languages) # test providers assert provider_manager['addic7ed'].plugin.list_subtitles.called assert provider_manager['opensubtitles'].plugin.list_subtitles.called assert provider_manager['podnapisi'].plugin.list_subtitles.called assert not provider_manager['thesubdb'].plugin.list_subtitles.called assert provider_manager['tvsubtitles'].plugin.list_subtitles.called # test result assert len(subtitles) == 1 assert sorted(subtitles[episodes['dallas_s01e03']]) == ['addic7ed', 'opensubtitles', 'podnapisi', 'tvsubtitles']
def test_list_subtitles_no_language(episodes, mock_providers): video = episodes['dallas_s01e03'] languages = {Language('eng')} video.subtitle_languages = languages subtitles = list_subtitles({video}, languages) # test providers assert not provider_manager['addic7ed'].plugin.list_subtitles.called assert not provider_manager['opensubtitles'].plugin.list_subtitles.called assert not provider_manager['podnapisi'].plugin.list_subtitles.called assert not provider_manager['thesubdb'].plugin.list_subtitles.called assert not provider_manager['tvsubtitles'].plugin.list_subtitles.called # test result assert len(subtitles) == 0
def test_list_subtitles_movie(movies, mock_providers): video = movies['man_of_steel'] languages = {Language('eng')} subtitles = list_subtitles({video}, languages) # test providers assert not provider_manager['addic7ed'].plugin.list_subtitles.called assert provider_manager['opensubtitles'].plugin.list_subtitles.called assert provider_manager['podnapisi'].plugin.list_subtitles.called assert provider_manager['thesubdb'].plugin.list_subtitles.called assert not provider_manager['tvsubtitles'].plugin.list_subtitles.called # test result assert len(subtitles) == 1 assert sorted(subtitles[movies['man_of_steel']]) == ['opensubtitles', 'podnapisi', 'thesubdb']
def test_list_subtitles_movie(movies, mock_providers): video = movies['man_of_steel'] languages = {Language('eng')} subtitles = list_subtitles({video}, languages) # test providers assert not provider_manager['addic7ed'].plugin.list_subtitles.called assert provider_manager['opensubtitles'].plugin.list_subtitles.called assert provider_manager['podnapisi'].plugin.list_subtitles.called assert not provider_manager['subscenter'].plugin.list_subtitles.called assert provider_manager['thesubdb'].plugin.list_subtitles.called assert not provider_manager['tvsubtitles'].plugin.list_subtitles.called # test result assert len(subtitles) == 1 assert sorted(subtitles[movies['man_of_steel']]) == ['opensubtitles', 'podnapisi', 'thesubdb']
def test_list_subtitles_episode_no_hash(episodes, mock_providers): video = episodes['dallas_s01e03'] languages = {Language('eng')} subtitles = list_subtitles({video}, languages) # test providers assert provider_manager['addic7ed'].plugin.list_subtitles.called assert provider_manager['opensubtitles'].plugin.list_subtitles.called assert provider_manager['podnapisi'].plugin.list_subtitles.called assert not provider_manager['thesubdb'].plugin.list_subtitles.called assert provider_manager['tvsubtitles'].plugin.list_subtitles.called # test result assert len(subtitles) == 1 assert sorted(subtitles[episodes['dallas_s01e03']]) == [ 'addic7ed', 'opensubtitles', 'podnapisi', 'tvsubtitles' ]
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 _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_list_subtitles(self): results = list_subtitles(test_video, languages=['en', 'fr'], cache_dir=cache_dir) self.assertTrue(len(results) > 0)
def test_list_subtitles_movie_0_por_br(self): videos = [MOVIES[0]] languages = {Language('por', 'BR')} subtitles = list_subtitles(videos, languages) self.assertTrue(len(subtitles) == len(videos)) self.assertTrue(len(subtitles[videos[0]]) > 0)
def test_list_subtitles_episodes(self): videos = [EPISODES[0], EPISODES[1]] languages = {Language('eng'), Language('fra')} subtitles = list_subtitles(videos, languages) self.assertTrue(len(subtitles) == len(videos)) self.assertTrue(len(subtitles[videos[0]]) > 0)
def search(filename, languages, config, providers=None): '''Search subtitles for a given filename Parameters: filename (str): Search subtitles for a given file on the provided languages 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. Returns: dict: A dictionary mapping the languages asked in the input with subtitles found on different providers. ''' video = _get_video(filename) # call APIs once logger.info('Contacting subtitle providers...') subtitles = subliminal.list_subtitles([video], set(languages), subliminal.core.ProviderPool, providers=providers, provider_configs=config) def _score(st): try: return subliminal.compute_score(st, video) except Exception as e: logger.warn('subliminal.compute_score() returned an error: %s', e) return 0 def _matches(st): try: return st.get_matches(video) except Exception as e: logger.warn('subliminal.get_matches() returned an error: %s', e) return ['??'] # sort by language and then by score logger.info('Sorting subtitles by score...') retval = {} for k in languages: tmp = [(_score(l), l, _matches(l)) for l in subtitles[video] \ if l.language == k] retval[k] = sorted(tmp, key=lambda x: x[0], reverse=True) return retval
def manual_search(path, language, hi, providers, providers_auth, sceneName, title, media_type): logging.debug('BAZARR Manually searching subtitles for this file: ' + path) final_subtitles = [] if hi == "True": hi = True else: hi = False language_set = set() for lang in ast.literal_eval(language): lang = alpha3_from_alpha2(lang) if lang == 'pob': language_set.add(Language('por', 'BR')) else: language_set.add(Language(lang)) use_scenename = settings.general.getboolean('use_scenename') minimum_score = settings.general.minimum_score minimum_score_movie = settings.general.minimum_score_movie use_postprocessing = settings.general.getboolean('use_postprocessing') postprocessing_cmd = settings.general.postprocessing_cmd video = get_video(path, title, sceneName, use_scenename, providers=providers, media_type=media_type) if video: min_score, max_score, scores = get_scores( video, media_type, min_score_movie_perc=int(minimum_score_movie), min_score_series_perc=int(minimum_score)) try: if providers: subtitles = list_subtitles([video], language_set, providers=providers, provider_configs=providers_auth, pool_class=provider_pool(), throttle_callback=provider_throttle, language_hook=None) # fixme else: subtitles = [] logging.info("BAZARR All providers are throttled") except Exception as e: logging.exception( "BAZARR Error trying to get subtitle list from provider for this file: " + path) else: subtitles_list = [] for s in subtitles[video]: try: matches = s.get_matches(video) except AttributeError: continue # skip wrong season/episodes if media_type == "series": can_verify_series = True if not s.hash_verifiable and "hash" in matches: can_verify_series = False if can_verify_series and not { "series", "season", "episode" }.issubset(matches): logging.debug( u"BAZARR Skipping %s, because it doesn't match our series/episode", s) continue score = compute_score(matches, s, video, hearing_impaired=hi) not_matched = scores - matches s.score = score # if score < min_score: # continue subtitles_list.append( dict(score=round((score / max_score * 100), 2), language=alpha2_from_alpha3(s.language.alpha3), hearing_impaired=str(s.hearing_impaired), provider=s.provider_name, subtitle=codecs.encode( pickle.dumps(s.make_picklable()), "base64").decode(), url=s.page_link, matches=list(matches), dont_matches=list(not_matched))) final_subtitles = sorted(subtitles_list, key=lambda x: x['score'], reverse=True) logging.debug('BAZARR ' + str(len(final_subtitles)) + " subtitles have been found for this file: " + path) logging.debug('BAZARR Ended searching subtitles for this file: ' + path) return final_subtitles
def test_list_subtitles_movie_0_por_br(self): videos = [MOVIES[0]] languages = {Language('por', 'BR')} subtitles = list_subtitles(videos, languages) self.assertEqual(len(subtitles), len(videos)) self.assertGreater(len(subtitles[videos[0]]), 0)
def test_list_subtitles_episodes(self): videos = [EPISODES[0], EPISODES[1]] languages = {Language('eng'), Language('fra')} subtitles = list_subtitles(videos, languages) self.assertEqual(len(subtitles), len(videos)) self.assertGreater(len(subtitles[videos[0]]), 0)
def test_list_subtitles_episodes(self): videos = [EPISODES[0], EPISODES[1]] languages = {Language("eng"), Language("fra")} subtitles = list_subtitles(videos, languages) self.assertTrue(len(subtitles) == len(videos)) self.assertTrue(len(subtitles[videos[0]]) > 0)
def test_list_subtitles_movie_0_por_br(self): videos = [MOVIES[0]] languages = {Language("por", "BR")} subtitles = list_subtitles(videos, languages) self.assertTrue(len(subtitles) == len(videos)) self.assertTrue(len(subtitles[videos[0]]) > 0)
username=config['OPENSUBTITLE']['username'], password=config['OPENSUBTITLE']['password']) # configure the cache region.configure('dogpile.cache.dbm', arguments={'filename': 'cachefile.dbm'}) # 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(