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!")
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
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)
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
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
def main(): pathList = [] downloadList = [] #if len(sys.argv) < 1 : # print "Missing Arguments" # quit() folder = "/home/Downloads/torrents" #print sys.argv[1] for root, dirs, files in os.walk(folder): path = root.split('/') #print root for file in files: if(file.endswith(".mp4") or file.endswith(".avi") or file.endswith(".mkv") ): print os.path.join(root,file) pathList.append(os.path.join(root,file)) print "Found %d files" % len(pathList) if len(pathList) >= 1: ops = OpenSubtitles() token = ops.login("","") print token for subToFind in pathList: f = File(subToFind) dirname = os.path.normpath(subToFind) subData = ops.search_subtitles([{'sublanguageid': 'pob','moviehash': f.get_hash() , 'moviebytesize': f.size }]) if not subData: print "Sub not found for %s " % f.path else: filename = '' if f.path.endswith(".mp4"): filename = dirname.replace('.mp4', '.srt') elif f.path.endswith('.mkv'): filename = dirname.replace('.mkv', '.srt') elif f.path.endswith('.avi'): filename = dirname.replace('.avi', '.srt') response = urllib2.urlopen(subData[0]['SubDownloadLink']) compressedFile = StringIO.StringIO() compressedFile.write(response.read()) response.close() compressedFile.seek(0) decompressedFile = gzip.GzipFile(fileobj=compressedFile,mode='rb') with open(filename ,'w') as outfile: outfile.write(decompressedFile.read()) time.sleep(1) ops.logout()
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
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")
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
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
def get_subtitle(filename): os_sub = OpenSubtitles() try: token = os_sub.login('thegyro','idontlikepasswords') if os.path.exists(os.path.join(source_dir,filename)): video_file = File(os.path.join(source_dir,filename)) video_hash = video_file.get_hash() video_size = video_file.size sub_param = {'sublanguageid':'English','moviehash':video_hash,'moviebytesize': video_size} subtitles = os_sub.search_subtitles([sub_param]) print subtitles else: raise Exception("File doesn't exist") except Exception as ex: print str(ex)
def downloadSubs(Parameters): #Assign/allocate object and get token after logging in with credentials from the Parameters object opensubs = OpenSubtitles() token = opensubs.login(Parameters.username, Parameters.password) if token is None: print '\n*** Login failed! ***\n' sys.exit() #Get hash and size of file from Parameters object f = File(os.path.join(Parameters.path, Parameters.video)) print '\tPath: %s' % Parameters.path print '\tFile: %s' % Parameters.video hash = f.get_hash() size = f.get_size() #Search subtitles DB using file hash and size. Looks like the first result is the best matching result data = opensubs.search_subtitles([{'sublanguageid': 'eng', 'moviehash': hash, 'moviebytesize': size}]) if data: #Download first result, decode it from BASE64, add gz extension, save file download = opensubs.download_subtitles([data[0]['IDSubtitleFile']]) data_decoded = base64.b64decode(unicode(download[0]['data'])) gz_file = os.path.join(Parameters.path, Parameters.subtitle) + '.gz' print '\nCreating gz file: %s' % gz_file download_file = open(gz_file,'w') download_file.write(data_decoded) download_file.close() print 'Created gz file: %s' % gz_file #Extract SRT file from gz file and place it in the same folder print 'Opening gz file: %s' % gz_file srt_file_buffer = gzip.open(gz_file, 'r') srt_file_name = os.path.join(Parameters.path, Parameters.subtitle) print 'Creating SRT file: %s' % srt_file_name srt_file = open(srt_file_name,'w') srt_file.write(srt_file_buffer.read()) srt_file.close() print 'Created SRT file: %s' % srt_file_name #Delete .gz file print 'Deleting %s' % gz_file os.remove(gz_file) else: print '*** No match found for file! ***'
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", )
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)
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
def test_hash(self): f = File(self.temp.name) assert f.get_hash() == '1e1e1e1e1e200000'
] 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], output_directory=movie_subtitle_folder, extension='srt') time.sleep(1)
def find_subtitles_by_hash(self, source): f = File(source) return self.find_subtitles(moviehash=f.get_hash(), moviebytesize=f.size)
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: print(item['SubDownloadLink']) request = urllib2.Request(item['SubDownloadLink']) response = urllib2.urlopen(request) buf = StringIO(response.read())
def com_meta_opensub_search(self, file_name): f = File(file_name) return self.opensubtitles_inst.search_subtitles([{'sublanguageid': 'all', 'moviehash': f.get_hash(), 'moviebytesize': f.size}])
def opensub(relpath): """OpenSubtitle identification of movie Uses the OpenSubtitles API to identify movie Args: relpath(str): relative path of movie file Returns: imdb_id(int): on success, returns idetified imdb id None: on failure Raises: None """ if 'sub' not in opensub.__dict__: sub = opensub_initiate() if sub: opensub.sub = sub token = sub.login( OpenSubKey.get_solo().uid, OpenSubKey.get_solo().key ) opensub.token = token opensub.lock = Lock() try: # login to opensub using API sub = opensub.sub token = opensub.token if not token: # return sub print 'null token' log.error('path: %s open sub null token' % relpath) return # check that the file is accessible if not path.exists(path.join( HDDRoot.get_solo().path, MovieFolder.get_solo().relpath, relpath, )): print "ERROR: " + relpath log.error('path: %s does not exist' % relpath) return f = File(path.join( HDDRoot.get_solo().path, MovieFolder.get_solo().relpath, relpath, )) if f is None: log.error('path: %s open sub file error' % relpath) return hash = f.get_hash() size = f.size with opensub.lock: data = sub.search_subtitles([{ 'sublanguageid': 'all', 'moviehash': hash, 'moviebytesize': size, }]) if type(data) is list: if data[0].get('IDMovieImdb', None): return data[0]['IDMovieImdb'] else: log.warning('%s opensub failed to identify movie' % relpath) except ProtocolError as e: # most likely network error or API server error print "E: " + str(e) except AssertionError: print 'Failed to authenticate opensubtitles login.' except ProtocolError as e: """ TODO: gaierror: [Errno 8] nodename nor servname provided, or not known """ if e.errcode == 503: # ProtocolError - 503 Service Unavailable print 'Check network connection and try again.' log.error('path: %s open sub error occured' % relpath) log.error(traceback.format_exc()) except Exception: log.error('path: %s open sub error occured' % relpath) log.error(traceback.format_exc())
def calculate_hash(self, filePath): logger.debug("filePath: {}".format(filePath)) f = File(filePath) hash = f.get_hash() logger.debug("hash: {}".format(hash)) return hash
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}'")
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'))
filter(lambda x: x in printable, s.lower().replace(" ", "-"))) ost = OpenSubtitles() from pythonopensubtitles.utils import File as OstFile token = ost.login('', '') print('OpenSubtitles token: %s' % token, file=sys.stderr) movies_list = [] movies_without_reference_sub_count = 0 for file_idx, file_path in enumerate(video_paths): f = OstFile(file_path) file_hash = f.get_hash() print(file=sys.stderr) print('-------------------------------------------------------', file=sys.stderr) print('Movie `%s` with hash `%s`:' % (file_path, file_hash), file=sys.stderr) subtitle_files = ost.search_subtitles([{ 'moviehash': file_hash, 'sublanguageid': 'eng' }]) if len(subtitle_files) == 0: file_basename = os.path.splitext(os.path.basename(file_path))[0] print('Video file `%s` not registered on OpenSubtitles' % file_path, file=sys.stderr)