Пример #1
0
    def search_subtitles(self):
        """
            search subtitles for a given file
        :return: Opensubtitles search data or None
        """

        try:
            movie_file = File(self.path)
        except:
            print "Error Calculating File Size"
            return False

        file_hash = movie_file.get_hash()
        if "Error" in file_hash:
            print "Error Calculating File Hash"
            return False

        if not self.is_auth:
            print "Error Connecting to OpenSubtitles"
            return False

        # search function
        searchdata = self.opensubtitles.search_subtitles([{
            'sublanguageid':
            self.language,
            'moviehash':
            file_hash,
            'moviebytesize':
            movie_file.size
        }])

        return searchdata
Пример #2
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "file",
        help="Path to the file you want to download subtitles for",
        type=str)

    args = parser.parse_args()

    if os.path.exists(args.file):
        f = File(os.path.abspath(args.file))
        path, file = os.path.split(args.file)
        path_full_nofile = os.path.abspath(path)
        path_file = file
        path_file_noext = os.path.splitext(args.file)[0]
    else:
        sys.exit("File does not exist")

    ost = OpenSubtitles()

    username = input("Please input your OpenSubtitles.org username: "******"Please input your OpenSubtitles.org password: "******"Logging in, please wait...")
    ost.login(username, password)
    print("Successfully logged in!")

    hash = f.get_hash()
    size = f.size

    print("Scouring the web for subtitles, please wait...")
    data = ost.search_subtitles([{
        'sublanguageid': 'eng',
        'moviehash': hash,
        'moviebytesize': size
    }])
    bestdic = None

    highestdownloadcount = 0
    for dict in data:
        downloadcount = int(dict.get('SubDownloadsCnt'))
        if downloadcount > highestdownloadcount:
            downloadcount = highestdownloadcount
            bestdic = dict

    print("Subtitle found in", bestdic.get('SubLanguageID'), "with",
          bestdic.get('SubDownloadsCnt'), "downloads.")
    print("Downloading subtitles, please wait...")
    urllib.request.urlretrieve(
        bestdic.get('SubDownloadLink'),
        path_full_nofile + "/" + path_file_noext + ".srt.gz")
    print("Subtitle downloaded!")
    print("Unzipping subtitle, please wait...")
    inF = gzip.open(path_full_nofile + "/" + path_file_noext + ".srt.gz", "rb")
    outF = open(path_full_nofile + "/" + path_file_noext + ".srt", "wb")
    outF.write(inF.read())
    inF.close()
    outF.close()
    os.remove(path_full_nofile + "/" + path_file_noext + ".srt.gz")
    print("Done!")
Пример #3
0
def downloader(file_list, search):

        for file_path in file_list:

            if search == 'hash':                                        
                fname = os.path.split(file_path)                    # fname stores file name and root directory path in a tuple
                #print('fname', '      ', fname)
                file_root = fname[0]                                             
                print('some more bs', file_root, '\n', file_path)
                f = File(file_path)                                 # File class has methods to get hash and size of file
                data = ost.search_subtitles([{'sublanguageid': 'all', 'moviehash': f.get_hash(), 'moviebytesize': f.size}])     # this method stores multiple subtitle's data in a list in multiple dictionaries using file hash and size.
                                                                                                                                # data stores hash, size ,imdbrating, and much more
            elif search == 'query':
                fname = os.path.split(nfdict[file_path])            
                #print('fname', '      ', fname)
                file_root = fname[0]
                print('some more bs', file_root, '\n', file_path)
                print('file root:  ', file_root)
                data = ost.search_subtitles([{'sublanguageid': 'all', 'query': file_path}])        # uses file name to search for subs and stores sub data in a list.
            
            if data == []:
                print('Could not find sub in opensubtitles.org')    # if sub not found data will be empty
                                                                    # file path and name will be adden to a dict as values and keys 
                if search == 'query':                               # respectively for hash search, and only name to a list for name search
                    newlist.append(fname[1])
                
                elif search == 'hash':
                    nfdict[fname[1]] = file_path
                
                continue
            
            else:
                #print("\n\n\n\n check check \n\n ")
                #print(data)
                id_subtitle_file = data[0].get('IDSubtitleFile')    # gets sub id (a 6 digit num)
                #print(data)
                imdb_rating(goodmv1, avgmv1, tmv1, data, fname)
                newsub = file_root + s + fname[1][:-4] + '.srt'     # a string with the name of the final sub file
                                                                    # used below to check if file already exists.
                if os.path.exists(newsub):
                    print("sub for this exists so i don't think downloading it again will do shit....")
                    continue
                
                else:                                               # downloads subtitles using sub id from data and stores sub in output dir.
                    ost.download_subtitles([id_subtitle_file], output_directory=file_root, extension='srt')
                    sub = file_root + s + str(id_subtitle_file) + '.srt'
                    
                    if os.path.exists(sub) == False:                # debugging to check if download failed
                        print("Probably subtitle downloading limit reached(for the account).")

                    try:
                        rename_file(file_root, id_subtitle_file, fname)
                        
                    except FileExistsError:
                        print('file exists so deleting the newly made file')
                        os.remove(sub)
