Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changes to Addic7ed plugin #39

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions bin/periscope
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down Expand Up @@ -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: "
Expand Down Expand Up @@ -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)

Expand All @@ -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:
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions periscope/periscope.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 14 additions & 4 deletions periscope/plugins/Addic7ed.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,19 @@ 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):
''' main method to call on the plugin, pass the filename and the wished
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 []
Expand All @@ -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()

Expand All @@ -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)
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion periscope/plugins/LegendasTV.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion periscope/plugins/OpenSubtitles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions periscope/plugins/Podnapisi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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"]
4 changes: 3 additions & 1 deletion periscope/plugins/SubDivX.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down
6 changes: 4 additions & 2 deletions periscope/plugins/SubScene.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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

Expand Down
4 changes: 3 additions & 1 deletion periscope/plugins/SubsWiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion periscope/plugins/SubtitleDatabase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 3 additions & 1 deletion periscope/plugins/SubtitleSource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion periscope/plugins/Subtitulos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion periscope/plugins/TheSubDB.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion periscope/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down