diff --git a/bin/periscope b/bin/periscope index f332fe9..8ac9d88 100755 --- a/bin/periscope +++ b/bin/periscope @@ -42,6 +42,7 @@ def main(): parser.add_option("-i", "--interactive", action="store_true", dest="interactive", help="ask which subtitle to download") parser.add_option("-q", "--query", action="append", dest="queries", help="query to send to the subtitles website") parser.add_option("--lang-in-name", action="store_true", dest='lang_in_name', help="append lang to subtitle filename") + parser.add_option("--subtitles-folder", action="store", type="string", dest="subtitles_folder", help="location of folder to save subtitles in") parser.add_option("--cache-folder", action="store", type="string", dest="cache_folder", help="location of the periscope cache/config folder (default is ~/.config/periscope)") parser.add_option("--list-plugins", action="store_true", dest="show_plugins", help="list all plugins supported by periscope") parser.add_option("--list-active-plugins", action="store_true", dest="show_active_plugins", help="list all plugins used to search subtitles (a subset of all the supported plugins)") @@ -70,8 +71,7 @@ def main(): exit() options.cache_folder = os.path.join(home, ".config", "periscope") - - periscope_client = periscope.Periscope(options.cache_folder) + periscope_client = periscope.Periscope(options.cache_folder) if options.show_active_plugins: print "Active plugins: " @@ -103,7 +103,7 @@ def main(): langs = periscope_client.preferedLanguages else: langs = options.langs - sub = periscope_client.downloadSubtitle(arg, langs, options.interactive) + sub = periscope_client.downloadSubtitle(arg, langs, options.interactive,options.subtitles_folder) if sub: subs.append(sub) @@ -118,6 +118,15 @@ def main(): shutil.move(s['subtitlepath'], new_name) s['subtitlepath'] = new_name + if options.subtitles_folder: + for s in subs: + new_path = "%s%s" % (options.subtitles_folder, os.path.basename(s['subtitlepath'])) + log.debug("New path: " + new_path) + shutil.move(s['subtitlepath'], new_path) + s['subtitlepath'] = new_path + else: + options.subtitles_folder = '.' + log.info("*"*50) log.info("Downloaded %s subtitles" %len(subs)) for s in subs: @@ -141,7 +150,8 @@ def recursive_search(entry, options): if mimetype in SUPPORTED_FORMATS: # Add it to the list only if there is not already one (or forced) basepath = os.path.splitext(entry)[0] - if options.force_download or not (os.path.exists(basepath+'.srt') or os.path.exists(basepath + '.sub')): +# print "****************" + options.subtitles_folder + '/' + os.path.basename(entry) + '.srt' + if options.force_download or not (os.path.exists(basepath+'.srt') or os.path.exists(basepath + '.sub') or os.path.exists(options.subtitles_folder + '/' + os.path.basename(entry) + '.srt' )): files.append(os.path.normpath(entry)) else: log.info("Skipping file %s as it already has a subtitle. Use the --force option to force the download" % entry) diff --git a/periscope/periscope.py b/periscope/periscope.py index 8c91646..62ab7e8 100755 --- a/periscope/periscope.py +++ b/periscope/periscope.py @@ -206,24 +206,24 @@ def selectBestSubtitle(self, subtitles, langs=["en"], interactive=False): return None #Could not find subtitles - def downloadSubtitle(self, filename, langs=None, interactive=False): + def downloadSubtitle(self, filename, langs=None, interactive=False,subtitles_folder=None): ''' Takes a filename and a language and creates ONE subtitle through plugins if interactive == True asks before downloading''' subtitles = self.listSubtitles(filename, langs) if subtitles: log.debug("All subtitles: ") log.debug(subtitles) - return self.attemptDownloadSubtitle(subtitles, langs, interactive) + return self.attemptDownloadSubtitle(subtitles, langs, interactive, subtitles_folder) else: return None - def attemptDownloadSubtitle(self, subtitles, langs, interactive=False): + def attemptDownloadSubtitle(self, subtitles, langs, interactive=False, subtitles_folder=None): subtitle = self.selectBestSubtitle(subtitles, langs, interactive) if subtitle: log.info("Trying to download subtitle: %s" %subtitle['link']) #Download the subtitle try: - subpath = subtitle["plugin"].createFile(subtitle) + subpath = subtitle["plugin"].createFile(subtitle,subtitles_folder) if subpath: subtitle["subtitlepath"] = subpath return subtitle diff --git a/periscope/plugins/Addic7ed.py b/periscope/plugins/Addic7ed.py index 13781d1..e49b472 100644 --- a/periscope/plugins/Addic7ed.py +++ b/periscope/plugins/Addic7ed.py @@ -60,7 +60,8 @@ def __init__(self, config, cache_folder_path): super(Addic7ed, self).__init__(langs=None,revertlangs=LANGUAGES) #http://www.addic7ed.com/serie/Smallville/9/11/Absolute_Justice self.host = "http://www.addic7ed.com" - self.release_pattern = re.compile(" \nVersion (.+), ([0-9]+).([0-9])+ MBs") + self.release_pattern = re.compile("Version (.+), ([0-9]+).([0-9])+ MBs") + logging.debug("Addic7ed plugin initialized") def process(self, filepath, langs): @@ -68,8 +69,10 @@ def process(self, filepath, langs): languages and it will query the subtitles source ''' fname = unicode(self.getFileName(filepath).lower()) guessedData = self.guessFileData(fname) + #logging.debug("Guessed data type: " + guessedData['type']) if guessedData['type'] == 'tvshow': subs = self.query(guessedData['name'], guessedData['season'], guessedData['episode'], guessedData['teams'], langs) +# logging.debug(guessedData['teams']) return subs else: return [] @@ -95,8 +98,11 @@ def query(self, name, season, episode, teams, langs=None): content = content.replace("The safer, easier way", "The safer, easier way \" />") soup = BeautifulSoup(content) + #logging.debug(soup) for subs in soup("td", {"class":"NewsTitle", "colspan" : "3"}): + #logging.debug(soup) if not self.release_pattern.match(str(subs.contents[1])): + logging.debug("No match found for: " + subs.contents[1]) continue subteams = self.release_pattern.match(str(subs.contents[1])).groups()[0].lower() @@ -111,17 +117,19 @@ def query(self, name, season, episode, teams, langs=None): logging.debug("[Addic7ed] Team from file: %s" %teams) logging.debug("[Addic7ed] match ? %s" %subteams.issubset(teams)) langs_html = subs.findNext("td", {"class" : "language"}) + #logging.debug(langs_html) + #logging.debug("[Addic7ed] Languages found: " + ','.join(langs_html)) lang = self.getLG(langs_html.contents[0].strip().replace(' ', '')) #logging.debug("[Addic7ed] Language : %s - lang : %s" %(langs_html, lang)) statusTD = langs_html.findNext("td") - status = statusTD.find("strong").string.strip() + status = statusTD.find("b").string.strip() # take the last one (most updated if it exists) links = statusTD.findNext("td").findAll("a") link = "%s%s"%(self.host,links[len(links)-1]["href"]) - #logging.debug("%s - match : %s - lang : %s" %(status == "Completed", subteams.issubset(teams), (not langs or lang in langs))) + logging.debug("%s - match : %s - lang : %s" %(status == "Completed", subteams.issubset(teams), (not langs or lang in langs))) if status == "Completed" and subteams.issubset(teams) and (not langs or lang in langs) : result = {} result["release"] = "%s.S%.2dE%.2d.%s" %(name.replace("_", ".").title(), int(season), int(episode), '.'.join(subteams) @@ -145,12 +153,14 @@ def splitTeam(self, subteams, sep): teams += t.split(sep) return teams - def createFile(self, subtitle): + def createFile(self, subtitle,subtitles_folder): '''pass the URL of the sub and the file it matches, will unzip it and return the path to the created file''' suburl = subtitle["link"] videofilename = subtitle["filename"] srtbasefilename = videofilename.rsplit(".", 1)[0] + if subtitles_folder: + srtbasefilename = subtitles_folder + os.path.basename(subtitle["filename"]) srtfilename = srtbasefilename +".srt" self.downloadFile(suburl, srtfilename) return srtfilename diff --git a/periscope/plugins/LegendasTV.py b/periscope/plugins/LegendasTV.py index df2aa74..e739f20 100644 --- a/periscope/plugins/LegendasTV.py +++ b/periscope/plugins/LegendasTV.py @@ -153,12 +153,14 @@ def LegendasTVLogin(self): request = urllib2.Request(self.url+'/login_verificar.php',login_data) response = urllib2.urlopen(request).read() - def createFile(self, subtitle): + def createFile(self, subtitle,subtitles_folder): '''pass the ID of the sub and the file it matches, will unzip it and return the path to the created file''' suburl = subtitle["link"] videofilename = subtitle["filename"] srtfilename = videofilename.rsplit(".", 1)[0] + '.srt' + if subtitles_folder: + srtbasefilename = subtitles_folder + os.path.basename(subtitle["filename"]) self.downloadFile(suburl, srtfilename) return srtfilename diff --git a/periscope/plugins/OpenSubtitles.py b/periscope/plugins/OpenSubtitles.py index f1c00be..ec6087e 100644 --- a/periscope/plugins/OpenSubtitles.py +++ b/periscope/plugins/OpenSubtitles.py @@ -99,12 +99,14 @@ def process(self, filepath, langs): fname = self.getFileName(filepath) return self.query(langs=langs, filename=fname) - def createFile(self, subtitle): + def createFile(self, subtitle,subtitles_folder): '''pass the URL of the sub and the file it matches, will unzip it and return the path to the created file''' suburl = subtitle["link"] videofilename = subtitle["filename"] srtbasefilename = videofilename.rsplit(".", 1)[0] + if subtitles_folder: + srtbasefilename = subtitles_folder + os.path.basename(subtitle["filename"]) self.downloadFile(suburl, srtbasefilename + ".srt.gz") f = gzip.open(srtbasefilename+".srt.gz") dump = open(srtbasefilename+".srt", "wb") diff --git a/periscope/plugins/Podnapisi.py b/periscope/plugins/Podnapisi.py index cbd420a..48c7c5f 100644 --- a/periscope/plugins/Podnapisi.py +++ b/periscope/plugins/Podnapisi.py @@ -102,7 +102,7 @@ def query(self, token, langs=None): log.debug(sublinks) return sublinks - def createFile(self, subtitle): + def createFile(self, subtitle,subtitles_folder): '''pass the URL of the sub and the file it matches, will unzip it and return the path to the created file''' subpage = subtitle["page"] @@ -119,5 +119,5 @@ def createFile(self, subtitle): dlimg = soup.find("img", {"title" : "Download"}) subtitle["link"] = self.host + dlimg.parent["href"] - SubtitleDatabase.SubtitleDB.createFile(self, subtitle) + SubtitleDatabase.SubtitleDB.createFile(self, subtitle,subtitles_folder) return subtitle["link"] diff --git a/periscope/plugins/SubDivX.py b/periscope/plugins/SubDivX.py index 2fe5b89..4a6c2cf 100644 --- a/periscope/plugins/SubDivX.py +++ b/periscope/plugins/SubDivX.py @@ -121,7 +121,7 @@ def query(self, name, season=None, episode=None, extra=None): sorted_links = sorted(sublinks, key=lambda k: k['rating'], reverse=True) return sorted_links - def createFile(self, subtitle): + def createFile(self, subtitle,subtitles_folder): '''Download and extract subtitle. Pass the URL of the sub and the file it matches, will unzip it @@ -146,6 +146,8 @@ def createFile(self, subtitle): video_filename = os.path.basename(subtitle["filename"]) base_filename, _ = os.path.splitext(video_filename) base_rar_filename, _ = os.path.splitext(subtitle["filename"]) + if subtitles_folder: + base_rar_filename = subtitles_folder + base_rar_filename rar_filename = '%s%s' % (base_rar_filename, '.rar') self.downloadFile(download_url, rar_filename) diff --git a/periscope/plugins/SubScene.py b/periscope/plugins/SubScene.py index 14aa914..91d9ffe 100644 --- a/periscope/plugins/SubScene.py +++ b/periscope/plugins/SubScene.py @@ -84,7 +84,7 @@ def process(self, filepath, langs): traceback.print_exc() return [] - def createFile(self, subtitle): + def createFile(self, subtitle,subtitles_folder): '''pass the URL of the sub and the file it matches, will unzip it and return the path to the created file''' subpage = subtitle["page"] @@ -94,7 +94,9 @@ def createFile(self, subtitle): dlhref = soup.find("div", {"class" : "download"}).find("a")["href"] subtitle["link"] = "http://subscene.com" + dlhref.split('"')[7] format = "zip" - archivefilename = subtitle["filename"].rsplit(".", 1)[0] + '.'+ format + archivefilename = subtitle["filename"].rsplit(".", 1)[0] + '.'+ format + if subtitles_folder: + archivefilename = subtitles_folder + os.path.basename(archivefilename) self.downloadFile(subtitle["link"], archivefilename) subtitlefilename = None diff --git a/periscope/plugins/SubsWiki.py b/periscope/plugins/SubsWiki.py index 15aab68..cde0f5e 100644 --- a/periscope/plugins/SubsWiki.py +++ b/periscope/plugins/SubsWiki.py @@ -118,12 +118,14 @@ def splitTeam(self, subteams, sep): teams += t.split(sep) return teams - def createFile(self, subtitle): + def createFile(self, subtitle,subtitles_folder): '''pass the URL of the sub and the file it matches, will unzip it and return the path to the created file''' suburl = subtitle["link"] videofilename = subtitle["filename"] srtbasefilename = videofilename.rsplit(".", 1)[0] + if subtitles_folder: + srtbasefilename = subtitles_folder + os.path.basename(subtitle["filename"]) srtfilename = srtbasefilename +".srt" self.downloadFile(suburl, srtfilename) return srtfilename diff --git a/periscope/plugins/SubtitleDatabase.py b/periscope/plugins/SubtitleDatabase.py index 63abef6..e1c8e59 100644 --- a/periscope/plugins/SubtitleDatabase.py +++ b/periscope/plugins/SubtitleDatabase.py @@ -61,12 +61,14 @@ def process(self, filepath, langs): log.exception("Error occured") return [] - def createFile(self, subtitle): + def createFile(self, subtitle,subtitles_folder): '''pass the URL of the sub and the file it matches, will unzip it and return the path to the created file''' suburl = subtitle["link"] videofilename = subtitle["filename"] srtbasefilename = videofilename.rsplit(".", 1)[0] + if subtitles_folder: + srtbasefilename = subtitles_folder + os.path.basename(subtitle["filename"]) zipfilename = srtbasefilename +".zip" self.downloadFile(suburl, zipfilename) diff --git a/periscope/plugins/SubtitleSource.py b/periscope/plugins/SubtitleSource.py index 127e1bb..9d0ab50 100644 --- a/periscope/plugins/SubtitleSource.py +++ b/periscope/plugins/SubtitleSource.py @@ -122,12 +122,14 @@ def query(self, token, langs=None): return sublinks - def createFile(self, subtitle): + def createFile(self, subtitle,subtitles_folder): '''pass the URL of the sub and the file it matches, will unzip it and return the path to the created file''' suburl = subtitle["link"] videofilename = subtitle["filename"] srtfilename = videofilename.rsplit(".", 1)[0] + '.srt' + if subtitles_folder: + srtfilename = subtitles_folder + os.path.basename(subtitle["filename"]) self.downloadFile(suburl, srtfilename) return srtfilename diff --git a/periscope/plugins/Subtitulos.py b/periscope/plugins/Subtitulos.py index 20846c4..ea543fa 100644 --- a/periscope/plugins/Subtitulos.py +++ b/periscope/plugins/Subtitulos.py @@ -109,12 +109,14 @@ def splitTeam(self, subteams, sep): teams += t.split(sep) return teams - def createFile(self, subtitle): + def createFile(self, subtitle,subtitles_folder): '''pass the URL of the sub and the file it matches, will unzip it and return the path to the created file''' suburl = subtitle["link"] videofilename = subtitle["filename"] srtbasefilename = videofilename.rsplit(".", 1)[0] + if subtitles_folder: + srtbasefilename = subtitles_folder + os.path.basename(subtitle["filename"]) srtfilename = srtbasefilename +".srt" self.downloadFile(suburl, srtfilename) return srtfilename diff --git a/periscope/plugins/TheSubDB.py b/periscope/plugins/TheSubDB.py index 1caa847..1bc60db 100644 --- a/periscope/plugins/TheSubDB.py +++ b/periscope/plugins/TheSubDB.py @@ -92,12 +92,14 @@ def get_hash(self, name): data += f.read(readsize) return hashlib.md5(data).hexdigest() - def createFile(self, subtitle): + def createFile(self, subtitle,subtitles_folder): '''pass the URL of the sub and the file it matches, will unzip it and return the path to the created file''' suburl = subtitle["link"] videofilename = subtitle["filename"] srtfilename = videofilename.rsplit(".", 1)[0] + '.srt' + if subtitles_folder: + srtfilename = subtitles_folder + os.path.basename(subtitle["filename"]) self.downloadFile(suburl, srtfilename) return srtfilename diff --git a/periscope/plugins/__init__.py b/periscope/plugins/__init__.py index 712f027..07c6aa7 100644 --- a/periscope/plugins/__init__.py +++ b/periscope/plugins/__init__.py @@ -31,7 +31,7 @@ #from SubScene import SubScene # Don't want to be included -#from Addic7ed import Addic7ed +from Addic7ed import Addic7ed #from TvSubtitles import TvSubtitles # API not working