Пример #4
0
def main(video):
    log("info", "Loaded module subtitles")
    # Logging in to OST
    log("info", "Logging in to OpenSubtitles")
    ost = OpenSubtitles()
    token = ost.login(opensubtitles_username, opensubtitles_password)
    if isinstance(token, str):
        log("success", "Logged in to OpenSubtitles")
    else:
        log("critical", "Invalid username / password entered")
        return

    # Opening File
    file_path = video_path + video
    if not path.exists(file_path):
        log("critical", "Could not find specified video file '" + file_path + "'")
        return

    # Hashing file
    log("info", "Generating video hash...")
    f = File(file_path)
    hash = f.get_hash()
    log("success", "File hash generated: " + hash)

    # Searching OST
    log("info", "Querying OpenSubtitles for subtitles...")
    data = ost.search_subtitles([{'sublanguageid': 'all', 'moviehash': hash}])
    if len(data) > 0:
        log("success", f"Found {len(data)} results.")
    else:
        log("warning", "No results found.")
        # TODO: Implement series and episode-based downloading
        return
    subtitle_id = data[0]["IDSubtitleFile"]
    log("info", "Attempting download of subtitles with ID " + str(subtitle_id))
    try:
        if isinstance(ost.download_subtitles([subtitle_id], override_filenames={subtitle_id: video + '.srt'}, output_directory=video_path, extension='srt'), dict):
            log("success", "Subtitles successfully downloaded. Enjoy your video!")
        else:
            log("critical", "Subtitle download failed.")
            return
    except:
        #TODO: Make neater
        log("error", "Something went wrong. Trying second option in list(?)")
        subtitle_id = data[1]["IDSubtitleFile"]
        log("info", "Attempting download of subtitles with ID " + str(subtitle_id))
        try:
            if isinstance(ost.download_subtitles([subtitle_id], override_filenames={subtitle_id: video + '.srt'}, output_directory=video_path, extension='srt'), dict):
                log("success", "Subtitles successfully downloaded. Enjoy your video!")
            else:
                log("critical", "Subtitle download failed.")
                return
        except:
            log("critical", "giving up after too many tries")
            return
Пример #5
0
def download_all_subtitles(filepath):
    dirname = os.path.dirname(filepath)
    basename = os.path.basename(filepath)
    basename_without_ext = os.path.splitext(basename)[0]
    ost = OpenSubtitles()
    ost.login(None, None)
    f = File(filepath)
    h = f.get_hash()
    results_from_hash = (ost.search_subtitles([{
        "sublanguageid": "all",
        "moviehash": h
    }]) or [])
    languages_in_results_from_hash = [
        lang_id
        for lang_id in [r.get("SubLanguageID") for r in results_from_hash]
    ]
    results_from_filename = [
        r for r in ost.search_subtitles([{
            "sublanguageid": "all",
            "query": basename_without_ext
        }])
    ]
    results_from_filename_but_not_from_hash = [
        r for r in results_from_filename if r.get("SubLanguageID")
        and r.get("SubLanguageID") not in languages_in_results_from_hash
    ]
    results = results_from_hash + results_from_filename_but_not_from_hash
    wait_before_next_chunk = False
    for chunk in _chunks(results, 10):
        sub_ids = {
            r["IDSubtitleFile"]: f'{basename_without_ext}.{r["ISO639"]}.srt'
            for r in chunk
        }

        def _download_subtitle_chunk(retries=5):
            nonlocal ost
            try:
                ost.download_subtitles(
                    [_id for _id in sub_ids.keys()],
                    override_filenames=sub_ids,
                    output_directory=dirname,
                    extension="srt",
                )
            except ProtocolError as e:
                if retries == 0:
                    raise e
                time.sleep(10)
                ost = OpenSubtitles()
                ost.login(None, None)
                _download_subtitle_chunk(retries=retries - 1)

        if wait_before_next_chunk:
            time.sleep(10)
        _download_subtitle_chunk()
        wait_before_next_chunk = True
