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()
class OpenSubtitleSession: def __init__(self, username, password): self.username = username self.password = password self._ost = OpenSubtitles() def __enter__(self): token = self._ost.login(self.username, self.password) if not token: raise ValueError('Failed to login') return self._ost def __exit__(self, exc_type, exc_val, exc_tb): self._ost.logout()
class CommonMetadataOpenSubtitles(object): """ Class for interfacing with Opensubtitles """ def __init__(self, user_name, user_password): self.opensubtitles_inst = OpenSubtitles() self.token = self.opensubtitles_inst.login(user_name, user_password) 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 com_meta_opensub_ping(self): self.opensubtitles_inst.no_operation() def com_meta_opensub_logoff(self): self.opensubtitles_inst.logout()
def main(): # Run the program in current directory # Get login details from user ( username, password ) = get_login_credentials() # The OpenSubtitles object ost = OpenSubtitles() # Login to opensubtitles.org through the API print("Logging in...") while ost.login(username, password) is None: print("Login failed, try again.") ( username, password ) = get_login_credentials() # Creates the Subs directory if it doesn't exist create_subs_dir() # Gets a list of all the video file names in the directory (.mp4 or .mkv) filename_list = get_video_filenames() video_list = to_video_object_list(filename_list) print("Downloading all subs...") # Search and download subtitles for all videos for video in video_list: print("Downloading " + video.search_name + " S" + video.season + " E" + video.episode + "Subs") # Creates a list of all the searched subtitles search_data = ost.search_subtitles([{ 'sublanguageid': 'eng', 'query': video.search_name.lower(), 'season': video.season, 'episode': video.episode }]) # Finds subtitle file ID of most downloaded subtitles file subtitle_file_id = find_most_downloaded(search_data) ost.download_subtitles([subtitle_file_id], output_directory='.\\Subs\\', override_filenames={str(subtitle_file_id): video.file_name + '-eng.srt'}, extension='srt') print("Done!") print("Logging out...") ost.logout()
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()) data = gzip.GzipFile(fileobj=buf).read() out = open('titles.srt', 'wb') out.write(data) out.close() break fd.logout()
def download_subtitles(initial_path, user, password, languages, recursive=True): ''' Logins into opensubtitles.org and start the recursive search ''' global config global failed_list try: #if os.path.isfile(config_file): with open(config_file) as f: config = json.load(f) except: config = { 'last_download_date': None, 'today_download_count': 0, 'username': '', 'password': '', 'languages': ['por', 'pob', 'eng'], 'initial_path': initial_path } today = datetime.date.today().strftime("%Y%m%d") if config['last_download_date'] != today: config['last_download_date'] = today config['today_download_count'] = 0 if user == None: user = config['username'] else: config['username'] = user if user != None: password = config['password'] else: config['password'] = password config['languages'] = languages config['initial_path'] = initial_path if not config.has_key('ignore_previously_failed'): config['ignore_previously_failed'] = True _save_config() if user == None or password == None: print ''' ERROR: You need to provide login info for OpenSubitles. Either:\n - Pass user/password OR - Edit config/config.json to set a fix user/password ''' return False if os.path.isfile(failed_list_file): with open(failed_list_file) as f: failed_list = f.readlines() failed_list = [x.strip() for x in failed_list] print "Connecting to OpenSubtitles.org..." osub = OpenSubtitles() time.sleep(0.1) for i in range(6): try: token = osub.login(user, password) if token == None: print "---> Error. Invalid username/password. Please check your login information with OpenSubtitles.org" return False else: break except: time.sleep(1) if i < 5: print "---> Connection trial #%d failed" % (i + 1) else: print "ERROR: Could not connect to OpenSubtitle.org" return False _download_subtitles_at_path(initial_path, osub, languages, recursive) osub.logout() remaining_downloads = MAX_DOWNLOADS_PER_DAY - config['today_download_count'] print "Done!" print "%d subtitles downloaded at this session" % (success_downloads) print "%d downloads failed at this session" % (failed_downloads) print "You can still download %d subtitles today from OpenSubtitles.org (Max per day:%d)" % ( remaining_downloads, MAX_DOWNLOADS_PER_DAY) print "" print "%d video files not considered during this search\n(Either already had subtitle or had failed in a previous search)" % ( skipped_files) print "(If you want to try downloading them again, please remove their names from the following file, or delete this file)" print failed_list_file #$with open(failed_list_file, 'w') as f: # for item in failed_list: # f.write("%s\n" % (item)) return True
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()) data = gzip.GzipFile(fileobj=buf).read() out = open('titles.srt', 'wb') out.write(data) out.close() break fd.logout()
class TestOpenSubtitles(unittest.TestCase): def setUp(self): self.mock = MockServerProxy() self.ost = OpenSubtitles() self.ost.xmlrpc = self.mock def test_login(self): self.mock.LogIn = lambda *_: { 'status': '403', } assert self.ost.login('*****@*****.**', 'goodpassword') is None self.mock.LogIn = lambda *_: { 'status': '200 OK', 'token': 'token', } assert self.ost.login('*****@*****.**', 'goodpassword') == 'token' def test_search_subtitles(self): self.mock.SearchSubtitles = lambda *_: fixture('search_subtitles') data = self.ost.search_subtitles([]) assert len(data) == 1 assert data[0].get('IDSubtitle') == '7783633' assert data[0].get('IDSubtitleFile') == '1956355942' assert data[0].get('IDSubMovieFile') == '19353776' def test_search_imdb(self): self.mock.SearchMoviesOnIMDB = lambda *_: { 'status': '200 OK', 'data': [{ 'IDMovieImdb': 'id', }] } # TODO: not sure if these are the right params. I am just keeping the test because it was on the README data = self.ost.search_movies_on_imdb([]) assert data[0].get('IDMovieImdb') == 'id' def test_no_operation(self): self.mock.NoOperation = lambda *_: {'status': '200 OK'} assert self.ost.no_operation() def test_logout(self): self.mock.LogOut = lambda *_: {'status': '403'} assert not self.ost.logout() self.mock.LogOut = lambda *_: {'status': '200 OK'} assert self.ost.logout() def test_auto_update(self): self.mock.AutoUpdate = lambda *_: { 'status': '200 OK', 'version': 'something', } data = self.ost.auto_update('SubDownloader') assert 'version' in data.keys() def test_already_exists(self): self.mock.TryUploadSubtitles = lambda *_: { 'status': '403', } # TODO: The error here is unauthorized and not that the subtitle wasn't found, # however, for not breaking compatibility we will keep it this way for now. assert not self.ost.try_upload_subtitles([]) self.mock.TryUploadSubtitles = lambda *_: { 'status': '200 OK', 'alreadyindb': 1, } assert self.ost.try_upload_subtitles([]) def test_upload_subtitles(self): self.mock.UploadSubtitles = lambda *_: { 'status': '200 OK', 'data': { 'url': 'http://example.com', }, } data = self.ost.upload_subtitles([]) assert 'url' in data.keys() def test_check_subtitle_hash(self): self.mock.CheckSubHash = lambda *_: { 'status': '200 OK', 'data': {}, } data = self.ost.check_subtitle_hash([]) assert data == {} def test_check_movie_hash(self): self.mock.CheckMovieHash = lambda *_: { 'status': '200 OK', 'data': {}, } data = self.ost.check_movie_hash([]) assert data == {} def test_insert_movie_hash(self): self.mock.InsertMovieHash = lambda *_: { 'status': '200 OK', 'data': {}, } data = self.ost.insert_movie_hash([]) assert data == {} def test_report_wrong_movie_hash(self): self.mock.ReportWrongMovieHash = lambda *_: { 'status': '419', } assert not self.ost.report_wrong_movie_hash([]) self.mock.ReportWrongMovieHash = lambda *_: { 'status': '200 OK', } assert self.ost.report_wrong_movie_hash([]) def test_report_wrong_movie_hash_404(self): self.mock.ReportWrongMovieHash = lambda *_: { 'status': '404', } assert not self.ost.report_wrong_movie_hash('hash') self.mock.ReportWrongMovieHash = lambda *_: { 'status': '200 OK', } assert self.ost.report_wrong_movie_hash('hash') def test_get_subtitle_languages(self): self.mock.GetSubLanguages = lambda *_: { 'status': '200 OK', 'data': {}, } assert self.ost.get_subtitle_languages() == {} def test_get_available_translations(self): self.mock.GetAvailableTranslations = lambda *_: { 'status': '200 OK', 'data': {}, } assert self.ost.get_available_translations('SubDownloader') == {} def test_subtitles_votes(self): self.mock.SubtitlesVote = lambda *_: { 'status': '200 OK', 'data': {}, } assert self.ost.subtitles_votes({}) == {} def test_get_comments(self): self.mock.GetComments = lambda *_: { 'status': '200 OK', 'data': {}, } assert self.ost.get_comments([]) == {} def test_add_comment(self): self.mock.AddComment = lambda *_: { 'status': '403', } assert not self.ost.add_comment({}) self.mock.AddComment = lambda *_: { 'status': '200 OK', } assert self.ost.add_comment({}) def test_add_request(self): self.mock.AddRequest = lambda *_: { 'status': '200 OK', 'data': {}, } assert self.ost.add_request({}) == {} def test_download_subtitles(self): self.mock.DownloadSubtitles = lambda *_: fixture('download_subtitles') with tempfile.TemporaryDirectory() as tmpdirname: data = self.ost.download_subtitles(['id'], output_directory=tmpdirname) assert data, data @mock.patch('pythonopensubtitles.opensubtitles.decompress', return_value='test_decoded_data') def test_download_subtitles_force_encoding(self, mock_decompress): self.mock.DownloadSubtitles = lambda *_: fixture('download_subtitles') with tempfile.TemporaryDirectory() as tmpdirname: data = self.ost.download_subtitles(['id'], output_directory=tmpdirname, encoding='test_encoding') encoded_data = self.ost._get_from_data_or_none('data') mock_decompress.assert_called_with(encoded_data[0]['data'], encoding='test_encoding') assert data, data