def on_task_output(self, task, config): """ Configuration:: subliminal: languages: List of languages (3-letter ISO-639-3 code) in order of preference. At least one is required. alternatives: List of second-choice languages; subs will be downloaded but entries rejected. exact_match: Use file hash only to search for subs, otherwise Subliminal will try to guess by filename. """ if not task.accepted: log.debug('nothing accepted, aborting') return from babelfish import Language from dogpile.cache.exception import RegionAlreadyConfigured import subliminal try: subliminal.cache_region.configure('dogpile.cache.dbm', arguments={ 'filename': os.path.join( tempfile.gettempdir(), 'cachefile.dbm'), 'lock_factory': subliminal.MutexLock }) except RegionAlreadyConfigured: pass logging.getLogger("subliminal").setLevel(logging.CRITICAL) logging.getLogger("enzyme").setLevel(logging.WARNING) langs = set([Language(s) for s in config['languages']]) alts = set([Language(s) for s in config.get('alternatives', [])]) for entry in task.accepted: if not 'location' in entry: log.warning( 'Cannot act on entries that do not represent a local file.' ) elif not os.path.exists(entry['location']): entry.fail('file not found: %s' % entry['location']) elif not '$RECYCLE.BIN' in entry[ 'location']: # ignore deleted files in Windows shares try: video = subliminal.scan_video(entry['location']) msc = video.scores['hash'] if config['exact_match'] else 0 if langs & video.subtitle_languages: continue # subs for preferred lang(s) already exists elif subliminal.download_best_subtitles([video], langs, min_score=msc): log.info('Subtitles found for %s' % entry['location']) elif alts and (alts - video.subtitle_languages) and \ subliminal.download_best_subtitles([video], alts, min_score=msc): entry.fail( 'subtitles found for a second-choice language.') else: entry.fail('cannot find any subtitles for now.') except Exception as err: # don't want to abort the entire task for errors in a # single video file or for occasional network timeouts log.debug(err.message) entry.fail(err.message)
def com_meta_fetch_subtitle_batch(dir_name, sub_lang='eng'): """ # batch fetch subtitles """ # configure the cache subliminal.cache_region.configure('dogpile.cache.dbm', arguments={'filename': '/mediakraken/cache/cachefile.dbm'}) # scan for videos in the folder and their subtitles videos = subliminal.scan_videos(dir_name, subtitles=True, embedded_subtitles=True) # download subliminal.download_best_subtitles(videos, Language(sub_lang))
def find_file_subtitles(path): """ Finds subtitles for the given video file path. :param path: The path of the video file to find subtitles to. :return: The list of subtitles file paths, or None if a problem occurred. """ logger.info('Searching subtitles for file: {}'.format(path)) try: # Get required video information. video = subliminal.scan_video(path) other_languages = [] subtitle_results = [] for language, providers in LANGUAGES_MAP.items(): # Filter providers the user didn't ask for. if not providers: other_languages.append(language) else: current_result = subliminal.download_best_subtitles( {video}, languages={language}, providers=providers).values() if len(current_result) > 0: subtitle_results.extend(list(current_result)[0]) # Download all other languages. for language in other_languages: current_result = subliminal.download_best_subtitles( {video}, languages={language}).values() if len(current_result) > 0: subtitle_results.extend(list(current_result)[0]) # Handle results. if len(subtitle_results) == 0: logger.info('No subtitles were found. Moving on...') else: logger.info('Found {} subtitles. Saving files...'.format( len(subtitle_results))) # Save subtitles alongside the video file. results_list = list() for subtitles in subtitle_results: # Filter empty subtitles files. if subtitles.content is None: logger.debug( 'Skipping subtitle {}: no content'.format(subtitles)) continue subtitles_path = get_subtitle_path(video.name, subtitles.language) logger.info('Saving {} to: {}'.format(subtitles, subtitles_path)) open(subtitles_path, 'wb').write(subtitles.content) results_list.append(subtitles_path) return results_list except ValueError: # Subliminal raises a ValueError if the given file is not a video file. logger.info('Not a video file. Moving on...')
def on_task_output(self, task, config): """ Configuration:: subliminal: languages: List of languages (3-letter ISO-639-3 code) in order of preference. At least one is required. alternatives: List of second-choice languages; subs will be downloaded but entries rejected. exact_match: Use file hash only to search for subs, otherwise Subliminal will try to guess by filename. """ if not task.accepted: log.debug('nothing accepted, aborting') return from babelfish import Language from dogpile.cache.exception import RegionAlreadyConfigured import subliminal try: subliminal.cache_region.configure('dogpile.cache.dbm', arguments={'filename': os.path.join(tempfile.gettempdir(), 'cachefile.dbm'), 'lock_factory': subliminal.MutexLock}) except RegionAlreadyConfigured: pass logging.getLogger("subliminal").setLevel(logging.CRITICAL) logging.getLogger("enzyme").setLevel(logging.WARNING) langs = set([Language(s) for s in config['languages']]) alts = set([Language(s) for s in config.get('alternatives', [])]) for entry in task.accepted: if not 'location' in entry: log.warning('Cannot act on entries that do not represent a local file.') elif not os.path.exists(entry['location']): entry.fail('file not found: %s' % entry['location']) elif not '$RECYCLE.BIN' in entry['location']: # ignore deleted files in Windows shares try: video = subliminal.scan_video(entry['location']) msc = video.scores['hash'] if config['exact_match'] else 0 if langs & video.subtitle_languages: continue # subs for preferred lang(s) already exists elif subliminal.download_best_subtitles([video], langs, min_score=msc): log.info('Subtitles found for %s' % entry['location']) elif alts and (alts - video.subtitle_languages) and \ subliminal.download_best_subtitles([video], alts, min_score=msc): entry.fail('subtitles found for a second-choice language.') else: entry.fail('cannot find any subtitles for now.') except Exception as err: # don't want to abort the entire task for errors in a # single video file or for occasional network timeouts if err.args: msg = err.args[0] else: # Subliminal errors don't always have a message, just use the name msg = 'subliminal error: %s' % err.__class__.__name__ log.debug(msg) entry.fail(msg)
def get_subtitle(magnet, lang): print("Explorando torrent") lt_session = session() lt_session.listen_on(6881, 6891) params = {"save_path": "/tmp"} handle = add_magnet_uri(lt_session, magnet, params) lt_session.start_dht() print "esperando" while (not handle.has_metadata()): sleep(.1) print "esperando" info = handle.get_torrent_info() print "esperando", info files = info.files() biggest_file = ("", 0) for file_ in files: if file_.size > biggest_file[1]: biggest_file = [file_.path, file_.size] print("Adivinando data") filepath = biggest_file[0] guess = guess_video_info(filepath, info = ['filename']) video = Video.fromguess(filepath, guess) video.size = biggest_file[1] print("Bajando el subtitulo para lang = " + lang) subtitle = download_best_subtitles([video], {Language(lang)}, single=True) if not len(subtitle): subtitle = None else: subtitle = get_subtitle_path(video.name) lt_session.remove_torrent(handle) return subtitle
def import_subs(filename): if not core.GETSUBS: return try: subliminal.cache_region.configure('dogpile.cache.memory') except: pass languages = set() for item in core.SLANGUAGES: try: languages.add(Language(item)) except: pass if not languages: return logger.debug("Attempting to download subtitles for %s" %(filename), 'SUBTITLES') try: # subliminal.logger = subliminal.logging.getLogger('subliminal') # subliminal.logger.setLevel(subliminal.logging.DEBUG) # ch = subliminal.logging.StreamHandler() # ch.setLevel(subliminal.logging.DEBUG) # formatter = subliminal.logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') # ch.setFormatter(formatter) # subliminal.logger.addHandler(ch) video = subliminal.scan_video(filename, subtitles=True, embedded_subtitles=True) subtitles = subliminal.download_best_subtitles([video], languages, hearing_impaired=False) saved_subtitles = subliminal.save_subtitles(video, subtitles[video]) logger.debug("Saved subtitles:%s" %(saved_subtitles), 'SUBTITLES') except Exception as e: logger.error("Failed to download subtitles for %s due to: %s" %(filename, e), 'SUBTITLES')
def download_subs(videos, languages): """ Will scan for videos newer than one week and try to download subtitles in English and Spanish for them. Parameters ---------- videos: list of languages: list of babelfish.language """ nu_vids = [] for vid in videos: if len(vid.subtitle_languages) == 0: nu_vids.append(vid) # download try: subs = subliminal.download_best_subtitles(nu_vids, languages) except: raise log.info('Subs found:') log.info(subs) # save log.debug('Saving subtitle files.') subliminal.save_subtitles(subs, single=False)
def test_download_best_subtitles_hearing_impaired(self): videos = [MOVIES[0]] for video in videos: video.name = os.path.join(TEST_DIR, video.name.split(os.sep)[-1]) languages = {Language("eng")} subtitles = download_best_subtitles(videos, languages, hearing_impaired=True) self.assertTrue(subtitles[videos[0]][0].hearing_impaired == True)
def getsrt(source, srcPath=watchPath): try: video = scan_video(source) videos = [video] logging.debug(prelog + 'getting subtitles') subtitles = download_best_subtitles( videos, {Language('eng'), Language('fra')}, providers=None, provider_configs={ 'addic7ed': { 'username': '******', 'password': '******' }, 'opensubtitles': { 'username': '******', 'password': '******' } }) sourceDir = os.path.dirname(source) savePath = sourceDir.replace(srcPath, srtPath) if not os.path.isdir(savePath): logging.debug(prelog + 'creating srt directory ' + savePath) os.makedirs(savePath) save_subtitles(video, subtitles[video], directory=savePath) except: logging.exception(prelog) pass
def download(request): language_query = request.GET.get('l') video_query = request.GET.get('q') if language_query is None or video_query is None: response = JsonResponse( {'errors': [{ 'message': 'MISSING_PACK_OR_LANGUAGE' }]}) response.status_code = 400 return response language = Language(language_query) video = subliminal.Video.fromname(video_query) subtitles = subliminal.download_best_subtitles({video}, {language}) if len(subtitles[video]) == 0: response = HttpResponse("No subtitles found for %s" % video.name) else: content = subtitles[video][0].content response = HttpResponse(content, content_type='application/octet-stream') response[ 'Content-Disposition'] = 'attachment; filename=%s.srt' % basename( video.name) return response
def download_subtitles(movie_title): # Creating subliminal.video object using movie title. video = subliminal.Video.fromname(movie_title) print("Downloading subtitles for '", movie_title, "'...", sep='') # Downloading subtitles for created video object. If several are # available, subtitles with higher rating will be chosen. All available # providers are used for searching. best_subtitles = \ subliminal.download_best_subtitles({video}, {babelfish.Language('eng')}) if not best_subtitles[video]: print("No subtitles found for '", movie_title, "'...", sep='') return [] # This line can enable saving downloaded files for further use. Default # directory is the directory, where running script is located. # Note: when the script is running in non-sudo mode on Linux, # downloaded files will be saved in user Home directory. # subliminal.save_subtitles(video, [best_subtitles[video][0]]) # Converting list of subtitles to string, so pysrt module can then convert # it to its own format. subtitles_string = '' for item in [best_subtitles[video][0]]: subtitles_string += item.text # Converting string to list of strings without any SRT-special content # (text only) and returning it. return pysrt.from_string(subtitles_string)
def import_subs(filename): if not core.GETSUBS: return try: subliminal.cache_region.configure('dogpile.cache.memory') except: pass languages = set() for item in core.SLANGUAGES: try: languages.add(Language(item)) except: pass if not languages: return logger.debug("Attempting to download subtitles for %s" % (filename), 'SUBTITLES') try: video = subliminal.scan_video(filename, subtitles=True, embedded_subtitles=True) subtitles = subliminal.download_best_subtitles([video], languages, hearing_impaired=False) subliminal.save_subtitles(subtitles) except Exception as e: logger.error( "Failed to download subtitles for %s due to: %s" % (filename, e), 'SUBTITLES')
def test_download_best_subtitles_min_score(self): videos = [MOVIES[0]] for video in videos: video.name = os.path.join(TEST_DIR, os.path.split(video.name)[1]) languages = {Language('eng'), Language('fra')} subtitles = download_best_subtitles(videos, languages, min_score=1000) self.assertEqual(len(subtitles), 0)
def import_subs(filename): if not core.GETSUBS: return try: subliminal.region.configure('dogpile.cache.dbm', arguments={'filename': 'cachefile.dbm'}) except Exception: pass languages = set() for item in core.SLANGUAGES: try: languages.add(Language(item)) except Exception: pass if not languages: return logger.info('Attempting to download subtitles for {0}'.format(filename), 'SUBTITLES') try: video = subliminal.scan_video(filename) subtitles = subliminal.download_best_subtitles({video}, languages) subliminal.save_subtitles(video, subtitles[video]) for subtitle in subtitles[video]: subtitle_path = subliminal.subtitle.get_subtitle_path( video.name, subtitle.language) os.chmod(subtitle_path, 0o644) except Exception as e: logger.error( 'Failed to download subtitles for {0} due to: {1}'.format( filename, e), 'SUBTITLES')
def get_subtitle(magnet, lang): print("Obtaining subtitle (experimental, might take a while)") lt_session = session() params = {"save_path": "/tmp"} handle = add_magnet_uri(lt_session, magnet, params) while (not handle.has_metadata()): sleep(.1) info = handle.get_torrent_info() files = info.files() biggest_file = ("", 0) for file_ in files: if file_.size > biggest_file[1]: biggest_file = [file_.path, file_.size] print("Guessing data") filepath = biggest_file[0] guess = guess_video_info(filepath, info=['filename']) video = Video.fromguess(filepath, guess) video.size = biggest_file[1] print("Donwloading Subtitle") subtitle = download_best_subtitles([video], {Language(lang)}, single=True) if not len(subtitle): subtitle = None else: subtitle = get_subtitle_path(video.name) lt_session.remove_torrent(handle) return subtitle
def get_subtitle(magnet, lang): print("Obtaining subtitle (experimental, might take a while)") lt_session = session() params = {"save_path": "/tmp"} handle = add_magnet_uri(lt_session, magnet, params) while (not handle.has_metadata()): sleep(.1) info = handle.get_torrent_info() files = info.files() biggest_file = ("", 0) for file_ in files: if file_.size > biggest_file[1]: biggest_file = [file_.path, file_.size] print("Guessing data") filepath = biggest_file[0] guess = guess_video_info(filepath, info = ['filename']) video = Video.fromguess(filepath, guess) video.size = biggest_file[1] print("Donwloading Subtitle") subtitle = download_best_subtitles([video], {Language(lang)}, single=True) if not len(subtitle): subtitle = None else: subtitle = get_subtitle_path(video.name) lt_session.remove_torrent(handle) return subtitle
def get_best_subtitle(self): # Get a subtitle for a given video file self.status_bar.pop(self.context_id) self.status_bar.push(self.context_id, "Downloading Subtitle") self.timeout = GObject.timeout_add(100, self.progress_pulse) self.subtitle = download_best_subtitles( [self.video], {Language(self.language_combo.get_active_text())}, providers=[self.provider_combo.get_active_text()]) try: self.subtitle = self.subtitle[self.video][0] self.status_bar.pop(self.context_id) self.status_bar.push(self.context_id, "Subtitle Downloaded Successfully") except IndexError: self.status_bar.pop(self.context_id) self.status_bar.push(self.context_id, "No Subtitle Found") GObject.source_remove(self.timeout) self.progress_bar.set_fraction(0) return False save_subtitles(self.video, [self.subtitle]) GObject.source_remove(self.timeout) self.progress_bar.set_fraction(1)
def subtitle(self, episodes): # Parse babelfish languages bb_lang = {Language.fromietf(l) for l in self.languages} # Create subliminal episode set sub_episodes = set() for episode in episodes: ep_path = os.path.join(episode['dir'], episode['filename']) sub_episode = Episode.fromguess(ep_path, episode) # Look for external subtitles (not done automatically, apparently) sub_episode.subtitle_languages |= set(search_external_subtitles(sub_episode.name).values()) sub_episodes.add(sub_episode) # download subtitles in the specified language subl_subtitles = download_best_subtitles(sub_episodes, bb_lang, providers=self.providers) for video, subtitles in subl_subtitles.items(): save_subtitles(video, subtitles) # save subtitle languages in episode dict
def getSubtitle(self): if not "sub" in self.config or not os.path.isfile(self.config['sub']): self.config['sub'] = {} name = os.path.splitext(self.config['file']['name'])[0] file = os.path.join(self.config['file']['path'], name + ".srt") if os.path.isfile(file): print "found file. copying to temp" shutil.copy(file, self.config['temp']['path']) self.config['sub']['file'] = os.path.join(self.config['temp']['path'], name + ".srt") self.config['sub']['lang'] = self.config['language']['subtitle'][0] else: print "trying to download subtitle" file = self.config['file']['name'] lang = self.config['language']['subtitle'] languages = set(); for l in lang: languages.add(Language(l)) print languages videoPath = os.path.join(self.config['temp']['path'], file) video = set([subliminal.scan_video(videoPath)]) print video cache = self.config['temp']['path'] if "temp" in self.config else self.config['file']['path'] sub = subliminal.download_best_subtitles(video, languages) print sub.items() if not sub.items(): self.config['sub'] = False for item in sub.items(): subLang = item[1][0].language.alpha3 self.config['sub'][subLang] = {} self.config['sub'][subLang]['lang'] = subLang self.config['sub'][subLang]['file'] = subliminal.subtitle.get_subtitle_path(videoPath, Language(subLang)) print self.config['sub'] return self.config['sub']
def get_best_subtitle(self): # Get a subtitle for a given video file self.status_bar.pop(self.context_id) self.status_bar.push(self.context_id, "Downloading Subtitle") self.timeout = GObject.timeout_add( 100, self.progress_pulse ) self.subtitle = download_best_subtitles( [self.video], { Language( self.language_combo.get_active_text() ) }, providers=[self.provider_combo.get_active_text()] ) try: self.subtitle = self.subtitle[self.video][0] self.status_bar.pop(self.context_id) self.status_bar.push(self.context_id, "Subtitle Downloaded Successfully") except IndexError: self.status_bar.pop(self.context_id) self.status_bar.push(self.context_id, "No Subtitle Found") GObject.source_remove(self.timeout) self.progress_bar.set_fraction(0) return False save_subtitles(self.video, [self.subtitle]) GObject.source_remove(self.timeout) self.progress_bar.set_fraction(1)
def download(self, video_file): subtitle = None settings = get_settings() download_dir = settings.save_path log.info("Downloading subtitle") filepath = join(download_dir, video_file[0]) guess = guess_video_info(filepath, info=['filename']) video = Video.fromguess(filepath, guess) video.size = video_file[1] try: subtitle = download_best_subtitles([video], {Language(self.lang)}, single=True) except ValueError: pass if subtitle is not None and len(subtitle): log.info("CLAH %s %s", download_dir, video.name) subtitle = get_subtitle_path( join( download_dir, video.name.replace("(", "\(").replace(")", "\)").replace(" ", "\ "))) log.info( "video_file: %s, filepath: %s, guess: %s, video: %s, " "subtitle: %s", video_file, filepath, guess, video, subtitle) return subtitle
def download(self, model): """Download subtitles using subliminal""" video = subliminal.scan_video(model.series_path) subtitles = subliminal.download_best_subtitles( {video}, {Language('eng')}, ) save_subtitles(subtitles, True, config.download_path)
def fetch_subtitle(self, path): """ Fetch the subtitle using subliminal or from local file :param path: the file path to save the subtitle or to load the subtitle details from :return: :rtype: dict """ p = path + os.path.sep + self.get_video_string() + ".pkl" if not os.path.isfile(p) or not glob.glob( f"{path}/{self._video_obj.name}*.srt"): self.load_video_obj() logging.debug("Fetching %s's best matched subtitle" % self.get_video_string()) # This download the best subtitle as SRT file to the current directory try: subtitle = download_best_subtitles({self._video_obj}, {self._lang}, hearing_impaired=True) subtitle = subtitle[self._video_obj] except GuessitException: subtitle = [] if not subtitle: raise SubtitleNotFound save_subtitles(self._video_obj, subtitle, encoding='utf-8', directory=path) self._save_subtitle_info_dict(path) logging.debug("Loading %s metadata from %s" % (self.get_video_string(), p)) with open(p, "rb") as f: # os.chdir(owd) return pickle.load(f) # test if the subtitle object is loadable
def handle_subliminal_download(video, video_path, languages_to_retrieve): """ # Download the best subtitles in french and english Args: video : Name of video video_path: absolute path to videos languages_to_retrieve : dict of subtitles languages to retrieve return : dict with the path of each subtitles with str(language) as key / Exemple : str(<Language [en]> = 'en' """ subtitles_returned = {} best_subtitles = download_best_subtitles([video], set( map(Language, languages_to_retrieve))) if best_subtitles[video]: for retrieved_subtitle in best_subtitles[video]: subtitles_are_saved = save_subtitles(video, [retrieved_subtitle], encoding='utf8') if subtitles_are_saved: srt_fullpath = subtitle.get_subtitle_path( video_path, retrieved_subtitle.language) webvtt_fullpath = os.path.splitext(srt_fullpath)[0] + '.vtt' if os.path.isfile(webvtt_fullpath): # Add the subtitles path to subtitles_returned even if they are already downloaded/converted subtitles_returned[ retrieved_subtitle.language.alpha3] = webvtt_fullpath if os.path.isfile(srt_fullpath): # Add the subtitles path to subtitles_returned after converting them in .vtt convert_subtitles_to_webvtt(srt_fullpath, webvtt_fullpath) subtitles_returned[ retrieved_subtitle.language.alpha3] = webvtt_fullpath return subtitles_returned
def get_subtitle(magnet, lang): print("Explorando torrent") lt_session = session() lt_session.listen_on(6881, 6891) params = {"save_path": "/tmp"} handle = add_magnet_uri(lt_session, magnet, params) lt_session.start_dht() print "esperando" while (not handle.has_metadata()): sleep(.1) print "esperando" info = handle.get_torrent_info() print "esperando", info files = info.files() biggest_file = ("", 0) for file_ in files: if file_.size > biggest_file[1]: biggest_file = [file_.path, file_.size] print("Adivinando data") filepath = biggest_file[0] guess = guess_video_info(filepath, info=['filename']) video = Video.fromguess(filepath, guess) video.size = biggest_file[1] print("Bajando el subtitulo para lang = " + lang) subtitle = download_best_subtitles([video], {Language(lang)}, single=True) if not len(subtitle): subtitle = None else: subtitle = get_subtitle_path(video.name) lt_session.remove_torrent(handle) return subtitle
def download_subtitles(): """Download subtitles""" if request.json: mydata = request.json if not 'path' in mydata: # check if empty log(NO_PATH_PROVIDED) return error(NO_PATH_PROVIDED, 406) # check if languages is empty, if so use english if not 'languages' in mydata or not mydata['languages']: mydata['languages'] = "eng" log(json.dumps(mydata)) path = mydata['path'] videos = [] try: videos.append(scan_video(path)) subtitles = download_best_subtitles( videos, parse_languages(mydata['languages'])) for video in videos: save_subtitles(video, subtitles[video]) return json.dumps(mydata) except Exception: log(NO_SUBTITLES_FOUND) return error(NO_SUBTITLES_FOUND, 404) else: log(NO_DATA_RECEIVED) return error(NO_DATA_RECEIVED, 406)
def download_subtitles(args): """Function to process supported video files and download subtitles for them Parameters: args (ArgumentParser object): contains all the command line args Returns: 1 (int): for success """ # collecting all the video file names in the targeted directory language_dict = {} path = ' '.join(args.directory) print("[User] checking files in {} ...".format(path)) videos = subliminal.scan_videos(path) #TODO: download new video's subtitles , age=timedelta(weeks=2) if args.language == 'en': language_dict ={Language('eng')} elif args.language == 'fr': language_dict ={Language('fra')} else: language_dict = {Language('eng'), Language('fra')} subtitles = subliminal.download_best_subtitles(videos, language_dict) for v in videos: subliminal.save_subtitles(v, subtitles[v]) # removing en/fr from the subtitle name for file in os.listdir(path): for ext in SUBTITLE_EXT: lan_ext = '.{}{}'.format('en', ext) # print('file: ', file, '\t\t lan_ext: ', lan_ext) file_path = os.path.join(path, file) new_file_path = os.path.join(path, file.replace(lan_ext , ext)) # print('file_path: ', file_path, '\t\t new_file_path: ', new_file_path) if file.endswith(lan_ext): os.rename(file_path, new_file_path) return 1
def test_download_best_subtitles_min_score(self): videos = [MOVIES[0]] for video in videos: video.name = os.path.join(TEST_DIR, video.name.split(os.sep)[-1]) languages = {Language("eng"), Language("fra")} subtitles = download_best_subtitles(videos, languages, min_score=1000) self.assertTrue(len(subtitles) == 0)
def download_subtitles(directory): name = 'dogpile.cache.dbm' cache_file = get_cache_file(name) region.configure('dogpile.cache.dbm', arguments={'filename': cache_file}) videos = scan_videos(directory) subtitles = download_best_subtitles(videos, {babelfish.Language('eng')}) for video in videos: save_subtitles(video, subtitles[video], single=True)
def test_download_best_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 = download_best_subtitles(videos, languages) for video in videos: self.assertIn(video, subtitles) self.assertEqual(len(subtitles[video]), 2)
def downloadSubtitles(subtitles_info): existing_subtitles = subtitles_info['subtitles'] # First of all, check if we need subtitles languages = getNeededLanguages(existing_subtitles) if not languages: logger.log(u'%s: No missing subtitles for S%02dE%02d' % (subtitles_info['show.indexerid'], subtitles_info['season'], subtitles_info['episode']), logger.DEBUG) return (existing_subtitles, None) subtitles_path = getSubtitlesPath(subtitles_info['location']).encode(sickbeard.SYS_ENCODING) video_path = subtitles_info['location'].encode(sickbeard.SYS_ENCODING) providers = getEnabledServiceList() try: video = subliminal.scan_video(video_path, subtitles=False, embedded_subtitles=False) except Exception: logger.log(u'%s: Exception caught in subliminal.scan_video for S%02dE%02d' % (subtitles_info['show.indexerid'], subtitles_info['season'], subtitles_info['episode']), logger.DEBUG) return (existing_subtitles, None) try: # TODO: Add gui option for hearing_impaired parameter ? found_subtitles = subliminal.download_best_subtitles([video], languages=languages, hearing_impaired=False, only_one=not sickbeard.SUBTITLES_MULTI, providers=providers) if not found_subtitles: logger.log(u'%s: No subtitles found for S%02dE%02d on any provider' % (subtitles_info['show.indexerid'], subtitles_info['season'], subtitles_info['episode']), logger.DEBUG) return (existing_subtitles, None) for index, subtitle in enumerate(found_subtitles[video]): encoding = subliminal.subtitle.Subtitle.guess_encoding(subtitle) found_subtitles[video][index].encoding = encoding subliminal.save_subtitles(video, found_subtitles[video], directory=subtitles_path, single=not sickbeard.SUBTITLES_MULTI) for video, subtitles in found_subtitles.iteritems(): for subtitle in subtitles: new_video_path = subtitles_path + "/" + video.name.rsplit("/", 1)[-1] new_subtitles_path = subliminal.subtitle.get_subtitle_path(new_video_path, subtitle.language if sickbeard.SUBTITLES_MULTI else None) sickbeard.helpers.chmodAsParent(new_subtitles_path) sickbeard.helpers.fixSetGroupID(new_subtitles_path) if not sickbeard.EMBEDDED_SUBTITLES_ALL and sickbeard.SUBTITLES_EXTRA_SCRIPTS and video_path.endswith(('.mkv','.mp4')): run_subs_extra_scripts(subtitles_info, found_subtitles) current_subtitles = subtitlesLanguages(video_path)[0] new_subtitles = frozenset(current_subtitles).difference(existing_subtitles) except Exception as e: logger.log("Error occurred when downloading subtitles for: %s" % video_path) logger.log(traceback.format_exc(), logger.ERROR) return (existing_subtitles, None) if sickbeard.SUBTITLES_HISTORY: for video, subtitles in found_subtitles.iteritems(): for subtitle in subtitles: logger.log(u'history.logSubtitle %s, %s' % (subtitle.provider_name, subtitle.language.opensubtitles), logger.DEBUG) history.logSubtitle(subtitles_info['show.indexerid'], subtitles_info['season'], subtitles_info['episode'], subtitles_info['status'], subtitle) return (current_subtitles, new_subtitles)
def test_download_best_subtitles_hearing_impaired(self): videos = [MOVIES[0]] for video in videos: video.name = os.path.join(TEST_DIR, os.path.split(video.name)[1]) languages = {Language('eng')} subtitles = download_best_subtitles(videos, languages, hearing_impaired=True) self.assertTrue(subtitles[videos[0]][0].hearing_impaired)
def test_download_best_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 = download_best_subtitles(videos, languages, single=True) for video in videos: self.assertTrue(video in subtitles and len(subtitles[video]) == 1) self.assertTrue(os.path.exists(os.path.splitext(video.name)[0] + ".srt"))
def downloadSubtitle_OpenSubtitles_heb(videoname, folder): video = subliminal.Video.fromname(videoname) best_subtitles = subliminal.download_best_subtitles( [video], {babelfish.Language('heb')}, providers=None) best_subtitle = best_subtitles[video][0] file_name = folder + "/" + videoname + ".srt" subtitle = best_subtitle.content with open(file_name, 'wb') as f: f.write(subtitle)
def download_sub(self, language): l = Language(language) v = scan_video(self.path) sub_path = get_subtitle_path(v.name, l) if not os.path.isfile(sub_path): sub = download_best_subtitles((v,), {l}) # TODO Save in tmp folder if regular is not available save_subtitles(sub) return sub_path
def find_subtitles(video, language): """ Input: object video Output: returns the best subtitle found Throws: ValueError """ subtitles = download_best_subtitles([video], {Language(language)}) best_subtitle = subtitles[video][0] return best_subtitle
def test_download_best_subtitles_no_language(episodes): video = episodes['bbt_s07e05'] languages = {Language('fra')} video.subtitle_languages = languages providers = ['addic7ed'] subtitles = download_best_subtitles({video}, languages, min_score=video.scores['hash'], providers=providers) assert len(subtitles) == 0
def test_download_best_subtitles_undefined(episodes): video = episodes['bbt_s07e05'] languages = {Language('und')} video.subtitle_languages = languages providers = ['addic7ed'] subtitles = download_best_subtitles({video}, languages, min_score=video.scores['hash'], only_one=True, providers=providers) assert len(subtitles) == 0
def test_download_best_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 = download_best_subtitles(videos, languages, single=True) for video in videos: self.assertTrue(video in subtitles and len(subtitles[video]) == 1) self.assertTrue( os.path.exists(os.path.splitext(video.name)[0] + '.srt'))
def subliminal(): parser = subliminal_parser() args = parser.parse_args() # parse paths try: args.paths = [p.decode('utf-8') for p in args.paths] except UnicodeDecodeError: parser.error('argument paths: encodings is not utf-8: %r' % args.paths) # parse languages try: args.languages = {babelfish.Language.fromalpha2(l) for l in args.languages} except babelfish.Error: parser.error('argument -l/--languages: codes are not ISO-639-1: %r' % args.languages) # parse age if args.age is not None: match = re.match(r'^(?:(?P<weeks>\d+?)w)?(?:(?P<days>\d+?)d)?(?:(?P<hours>\d+?)h)?$', args.age) if not match: parser.error('argument -a/--age: invalid age: %r' % args.age) args.age = datetime.timedelta(**match.groupdict()) # setup verbosity if args.verbose: logging.basicConfig(level=logging.DEBUG) elif not args.quiet: logging.basicConfig(level=logging.WARN) # configure cache cache_region.configure('dogpile.cache.dbm', arguments={'filename': os.path.expanduser(args.cache_file)}) # scan videos videos = scan_videos([p for p in args.paths if os.path.exists(p)], subtitles=not args.force, age=args.age) # guess videos videos.extend([Video.fromguess(os.path.split(p)[1], guessit.guess_file_info(p, 'autodetect')) for p in args.paths if not os.path.exists(p)]) # download best subtitles subtitles = download_best_subtitles(videos, args.languages, providers=args.providers, provider_configs=None, single=args.single, min_score=args.min_score, hearing_impaired=args.hearing_impaired) # output result if not subtitles: if not args.quiet: sys.stderr.write('No subtitles downloaded\n') exit(1) if not args.quiet: subtitles_count = sum([len(s) for s in subtitles.values()]) if subtitles_count == 1: print('%d subtitle downloaded' % subtitles_count) else: print('%d subtitles downloaded' % subtitles_count)
def test_download_best_subtitles(episodes): video = episodes['bbt_s07e05'] languages = {Language('fra'), Language('por', 'BR')} providers = ['addic7ed', 'thesubdb'] expected_subtitles = {('thesubdb', '9dbbfb7ba81c9a6237237dae8589fccc'), ('addic7ed', 'updated/10/80254/1')} subtitles = download_best_subtitles({video}, languages, providers=providers) assert len(subtitles) == 1 assert len(subtitles[video]) == 2 assert {(s.provider_name, s.id) for s in subtitles[video]} == expected_subtitles
def test_save_subtitles_single(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 = download_best_subtitles(videos, languages) save_subtitles(subtitles, single=True) for video in videos: self.assertIn(video, subtitles) self.assertEqual(len(subtitles[video]), 2) self.assertTrue(os.path.exists(os.path.splitext(video.name)[0] + '.srt'))
def test_download_best_subtitles_only_one(episodes): video = episodes['bbt_s07e05'] languages = {Language('nld'), Language('por', 'BR')} providers = ['addic7ed', 'thesubdb'] expected_subtitles = {('thesubdb', '9dbbfb7ba81c9a6237237dae8589fccc-pt-BR')} subtitles = download_best_subtitles({video}, languages, only_one=True, providers=providers) assert len(subtitles) == 1 assert len(subtitles[video]) == 1 assert {(s.provider_name, s.id) for s in subtitles[video]} == expected_subtitles
def find_subs(filename, movie_dir): video = Video.fromname(filename) subtitles = download_best_subtitles([video], {Language("eng")}) if len(subtitles[video]) == 0: print("No subs found") return best_sub = subtitles[video][0] save_subtitles(video, [best_sub], directory=movie_dir) return get_sub_path(movie_dir)
def download_sub(self, lang='eng'): prov_conf = {'opensubtitles': {'username': '******', 'password': '******'}} logging.info("{}: Downloading subtitles...".format(self.filename)) vid = scan_video(self.path) best_subs = download_best_subtitles({vid}, {babelfish.Language(lang)}, only_one=True, provider_configs=prov_conf) if best_subs[vid]: sub = best_subs[vid][0] save_subtitles(vid, [sub], single=True) logging.info("{}: Subtitles successfully downloaded.".format(self.filename)) else: logging.error("{}: No subtitles found online.".format(self.filename))
def download_subtitles(directory): if not directory: directory = os.getcwd() logger.info('Downloading subtitles for videos in {}'.format(directory)) backend = 'dogpile.cache.dbm' cache_file = u.get_cache_file('subliminal.cache') region.configure(backend, arguments={'filename': cache_file}) videos = scan_videos(directory) subtitles = download_best_subtitles(videos, {babelfish.Language('eng')}) for video in videos: save_subtitles(video, subtitles[video], single=True)
def test_save_subtitles_single(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 = download_best_subtitles(videos, languages) save_subtitles(subtitles, single=True) for video in videos: self.assertIn(video, subtitles) self.assertEqual(len(subtitles[video]), 2) self.assertTrue( os.path.exists(os.path.splitext(video.name)[0] + '.srt'))
def downloadsubtitles(self_) : LOGGER.info('*** START DOWNLOADING SUBTITLES ***') if SIMULATE_MODE : return # scan for videos in the folder and their subtitles videos = subliminal.scan_videos(self_.scanPath, subtitles=True, embedded_subtitles=True) # download subs = subliminal.download_best_subtitles(videos, {babelfish.Language('eng')}) # save for video, sub in subs.items(): subliminal.save_subtitles(video, sub)
def download_subs(file): print(" Analyzing video file...") try: video = scan_video(file['full_path']) except ValueError as ex: print(" Failed to analyze video. ", ex) return None print(" Choosing subtitle from online providers...") best_subtitles = download_best_subtitles({video}, {Language('eng')}, only_one=True) if best_subtitles[video]: sub = best_subtitles[video][0] print(" Choosen subtitle: {f}".format(f=sub)) print(" Downloading...") save_subtitles(video, [sub], single=True) else: print(" ERROR: No subtitles found online.")
def run(self): """ The main thread. this will run: - download torrent using utorrent - check utorent for status until finish downloading - move file to new location :return: """ # start downloading and get hash self.state = DownloadTorrentThread.STATE_DOWNLOADING before_list = utorrentUtils.get_all_torrents() if not utorrentUtils.download_file(self.magnet_link): # TODO: run utorrent raise RuntimeError('Utorrent not working!') time.sleep(1) after_list = utorrentUtils.get_all_torrents() self.hash, torrent_data = self._get_new_downloaded_hash(before_list, after_list) if not self.hash: print 'file already existing in utorrent' return # print self.hash # print torrent_data torrent_name = self._wait_to_finish_downloading() # get all video files and move them to the correct location self.state = DownloadTorrentThread.STATE_MOVING files = utorrentUtils.get_torrent_files(self.hash) video_files_data = utorrentUtils.get_data_for_video_files(files, torrent_name=torrent_name) if video_files_data: self.video_files_data = video_files_data self._copy_files() # download subtitles for data in video_files_data: src_file = data['full_file_path'] dst_dir = os.path.join(LocalFilesUtil.get_series_path(data['series_name']), 'Season ' + str(data['season'])) dst_file = os.path.join(dst_dir, os.path.split(src_file)[1]) videos = subliminal.scan_videos([dst_file]) subtitles = subliminal.download_best_subtitles(videos, {Language('heb'), Language('eng')}) subliminal.save_subtitles(subtitles) self.state = DownloadTorrentThread.STATE_FINISHED pass
def download(self, video_file): settings = get_settings() download_dir = settings['save_path'] print("Downloading subtitle") log.info("Downloading subtitle") filepath = join(download_dir, video_file[0]) guess = guess_video_info(filepath, info=['filename']) video = Video.fromguess(filepath, guess) video.size = video_file[1] subtitle = download_best_subtitles([video], {Language(self.lang)}, single=True) if not len(subtitle): subtitle = None else: subtitle = get_subtitle_path(join(download_dir, video.name)) log.info("video_file: %s, filepath: %s, guess: %s, video: %s" "subtitle: %s", video_file, filepath, guess, video, subtitle) return subtitle
def downloadSub(myFile, lang, path, verbose): cli = False print "--- Trying to download..." origWD = os.getcwd() # current working directory os.chdir(path) # change working directory to where the videos are if cli == True: # old subliminal: #if call(["subliminal", "-q", "-l", lang, "--", myFile]): # try to download the subtitles # new subliminal if call(["subliminal", "download", "-l", lang, "--", myFile]): # try to download the subtitles print "*** Could not find %s subtitles" % langName(lang).lower() subDownloads = foundLang("%s - not found" % lang) else: print "--- Downloaded %s subtitles" % langName(lang).lower() subDownloads = foundLang(lang) # sending language code to be added # subName = "%s.%s.%s" % (os.path.splitext(myFile)[0], lang, "srt") else: video = Video.fromname(myFile) if verbose: print "--- Checking subtititles for \n %s" % video # configure the cache #region.configure('dogpile.cache.dbm', arguments={'filename': 'cachefile.dbm'}) my_region = region.configure('dogpile.cache.memory', arguments={'filename': 'cachefile.dbm'}, replace_existing_backend=True) if verbose: print "--- Searching for best subtitle..." #best_subtitles = download_best_subtitles([video], {lang}, providers=['podnapisi']) best_subtitles = download_best_subtitles([video], {lang}, providers=['podnapisi', 'opensubtitles', 'addic7ed']) #best_subtitles = download_best_subtitles([video], {lang}) try: best_subtitle = best_subtitles[video][0] except: print "*** Could not find %s subtitles" % langName(lang).lower() subDownloads = foundLang("%s - not found" % lang) else: print "--- Downloaded %s subtitles" % langName(lang).lower() #if verbose: # print "--- Score for this subtitle is %s" % compute_score(best_subtitle, video) subDownloads = foundLang(lang) # sending language code to be added if verbose: print "--- Saving subtitles..." save_subtitles(video, [best_subtitle]) os.chdir(origWD) # change working directory back return subDownloads
def download(request): language_query = request.GET.get('l') video_query = request.GET.get('q') if language_query is None or video_query is None: response = JsonResponse({'errors': [{'message': 'MISSING_PACK_OR_LANGUAGE'}]}) response.status_code = 400 return response language = Language(language_query) video = subliminal.Video.fromname(video_query) subtitles = subliminal.download_best_subtitles({video}, {language}) if len(subtitles[video]) == 0: response = HttpResponse("No subtitles found for %s" % video.name) else: content = subtitles[video][0].content response = HttpResponse(content, content_type='application/octet-stream') response['Content-Disposition'] = 'attachment; filename=%s.srt' % basename(video.name) return response