Пример #6
0
def find_subtitles(path, langid='all'):
    ''' Get a list of subtitles for a given file '''
    f = File(path)
    hash = f.get_hash()
    assert type(hash) == str
    size = f.size
    assert isinstance(size, Number)
    data = osmgr.search_subtitles([{
        'sublanguageid': langid,
        'moviehash': hash,
        'moviebytesize': size
    }])
    assert type(data) == list
    return data
Пример #7
0
def downloadSub():
    names = os.listdir()
    for name in names:
        if (name.endswith(tuple(ext))):
            file_name, file_ext = splitext(name)

            class Test(object):
                usename = 'ermisss'
                password = '******'
                video = name
                path = currdir

            # Test.video=file_name
            fullPath = currdir + '/' + file_name

            token = ost.login('ermisss', '258456++')
            assert type(token) == str

            f = File(path.join(Test.path, Test.video))
            hash = f.get_hash()
            assert type(hash) == str

            log.write("hash:" + hash + "\n")

            size = f.size
            assert type(size) == str

            log.write("size:" + size + "\n")

            dataid = ost.search_subtitles([{
                'sublanguageid': 'tur',
                'moviehash': hash,
                'moviebytesize': size
            }])
            log.write("id:" + dataid + "\n")
            id_subtitle = dataid[0].get('IDSubtitle')
            id_sub_movie_file = dataid[0].get('IDSubMovieFile')

            subURL = dataid[0].get('SubDownloadLink')
            assert type(dataid) == list

            urllib.request.urlretrieve(subURL, fullPath + ".srt.gz")
            with gzip.open(fullPath + '.srt.gz', 'rb') as f_in:
                with open(fullPath + '.srt', 'wb') as f_out:
                    shutil.copyfileobj(f_in, f_out)

            if os.path.exists(fullPath + '.srt.gz'):
                os.remove(fullPath + '.srt.gz')

            log.write(name + "\n")
Пример #8
0
 def download_subtitle(self):
     """
     Downloads subtitles from opensubtitles.org, in the defined language
     and stores them in a tempfile.
     Search is trying to match movie by hash and if it is unsuccessful,
     it searches by movie name.
     Only first match is considered.
     """
     ost = OpenSubtitles()
     logged_in = ost.login(self.os_username, self.os_password)
     if not logged_in:
         raise Error("Wrong opensubtitles credentials")
     # TODO refactor to consume path on event, potentially from queue
     mkv_files = [mkv for mkv in self.watch_path.glob("*.mkv")]
     subs = list()
     for movie in mkv_files:
         movie_file = File(movie.absolute())
         # search by hash, if not, by name
         ost_data = ost.search_subtitles([
             {
                 "sublanguageid":
                 self.os_language
                 if len(self.os_language) == 3 else self.os_language,
                 "moviehash":
                 movie_file.get_hash(),
             },
             {
                 "sublanguageid":
                 self.os_language
                 if len(self.os_language) == 3 else self.os_language,
                 "query":
                 movie.name,
             },
         ])
         if ost_data:
             # #  downloading first subtitle
             plain_link = ost_data[0]["SubDownloadLink"]
             sub_link_parts = plain_link.split("/download/")
             #  rebuilding link to get utf-8 subtitle
             sub_link = (sub_link_parts[0] + "/download/subencoding-utf8/" +
                         sub_link_parts[1])
             response = requests.get(sub_link)
             tmp, tmp_name = tempfile.mkstemp()
             with open(tmp, "wb") as srt_out:
                 srt_out.write(gzip.decompress(response.content))
             subs.append({"file_path": movie, "sub": tmp_name})
         else:
             subs.append({"file_path": movie, "sub": None})
     return subs
