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("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(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 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 test_video_fromguess_wrong_type(episodes): guess = {'type': 'subtitle'} with pytest.raises(ValueError) as excinfo: Video.fromguess(episodes['bbt_s07e05'].name, guess) assert str(excinfo.value) == 'The guess must be an episode or a movie guess'
def test_video_fromguess_movie(movies, monkeypatch): guess = {'type': 'movie'} monkeypatch.setattr(Movie, 'fromguess', Mock()) Video.fromguess(movies['man_of_steel'].name, guess) assert Movie.fromguess.called
def test_video_fromguess_episode(episodes, monkeypatch): guess = {'type': 'episode'} monkeypatch.setattr(Episode, 'fromguess', Mock()) Video.fromguess(episodes['bbt_s07e05'].name, guess) assert Episode.fromguess.called
def scan_video(path, dont_use_actual_file=False, hints=None, providers=None, skip_hashing=False): """Scan a video from a `path`. patch: - allow passing of hints/options to guessit - allow dry-run with dont_use_actual_file - add crap removal (obfuscated/scrambled) - trust plex's movie name :param str path: existing path to the video. :return: the scanned video. :rtype: :class:`~subliminal.video.Video` """ hints = hints or {} video_type = hints.get("type") # check for non-existing path if not dont_use_actual_file and not os.path.exists(path): raise ValueError('Path does not exist') # check video extension if not path.lower().endswith(VIDEO_EXTENSIONS): raise ValueError('%r is not a valid video extension' % os.path.splitext(path)[1]) dirpath, filename = os.path.split(path) logger.info('Determining basic video properties for %r in %r', filename, dirpath) # hint guessit the filename itself and its 2 parent directories if we're an episode (most likely # Series name/Season/filename), else only one split_path = os.path.normpath(path).split(os.path.sep)[-3 if video_type == "episode" else -2:] # remove crap from folder names if video_type == "episode": if len(split_path) > 2: split_path[-3] = remove_crap_from_fn(split_path[-3]) else: if len(split_path) > 1: split_path[-2] = remove_crap_from_fn(split_path[-2]) guess_from = os.path.join(*split_path) # remove crap from file name guess_from = remove_crap_from_fn(guess_from) # guess hints["single_value"] = True if "title" in hints: hints["expected_title"] = [hints["title"]] guessed_result = guessit(guess_from, options=hints) logger.debug('GuessIt found: %s', json.dumps(guessed_result, cls=GuessitEncoder, indent=4, ensure_ascii=False)) video = Video.fromguess(path, guessed_result) video.hints = hints # get possibly alternative title from the filename itself alt_guess = guessit(filename, options=hints) if "title" in alt_guess and alt_guess["title"] != guessed_result["title"]: if video_type == "episode": video.alternative_series.append(alt_guess["title"]) else: video.alternative_titles.append(alt_guess["title"]) logger.debug("Adding alternative title: %s", alt_guess["title"]) if dont_use_actual_file: return video # size and hashes if not skip_hashing: video.size = os.path.getsize(path) if video.size > 10485760: logger.debug('Size is %d', video.size) if "opensubtitles" in providers: video.hashes['opensubtitles'] = hash_opensubtitles(path) if "shooter" in providers: video.hashes['shooter'] = hash_shooter(path) if "thesubdb" in providers: video.hashes['thesubdb'] = hash_thesubdb(path) if "napiprojekt" in providers: try: video.hashes['napiprojekt'] = hash_napiprojekt(path) except MemoryError: logger.warning(u"Couldn't compute napiprojekt hash for %s", path) logger.debug('Computed hashes %r', video.hashes) else: logger.warning('Size is lower than 10MB: hashes not computed') return video
def scan_video(path, dont_use_actual_file=False, hints=None, providers=None, skip_hashing=False): """Scan a video from a `path`. patch: - allow passing of hints/options to guessit - allow dry-run with dont_use_actual_file - add crap removal (obfuscated/scrambled) - trust plex's movie name :param str path: existing path to the video. :return: the scanned video. :rtype: :class:`~subliminal.video.Video` """ hints = hints or {} video_type = hints.get("type") # check for non-existing path if not dont_use_actual_file and not os.path.exists(path): raise ValueError('Path does not exist') # check video extension if not path.lower().endswith(VIDEO_EXTENSIONS): raise ValueError('%r is not a valid video extension' % os.path.splitext(path)[1]) dirpath, filename = os.path.split(path) logger.info('Determining basic video properties for %r in %r', filename, dirpath) # hint guessit the filename itself and its 2 parent directories if we're an episode (most likely # Series name/Season/filename), else only one split_path = os.path.normpath(path).split( os.path.sep)[-3 if video_type == "episode" else -2:] # remove crap from folder names if video_type == "episode": if len(split_path) > 2: split_path[-3] = remove_crap_from_fn(split_path[-3]) else: if len(split_path) > 1: split_path[-2] = remove_crap_from_fn(split_path[-2]) guess_from = os.path.join(*split_path) # remove crap from file name guess_from = remove_crap_from_fn(guess_from) # guess hints["single_value"] = True if "title" in hints: hints["expected_title"] = [hints["title"]] guessed_result = guessit(guess_from, options=hints) logger.debug( 'GuessIt found: %s', json.dumps(guessed_result, cls=GuessitEncoder, indent=4, ensure_ascii=False)) video = Video.fromguess(path, guessed_result) video.hints = hints if dont_use_actual_file: return video # size and hashes if not skip_hashing: video.size = os.path.getsize(path) if video.size > 10485760: logger.debug('Size is %d', video.size) if "opensubtitles" in providers: video.hashes['opensubtitles'] = hash_opensubtitles(path) if "shooter" in providers: video.hashes['shooter'] = hash_shooter(path) if "thesubdb" in providers: video.hashes['thesubdb'] = hash_thesubdb(path) if "napiprojekt" in providers: try: video.hashes['napiprojekt'] = hash_napiprojekt(path) except MemoryError: logger.warning(u"Couldn't compute napiprojekt hash for %s", path) logger.debug('Computed hashes %r', video.hashes) else: logger.warning('Size is lower than 10MB: hashes not computed') return video
def scan_video(path, dont_use_actual_file=False, hints=None, providers=None, skip_hashing=False, hash_from=None): """Scan a video from a `path`. patch: - allow passing of hints/options to guessit - allow dry-run with dont_use_actual_file - add crap removal (obfuscated/scrambled) - trust plex's movie name :param str path: existing path to the video. :return: the scanned video. :rtype: :class:`~subliminal.video.Video` """ hints = hints or {} video_type = hints.get("type") # check for non-existing path if not dont_use_actual_file and not os.path.exists(path): raise ValueError('Path does not exist') # check video extension if not path.lower().endswith(VIDEO_EXTENSIONS): raise ValueError('%r is not a valid video extension' % os.path.splitext(path)[1]) dirpath, filename = os.path.split(path) logger.info('Determining basic video properties for %r in %r', filename, dirpath) # hint guessit the filename itself and its 2 parent directories if we're an episode (most likely # Series name/Season/filename), else only one split_path = os.path.normpath(path).split( os.path.sep)[-3 if video_type == "episode" else -2:] # remove crap from folder names if video_type == "episode": if len(split_path) > 2: split_path[-3] = remove_crap_from_fn(split_path[-3]) else: if len(split_path) > 1: split_path[-2] = remove_crap_from_fn(split_path[-2]) guess_from = os.path.join(*split_path) # remove crap from file name guess_from = remove_crap_from_fn(guess_from) # guess hints["single_value"] = True if "title" in hints: hints["expected_title"] = [hints["title"]] guessed_result = guessit(guess_from, options=hints) logger.debug( 'GuessIt found: %s', json.dumps(guessed_result, cls=GuessitEncoder, indent=4, ensure_ascii=False)) video = Video.fromguess(path, guessed_result) video.hints = hints # get possibly alternative title from the filename itself alt_guess = guessit(filename, options=hints) if "title" in alt_guess and alt_guess["title"] != guessed_result["title"]: if video_type == "episode": video.alternative_series.append(alt_guess["title"]) else: video.alternative_titles.append(alt_guess["title"]) logger.debug("Adding alternative title: %s", alt_guess["title"]) if dont_use_actual_file and not hash_from: return video # if all providers are throttled, skip hashing if not providers: skip_hashing = True # size and hashes if not skip_hashing: hash_path = hash_from or path # Drik add 1 # If exist, use hash from hash file hashfile = os.path.join(os.path.splitext(hash_path)[0] + '.openhash') if (os.path.isfile(hashfile)): try: f_open = open(hashfile, "r") hashandsize = f_open.read() hashandsize = hashandsize.split(";") video.size = int(hashandsize[1]) f_open.close() logger.debug("Read size from text file for: " + hash_path) except: logger.exception("Failed reading size from file for: " + hash_path) video.size = os.path.getsize(hash_path) else: video.size = os.path.getsize(hash_path) # Drik add 1 if video.size > 10485760: logger.debug('Size is %d', video.size) osub_hash = None if "bsplayer" in providers: video.hashes['bsplayer'] = osub_hash = hash_opensubtitles( hash_path) if "opensubtitles" in providers: video.hashes[ 'opensubtitles'] = osub_hash = osub_hash or hash_opensubtitles( hash_path) if "opensubtitlescom" in providers: video.hashes[ 'opensubtitlescom'] = osub_hash = osub_hash or hash_opensubtitles( hash_path) if "shooter" in providers: video.hashes['shooter'] = hash_shooter(hash_path) if "thesubdb" in providers: video.hashes['thesubdb'] = hash_thesubdb(hash_path) if "napiprojekt" in providers: try: video.hashes['napiprojekt'] = hash_napiprojekt(hash_path) except MemoryError: logger.warning(u"Couldn't compute napiprojekt hash for %s", hash_path) if "napisy24" in providers: # Napisy24 uses the same hash as opensubtitles video.hashes['napisy24'] = osub_hash or hash_opensubtitles( hash_path) logger.debug('Computed hashes %r', video.hashes) else: logger.warning('Size is lower than 10MB: hashes not computed') return video
def test_video_fromguess_movie(movies, monkeypatch): guess = {"type": "movie"} monkeypatch.setattr(Movie, "fromguess", Mock()) Video.fromguess(movies["man_of_steel"].name, guess) assert Movie.fromguess.called
def test_video_fromguess_episode(episodes, monkeypatch): guess = {"type": "episode"} monkeypatch.setattr(Episode, "fromguess", Mock()) Video.fromguess(episodes["bbt_s07e05"].name, guess) assert Episode.fromguess.called
def scan_video(path, subtitles=True, embedded_subtitles=True, video_type=None): """Scan a video and its subtitle languages from a video `path`. :param str path: existing path to the video. :param bool subtitles: scan for subtitles with the same name. :param bool embedded_subtitles: scan for embedded subtitles. :return: the scanned video. :rtype: :class:`Video` # patch: suggest video type to guessit beforehand """ # check for non-existing path if not os.path.exists(path): raise ValueError('Path does not exist') # check video extension if not path.endswith(VIDEO_EXTENSIONS): raise ValueError('%s is not a valid video extension' % os.path.splitext(path)[1]) dirpath, filename = os.path.split(path) logger.info('Scanning video (type: %s) %r in %r', video_type, filename, dirpath) # guess video = Video.fromguess(path, guess_file_info(path, options={"type": video_type})) # size and hashes video.size = os.path.getsize(path) if video.size > 10485760: logger.debug('Size is %d', video.size) video.hashes['opensubtitles'] = hash_opensubtitles(path) video.hashes['thesubdb'] = hash_thesubdb(path) logger.debug('Computed hashes %r', video.hashes) else: logger.warning('Size is lower than 10MB: hashes not computed') # external subtitles if subtitles: video.subtitle_languages |= set(patched_search_external_subtitles(path).values()) # video metadata with enzyme try: if filename.endswith('.mkv'): with open(path, 'rb') as f: mkv = MKV(f) # main video track if mkv.video_tracks: video_track = mkv.video_tracks[0] # resolution if video_track.height in (480, 720, 1080): if video_track.interlaced: video.resolution = '%di' % video_track.height else: video.resolution = '%dp' % video_track.height logger.debug('Found resolution %s with enzyme', video.resolution) # video codec if video_track.codec_id == 'V_MPEG4/ISO/AVC': video.video_codec = 'h264' logger.debug('Found video_codec %s with enzyme', video.video_codec) elif video_track.codec_id == 'V_MPEG4/ISO/SP': video.video_codec = 'DivX' logger.debug('Found video_codec %s with enzyme', video.video_codec) elif video_track.codec_id == 'V_MPEG4/ISO/ASP': video.video_codec = 'XviD' logger.debug('Found video_codec %s with enzyme', video.video_codec) else: logger.warning('MKV has no video track') # main audio track if mkv.audio_tracks: audio_track = mkv.audio_tracks[0] # audio codec if audio_track.codec_id == 'A_AC3': video.audio_codec = 'AC3' logger.debug('Found audio_codec %s with enzyme', video.audio_codec) elif audio_track.codec_id == 'A_DTS': video.audio_codec = 'DTS' logger.debug('Found audio_codec %s with enzyme', video.audio_codec) elif audio_track.codec_id == 'A_AAC': video.audio_codec = 'AAC' logger.debug('Found audio_codec %s with enzyme', video.audio_codec) else: logger.warning('MKV has no audio track') # subtitle tracks if mkv.subtitle_tracks: if embedded_subtitles: embedded_subtitle_languages = set() for st in mkv.subtitle_tracks: if st.language: try: embedded_subtitle_languages.add(Language.fromalpha3b(st.language)) except BabelfishError: logger.error('Embedded subtitle track language %r is not a valid language', st.language) embedded_subtitle_languages.add(Language('und')) elif st.name: try: embedded_subtitle_languages.add(Language.fromname(st.name)) except BabelfishError: logger.debug('Embedded subtitle track name %r is not a valid language', st.name) embedded_subtitle_languages.add(Language('und')) else: embedded_subtitle_languages.add(Language('und')) logger.debug('Found embedded subtitle %r with enzyme', embedded_subtitle_languages) video.subtitle_languages |= embedded_subtitle_languages else: logger.debug('MKV has no subtitle track') except EnzymeError: logger.exception('Parsing video metadata with enzyme failed') return video
def test_video_fromguess_wrong_type(episodes): guess = {'type': 'subtitle'} with pytest.raises(ValueError) as excinfo: Video.fromguess(episodes['bbt_s07e05'].name, guess) assert str( excinfo.value) == 'The guess must be an episode or a movie guess'
def test_video_fromguess_movie(movies, monkeypatch): guess = {'type': 'movie'} monkeypatch.setattr(Movie, 'fromguess', Mock()) Video.fromguess(movies['man_of_steel'].name, guess) assert Movie.fromguess.called
def test_video_fromguess_episode(episodes, monkeypatch): guess = {'type': 'episode'} monkeypatch.setattr(Episode, 'fromguess', Mock()) Video.fromguess(episodes['bbt_s07e05'].name, guess) assert Episode.fromguess.called
def scan_video(path, subtitles=True, embedded_subtitles=True, hints=None, video_fps=None, dont_use_actual_file=False): """Scan a video and its subtitle languages from a video `path`. :param dont_use_actual_file: guess on filename, but don't use the actual file itself :param str path: existing path to the video. :param bool subtitles: scan for subtitles with the same name. :param bool embedded_subtitles: scan for embedded subtitles. :param hints: hints dict for guessit :return: the scanned video. :rtype: :class:`Video` # patch: suggest video type to guessit beforehand """ hints = hints or {} # check for non-existing path if not dont_use_actual_file and not os.path.exists(path): raise ValueError('Path does not exist') # check video extension if not path.endswith(VIDEO_EXTENSIONS): raise ValueError('%s is not a valid video extension' % os.path.splitext(path)[1]) dirpath, filename = os.path.split(path) # hint guessit the filename itself and its 2 parent directories if we're an episode (most likely Series name/Season/filename), else only one guess_from = os.path.join(*os.path.normpath(path).split(os.path.sep)[-3 if hints.get("type") == "episode" else -2:]) hints = hints or {} logger.info('Scanning video (hints: %s) %r', hints, guess_from) # guess try: video = Video.fromguess(path, guess_file_info(guess_from, options=hints)) video.fps = video_fps if dont_use_actual_file: return video # size and hashes video.size = os.path.getsize(path) if video.size > 10485760: logger.debug('Size is %d', video.size) video.hashes['opensubtitles'] = hash_opensubtitles(path) video.hashes['thesubdb'] = hash_thesubdb(path) logger.debug('Computed hashes %r', video.hashes) else: logger.warning('Size is lower than 10MB: hashes not computed') # external subtitles if subtitles: video.subtitle_languages |= set(patched_search_external_subtitles(path).values()) except Exception: logger.error("Something went wrong when running guessit: %s", traceback.format_exc()) return # video metadata with enzyme try: if filename.endswith('.mkv'): with open(path, 'rb') as f: mkv = MKV(f) # main video track if mkv.video_tracks: video_track = mkv.video_tracks[0] # resolution if video_track.height in (480, 720, 1080): if video_track.interlaced: video.resolution = '%di' % video_track.height else: video.resolution = '%dp' % video_track.height logger.debug('Found resolution %s with enzyme', video.resolution) # video codec if video_track.codec_id == 'V_MPEG4/ISO/AVC': video.video_codec = 'h264' logger.debug('Found video_codec %s with enzyme', video.video_codec) elif video_track.codec_id == 'V_MPEG4/ISO/SP': video.video_codec = 'DivX' logger.debug('Found video_codec %s with enzyme', video.video_codec) elif video_track.codec_id == 'V_MPEG4/ISO/ASP': video.video_codec = 'XviD' logger.debug('Found video_codec %s with enzyme', video.video_codec) else: logger.warning('MKV has no video track') # main audio track if mkv.audio_tracks: audio_track = mkv.audio_tracks[0] # audio codec if audio_track.codec_id == 'A_AC3': video.audio_codec = 'AC3' logger.debug('Found audio_codec %s with enzyme', video.audio_codec) elif audio_track.codec_id == 'A_DTS': video.audio_codec = 'DTS' logger.debug('Found audio_codec %s with enzyme', video.audio_codec) elif audio_track.codec_id == 'A_AAC': video.audio_codec = 'AAC' logger.debug('Found audio_codec %s with enzyme', video.audio_codec) else: logger.warning('MKV has no audio track') # subtitle tracks if mkv.subtitle_tracks: if embedded_subtitles: embedded_subtitle_languages = set() for st in mkv.subtitle_tracks: if st.forced: logger.debug("Ignoring forced subtitle track %r", st) continue if st.language: try: embedded_subtitle_languages.add(Language.fromalpha3b(st.language)) except BabelfishError: logger.error('Embedded subtitle track language %r is not a valid language', st.language) embedded_subtitle_languages.add(Language('und')) elif st.name: try: embedded_subtitle_languages.add(Language.fromname(st.name)) except BabelfishError: logger.debug('Embedded subtitle track name %r is not a valid language', st.name) embedded_subtitle_languages.add(Language('und')) else: embedded_subtitle_languages.add(Language('und')) logger.debug('Found embedded subtitle %r with enzyme', embedded_subtitle_languages) video.subtitle_languages |= embedded_subtitle_languages else: logger.debug('MKV has no subtitle track') except EnzymeError: logger.error('Parsing video metadata with enzyme failed') except Exception: logger.error("Parsing video with enzyme has gone terribly wrong: %s", traceback.format_exc()) return video
def test_video_fromguess_wrong_type(episodes): guess = {"type": "subtitle"} with pytest.raises(ValueError) as excinfo: Video.fromguess(episodes["bbt_s07e05"].name, guess) assert str(excinfo.value) == "The guess must be an episode or a movie guess"