Пример #9
0
def download_all_subtitles(filepath):
    dirname = os.path.dirname(filepath)
    basename = os.path.basename(filepath)
    basename_without_ext = os.path.splitext(basename)[0]
    ost = OpenSubtitles()
    ost.login(None, None)
    f = File(filepath)
    h = f.get_hash()
    results = ost.search_subtitles([{"sublanguageid": "all", "moviehash": h}])
    for chunk in _chunks(results, 20):
        sub_ids = {
            r["IDSubtitleFile"]:
            f'{basename_without_ext}.{r["SubLanguageID"]}.srt'
            for r in chunk
        }
        ost.download_subtitles(
            [_id for _id in sub_ids.keys()],
            override_filenames=sub_ids,
            output_directory=dirname,
            extension="srt",
        )
Пример #10
0
 def find_subtitles_by_hash(self, source):
     f = File(source)
     return self.find_subtitles(moviehash=f.get_hash(), moviebytesize=f.size)
Пример #11
0
 def calculate_hash(self, filePath):
     logger.debug("filePath: {}".format(filePath))
     f = File(filePath)
     hash = f.get_hash()
     logger.debug("hash: {}".format(hash))
     return hash
Пример #12
0
def download_all_subtitles(filepath, skip=[]):
    dirname = os.path.dirname(filepath)
    basename = os.path.basename(filepath)
    basename_without_ext = os.path.splitext(basename)[0]
    ost = OpenSubtitles()
    ost.login(settings.OPENSUBTITLES_USERNAME, settings.OPENSUBTITLES_PASSWORD)
    f = File(filepath)
    h = f.get_hash()
    language_ids = [
        languages.get(part1=lang).part2b
        for lang in settings.SUBTITLE_LANGUAGES if lang not in skip
    ]
    results_from_hash = ([
        item for sublist in [
            ost.search_subtitles([{
                "sublanguageid": langid,
                "moviehash": h
            }]) or [] for langid in language_ids
        ] for item in sublist
    ])
    languages_in_results_from_hash = [
        lang_id
        for lang_id in [r.get("SubLanguageID") for r in results_from_hash]
    ]
    results_from_filename = ([
        item for sublist in [
            ost.search_subtitles([{
                "sublanguageid": langid,
                "query": basename_without_ext
            }]) or [] for langid in language_ids
        ] for item in sublist
    ])
    results_from_filename_but_not_from_hash = [
        r for r in results_from_filename if r.get("SubLanguageID")
        and r.get("SubLanguageID") not in languages_in_results_from_hash
    ]
    results = results_from_hash + results_from_filename_but_not_from_hash
    results = [
        r for r in results if r["ISO639"] in settings.SUBTITLE_LANGUAGES
    ]
    wait_before_next_chunk = False
    sub_filenames = []
    for chunk in _chunks(results, 10):
        sub_ids = {
            r["IDSubtitleFile"]: f'{basename_without_ext}.{r["ISO639"]}.srt'
            for r in chunk
        }
        sub_filenames = list(set(sub_filenames + list(sub_ids.values())))

        def _download_subtitle_chunk(retries=5):
            nonlocal ost
            if not sub_ids:
                return
            try:
                ost.download_subtitles(
                    [_id for _id in sub_ids.keys()],
                    override_filenames=sub_ids,
                    output_directory=dirname,
                    extension="srt",
                )
            except ProtocolError as e:
                if retries == 0:
                    raise e
                time.sleep(10)
                ost = OpenSubtitles()
                ost.login(None, None)
                _download_subtitle_chunk(retries=retries - 1)

        if wait_before_next_chunk:
            time.sleep(10)
        _download_subtitle_chunk()
        wait_before_next_chunk = True

    for sub_filename in sub_filenames:
        tmp_path = os.path.join(dirname, "fixed_" + sub_filename)
        output_path = os.path.join(dirname, sub_filename)
        os.system(
            f"timeout 5m alass '{filepath}' '{output_path}' '{tmp_path}'")
        os.system(f"mv '{tmp_path}' '{output_path}'")
Пример #13
0
    video = 'Trance.2013 WEBRip XViD juggs.avi'

    #title = 'Dark.City.1998.Directors.Cut.BRRip.H264.AAC.5.1ch.Gopo.srt'
    #subtitle = 'Dark.City.1998.Directors.Cut.BRRip.H264.AAC.5.1ch.Gopo.srt'


token = fd.login(Data.username, Data.password)
if not token:
    print("Chyba prihlaseni")
    sys.exit(1)

print(token)

from pythonopensubtitles.utils import File
f = File(os.path.join(Data.path, Data.video))
h = f.get_hash()
print("Hash: %s" % h)
print("Size: %f" % f.size)

data = fd.search_subtitles([{
    'sublanguageid': 'cze',
    'moviehash': h,
    'moviebytesize': f.size
}])

import urllib2
from StringIO import StringIO
import gzip

for item in data:
Пример #14
0
from pythonopensubtitles.utils import File

ost = OpenSubtitles()

auth_info = json.load(
    open(Path.home() / "Dropbox" / "scripts" / "opensubtitles.json"))
ost.login(auth_info["user"], auth_info["password"])

file_path = Path(sys.argv[1])
file_name = file_path.name
file_dir = file_path.parent

output_dir = file_dir / 'downloaded_subtitles'
output_dir.mkdir(exist_ok=True, parents=True)

f = File(file_path)

data = ost.search_subtitles([{
    'sublanguageid': 'eng',
    'moviehash': f.get_hash(),
    'moviebytesize': f.size
}])

for i, sub in enumerate(data):
    id_subtitle_file = sub.get('IDSubtitleFile')
    sub_file = file_path.stem + "_" + str(i) + ".srt"

    ost.download_subtitles(
        [id_subtitle_file],
        override_filenames={
            id_subtitle_file: sub_file,
Пример #15
0
 def test_hash(self):
     f = File(self.temp.name)
     assert f.get_hash() == '1e1e1e1e1e200000'
Пример #16
0
# --- title
print()
console.print(
    Panel.fit("sub_scan.py",
              box=rich.box.ASCII,
              style="bold magenta",
              border_style="bright_black"))

console.print(Markdown("> Checking file"))
file = sys.argv[-1]
console.print(file)  #sys.argv[1] is the file to upload
if not os.path.isfile(file):
    raise ("error: could not find file.")
else:
    print('> ok.')
f = File(file)
(path, filename) = os.path.split(os.path.abspath(file))
filename_root = filename[:-4]

console.print(Markdown("> Checking OpenSubtitles API access"))
ost = OpenSubtitles()
if ost.login(args.user, args.password) is None:
    raise (
        "error: cannot login to OpenSubtitles API using the given username/password."
    )
else:
    print('> ok.')

# best solution: hash size
# if no result => search for title on IMDB (manually)
# then search by IMDB movie id
Пример #17
0
def download(path, dir_mode=False):
    if not dir_mode:
        print("")

    print(
        colored(
            '=============================================================================',
            'yellow'))

    ost = OpenSubtitles()
    ost.login('subspy', 'subspy')

    f = File(path)

    data = ost.search_subtitles([{
        'sublanguageid': 'eng',
        'moviehash': f.get_hash(),
        'moviebytesize': f.size
    }])

    if data is None or len(data) == 0:
        print(
            colored(
                "Subtitles could not be found for " + os.path.basename(path),
                'red'))
        return

    best_match = {"index": 0, "ratio": 0}

    for current_index, search_result in enumerate(data):
        current_ratio = SequenceMatcher(
            None, search_result.get('MovieName'),
            os.path.basename(path).replace(".", " ")).ratio()

        if (current_ratio > best_match['ratio']):
            best_match['index'] = current_index
            best_match['ratio'] = current_ratio

        if (current_index > 10):
            break

    if (best_match['ratio'] < 0.5):
        print(
            colored(
                "Subtitles could not be found for " + os.path.basename(path),
                'red'))
        return

    print("[ TITLE  ] " + colored(
        data[best_match['index']].get('MovieName') + " (" +
        data[best_match['index']].get('MovieYear') + ")", 'cyan'))
    print("[ RATING ] " + colored(
        data[best_match['index']].get('MovieImdbRating') +
        "/10 on IMDb", 'cyan'))

    confidence = round(best_match['ratio'] * 100, 1)

    print("Matched with " + str(confidence) + '% confidence')

    id_subtitle_file = data[best_match['index']].get('IDSubtitleFile')

    existing_subtitle = os.path.join(
        os.path.dirname(path),
        Path(os.path.basename(path)).stem + ".srt")

    abort_flag = False
    suffix = ""

    if (os.path.isfile(existing_subtitle)):
        suffix = ".SubsPY"
        print(colored("\nSubtitles already exist for this file.", 'red'))

        if globals()['keep_all']:
            print(
                colored("Keeping both subtitles (added suffix) for ALL FILES.",
                        "cyan"))
        else:
            r = input(
                colored(
                    'Overwrite [o], keep existing [k], keep both [b] or keep both for all conflicts [a]? : ',
                    'magenta'))
            if r.lower() == "o":
                suffix = ""
                print(colored("Overwriting existing Subtitles", "cyan"))
            elif r.lower() == "k":
                print(colored("Skipping this download.", "cyan"))
                abort_flag = True
            elif r.lower() == "a":
                globals()['keep_all'] = True
                print(
                    colored(
                        "Keeping both subtitles (added suffix) for ALL FILES.",
                        "cyan"))
            else:
                print(
                    colored(
                        "Keeping both subtitles (added suffix) for this file.",
                        "cyan"))

    if not abort_flag:
        overrides = {
            id_subtitle_file:
            Path(os.path.basename(path)).stem + suffix + ".srt"
        }
        status = ost.download_subtitles([id_subtitle_file],
                                        override_filenames=overrides,
                                        output_directory=os.path.dirname(path),
                                        extension='srt')

        if status is None:
            input(
                colored(
                    "\nSubtitles could not be downloaded for " +
                    os.path.basename(path), 'red'))
            return

        print(colored("\nSubtitles downloaded successfully!", "green"))

    if not dir_mode:
        print(
            colored(
                '=============================================================================',
                'yellow'))
Пример #18
0
movies_features_to_extract = get_movies_status(movies_raw_path,
                                               movies_prep_path)
movies_to_extract = [
    k for k, v in movies_features_to_extract.items() if 'subtitle' in v
]
movies_to_extract
# -

# Loop through movie files, save their audio on folder prep
for movie_id in movies_to_extract:

    try:
        movie_file = movie_file_dict[movie_id]
        print(movie_id)

        f = File(str(movie_file))
        data = ost.search_subtitles([{
            'sublanguageid': 'eng',
            'imdbid': movie_id[2:],
            'moviehash': f.get_hash(),
            'moviebytesize': f.size
        }])
        id_subtitle_file = data[0].get('IDSubtitleFile')

        movie_prep_folder = movies_prep_path / f"{movie_id}"
        Path(movie_prep_folder).mkdir(parents=True, exist_ok=True)

        movie_subtitle_folder = movie_prep_folder / 'subtitle/'
        Path(movie_subtitle_folder).mkdir(parents=True, exist_ok=True)

        ost.download_subtitles([id_subtitle_file],
Пример #19
0
def process_file(video_path):
    f = File(video_path)
    ext = os.path.splitext(video_path)[1]

    for i in range(5):
        try:
            data = opensubtitles.search_subtitles([{
                'sublanguageid': args.language,
                'moviehash': f.get_hash(),
                'moviebytesize': f.size
            }])
            break
        except ProtocolError as e:
            print('Retrying...')
            sleep(5)

    if len(data) == 0:
        print('No subtitles found')
        return False

    print('Found %d %s subtitles' % (len(data), args.language), end='')

    max_download_count = -1
    subtitle = None
    for s in data:
        d = int(s['SubDownloadsCnt'])
        if d > max_download_count:
            max_download_count = d
            subtitle = s

    print(' with %d downloads' % max_download_count)

    url = subtitle['ZipDownloadLink']
    encoding = subtitle['SubEncoding']
    imdb_id = 'tt' + subtitle['IDMovieImdb'].rjust(7, '0')
    imdb_info = get_imdb_info(imdb_id)

    # series
    series_info = None
    if 'seriesID' in imdb_info:
        series_info = get_imdb_info(imdb_info['seriesID'])
        episode_info = imdb_info
        imdb_info = series_info

    title = imdb_info['Title']
    year = imdb_info['Year']
    if '–' in year:
        year = year[:year.find('–')]

    rating = imdb_info['imdbRating']
    print('Title: %s, score: %s, year: %s' % (title, rating, year))

    folder = os.path.join(args.destination,
                          title + ' (%s, %s)' % (year, rating))
    if series_info != None:
        folder = os.path.join(
            folder, 'Season_' + pad_with_zero(episode_info['Season']))
        title = pad_with_zero(
            episode_info['Episode']) + '-' + episode_info['Title']

    if not os.path.exists(folder):
        os.makedirs(folder)

    response = urllib.request.urlopen(url)
    data = response.read()
    zf = zipfile.ZipFile(io.BytesIO(data), 'r')
    for name in zf.namelist():
        if name.endswith('.srt'):
            content = zf.read(name)
            with open(os.path.join(folder, title + '.srt'), 'w') as f:
                f.write(content.decode(encoding))
            break

    if args.move:
        destination = os.path.join(folder, title + ext)
        if os.path.abspath(video_path) != os.path.abspath(destination):
            print('Moving video: ' + title + ext)
            shutil.move(video_path, destination)
        else:
            print('Not moving: same location')

    return True
# the link to the git repository of the library
# https://github.com/agonzalezro/python-opensubtitles
from pythonopensubtitles.opensubtitles import OpenSubtitles
from pythonopensubtitles.utils import File

ost = OpenSubtitles()
# the username and password of the opensubtitle account
ost.login("username", "password")

# the movie or episode path
f = File(r'path\to\file')

# you can get the subtitles Language Information with the function get_subtitle_language()
# ost.get_subtitle_languages()

# and then set the sublanguageid to the language id
data = ost.search_subtitles([{
    'sublanguageid': 'all',
    'moviehash': f.get_hash(),
    'moviebytesize': f.size
}])
id_subtitle_file = data[0].get('IDSubtitleFile')

# the output directory not set to any location so the subtitles will be downloaded in the
# current location
ost.download_subtitles([id_subtitle_file],
                       output_directory='',
                       extension='srt')
Пример #21
0
 def test_size(self):
     assert File(self.temp.name).size == "131072"
Пример #22
0
    def get_subtitles(self, resource, language=None):
        """
        Get the subtitles data for a video resource

        :param resource: Media file, torrent or URL to the media resource
        :type resource: str

        :param language: Language name or code (default: configured preferred language).
            Choose 'all' for all the languages
        :type language: str
        """

        from pythonopensubtitles.utils import File

        if resource.startswith('file://'):
            resource = resource[len('file://'):]

        resource = os.path.abspath(os.path.expanduser(resource))
        if not os.path.isfile(resource):
            return None, '{} is not a valid file'.format(resource)

        file = resource
        cwd = os.getcwd()
        media_dir = os.path.dirname(resource)
        os.chdir(media_dir)
        file = file.split(os.sep)[-1]

        local_subs = [{
            'IsLocal':
            True,
            'MovieName':
            '[Local subtitle]',
            'SubFileName':
            sub.split(os.sep)[-1],
            'SubDownloadLink':
            'file://' + os.path.join(media_dir, sub),
        } for sub in find_files_by_ext(media_dir, '.srt', '.vtt')]

        self.logger.info('Found {} local subtitles for {}'.format(
            len(local_subs), file))

        languages = [language.lower()] if language else self.languages

        try:
            file_hash = File(file).get_hash()
            subs = self._ost.search_subtitles([{
                'sublanguageid': 'all',
                'moviehash': file_hash,
            }])

            subs = [
                sub for sub in subs if not languages or languages[0] == 'all'
                or sub.get('LanguageName', '').lower() in languages
                or sub.get('SubLanguageID', '').lower() in languages
                or sub.get('ISO639', '').lower() in languages
            ]

            for sub in subs:
                sub['IsLocal'] = False

            self.logger.info('Found {} OpenSubtitles items for {}'.format(
                len(subs), file))

            return local_subs + subs
        finally:
            os.chdir(cwd)
Пример #23
0
import os
import time

EMAIL = "YOUR OpenSubtitles email"
PASSW = "YOUR OpenSubtitles password"

token = ost.login(EMAIL, PASSW)

filename=input("Enter path to file: ")
sub_path = os.path.dirname(filename)
name = '.'.join(filename.split('.')[0:-1])

sname = name+'.srt'
zname = name+'.zip'

f = File(filename)
hashh = f.get_hash()
size = f.size
print(hashh)
print(size)
data = ost.search_subtitles([{'sublanguageid': 'all', 'moviehash': hashh, 'moviebytesize': size}])
assert type(data)!=None

link = data[0]['ZipDownloadLink']
print(link)

file = open(os.path.join(sub_path,zname),'wb')
s = requests.get(link)
for chunk in s.iter_content(100000):
	if chunk:
		file.write(chunk)
Пример #24
0
def processOS(filename, detay):
    from pythonopensubtitles.opensubtitles import OpenSubtitles
    from pythonopensubtitles.utils import File
    trsubtitlelist = list()
    ensubtitlelist = list()

    ost = OpenSubtitles()
    ost.login('katates', 'hijuhiji')
    f = File(filename)
    data = ost.search_subtitles([{
        'imdbid': detay["imdb"],
        'season': detay["season"],
        'episode': detay["episode"],
        'sublanguageid': 'tur,eng',
        'tag': detay["group"] + "," + detay["vers"]
    }])
    for sub in data:
        realep = detay["episode"]

        name = "OS " + detay["name"]
        subhref = sub["SubDownloadLink"]
        sublang = sub["LanguageName"].replace("English",
                                              "en").replace("Turkish", "tr")
        subtrns = sub["SubTranslator"]
        subdown = int(sub["SubDownloadsCnt"])
        subep = sub["SeriesEpisode"]
        season = sub["SeriesSeason"]
        subgr = sub["InfoReleaseGroup"].lower()
        subvers = sub["InfoFormat"].lower()

        if int(realep) < 10:
            realep = realep[1:]

        if subep == realep:
            if "tr" == sublang:
                if detay["group"] in subgr:
                    if detay[
                            "vers"] in subvers:  # in because web can be web-dl or webrip
                        trsubtitlelist.append([
                            name, subhref, sublang, subtrns, subdown, subep,
                            season, subgr, subvers, 100
                        ])  # Both matches #1
                        break
                    else:
                        trsubtitlelist.append([
                            name, subhref, sublang, subtrns, subdown, subep,
                            season, subgr, subvers, 80
                        ])  # Only Group matches #3
                else:
                    if detay["vers"] in subvers:
                        trsubtitlelist.append([
                            name, subhref, sublang, subtrns, subdown, subep,
                            season, subgr, subvers, 90
                        ])  # Version matches #2
                    else:
                        trsubtitlelist.append([
                            name, subhref, sublang, subtrns, subdown, subep,
                            season, subgr, subvers, 0
                        ])  # Nothing matches #4
            else:
                if detay["group"] in subgr:
                    if detay[
                            "vers"] in subvers:  # in because web can be web-dl or webrip
                        ensubtitlelist.append([
                            name, subhref, sublang, subtrns, subdown, subep,
                            season, subgr, subvers, 100
                        ])  # Both matches #1
                    else:
                        ensubtitlelist.append([
                            name, subhref, sublang, subtrns, subdown, subep,
                            season, subgr, subvers, 80
                        ])  # Only Group matches #3
                else:
                    if detay["vers"] in subvers:
                        ensubtitlelist.append([
                            name, subhref, sublang, subtrns, subdown, subep,
                            season, subgr, subvers, 90
                        ])  # Version matches #2
                    else:
                        ensubtitlelist.append([
                            name, subhref, sublang, subtrns, subdown, subep,
                            season, subgr, subvers, 0
                        ])  # Nothing matches #4

    if len(trsubtitlelist) > 0:
        subtitlelistOS = sorted(trsubtitlelist,
                                key=lambda tup: (tup[-1], tup[4]),
                                reverse=True)
    else:
        subtitlelistOS = sorted(ensubtitlelist,
                                key=lambda tup: (tup[-1], tup[4]),
                                reverse=True)

    return subtitlelistOS