Skip to content

Commit

Permalink
More robust description parsing, better logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramiro Morales committed Oct 28, 2014
1 parent bdc06cd commit 27e200e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 39 deletions.
2 changes: 1 addition & 1 deletion addon.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="service.subtitles.subdivx"
name="Subdivx.com"
version="0.0.3"
version="0.0.4"
provider-name="cramm">
<requires>
<import addon="xbmc.python" version="2.14.0"/>
Expand Down
5 changes: 5 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
0.0.4
- More robust parsing of description, better logging.
- Also match variation in CSS class name just in case.
- Fix stripping of trailing '/' from param value.

0.0.3
- Handle arbitrary casing in subs file extension, e.g. .SRT, .sRt, etc.
- More code cleanup (PEP8, comment/docstring typos, logic of a helper function).
Expand Down
72 changes: 34 additions & 38 deletions service.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
from __future__ import print_function
import os
from os.path import join as pjoin
from pprint import pformat
import re
import shutil
import sys
import time
import unicodedata
import urllib
from urlparse import parse_qs

try:
import xbmc
Expand Down Expand Up @@ -90,39 +92,35 @@ def is_subs_file(fn):
# ==========


def _log(module, msg):
s = u"### [%s] - %s" % (module, msg)
xbmc.log(s.encode('utf-8'), level=xbmc.LOGDEBUG)


def log(msg):
_log(__name__, msg)
s = u"SUBDIVX - %s" % msg
xbmc.log(s.encode('utf-8'), level=xbmc.LOGDEBUG)


def geturl(url):
def get_url(url):
class MyOpener(urllib.FancyURLopener):
# version = HTTP_USER_AGENT
version = ''
my_urlopener = MyOpener()
log(u"Getting url: %s" % (url,))
log(u"get_url(): Getting url %s" % url)
try:
response = my_urlopener.open(url)
content = response.read()
except Exception:
log(u"Failed to get url:%s" % (url,))
log(u"get_url(): Failed to get url: %s" % url)
content = None
return content


def getallsubs(searchstring, languageshort, languagelong, file_original_path):
def get_all_subs(searchstring, languageshort, languagelong, file_original_path):
subs_list = []
if languageshort == "es":
log(u"Getting '%s' subs ..." % languageshort)
page = 1
while True:
url = SEARCH_PAGE_URL % {'page': page,
'query': urllib.quote_plus(searchstring)}
content = geturl(url)
content = get_url(url)
if content is None or not SUBTITLE_RE.search(content):
break
for match in SUBTITLE_RE.finditer(content):
Expand Down Expand Up @@ -166,6 +164,7 @@ def getallsubs(searchstring, languageshort, languagelong, file_original_path):
# Put subs with sync=True at the top
subs_list = sorted(subs_list, key=lambda s: s['sync'], reverse=True)

log(u"get_all_subs(): Returning %s" % pformat(subs_list))
return subs_list


Expand Down Expand Up @@ -198,6 +197,7 @@ def append_subtitle(item):

def Search(item):
"""Called when subtitle download is requested from XBMC."""
log(u'Search(): item = %s' % pformat(item))
# Do what's needed to get the list of subtitles from service site
# use item["some_property"] that was set earlier.
# Once done, set xbmcgui.ListItem() below and pass it to
Expand All @@ -212,17 +212,17 @@ def Search(item):
searchstring = "%s S%#02dE%#02d" % (tvshow, int(season), int(episode))
else:
searchstring = title
log(u"Search string = %s" % (searchstring,))
log(u"Search string = %s" % searchstring)

subs_list = getallsubs(searchstring, "es", "Spanish", file_original_path)
subs_list = get_all_subs(searchstring, "es", "Spanish", file_original_path)

for sub in subs_list:
append_subtitle(sub)


def Download(id, filename):
"""Called when subtitle download request from XBMC."""
# Cleanup temp dir, we recomend you download/unzip your subs in temp folder
# Cleanup temp dir, we recommend you download/unzip your subs in temp folder
# and pass that to XBMC to copy and activate
if xbmcvfs.exists(__temp__):
shutil.rmtree(__temp__)
Expand All @@ -232,11 +232,11 @@ def Download(id, filename):
# Get the page with the subtitle link,
# i.e. http://www.subdivx.com/X6XMjE2NDM1X-iron-man-2-2010
subtitle_detail_url = MAIN_SUBDIVX_URL + str(id)
content = geturl(subtitle_detail_url)
content = get_url(subtitle_detail_url)
match = DOWNLOAD_LINK_RE.findall(content)

actual_subtitle_file_url = MAIN_SUBDIVX_URL + "bajar.php?id=" + match[0][0] + "&u=" + match[0][1]
content = geturl(actual_subtitle_file_url)
content = get_url(actual_subtitle_file_url)
if content is not None:
header = content[:4]
if header == 'Rar!':
Expand All @@ -262,7 +262,7 @@ def Download(id, filename):
if packed:
files = os.listdir(__temp__)
init_filecount = len(files)
log(u"subdivx: init_filecount = %d" % init_filecount)
log(u"init_filecount = %d" % init_filecount)
filecount = init_filecount
max_mtime = 0
# Determine the newest file from __temp__
Expand Down Expand Up @@ -294,15 +294,15 @@ def Download(id, filename):
max_mtime = mtime
waittime = waittime + 1
if waittime == 20:
log(u"Failed to unpack subtitles in '%s'" % (__temp__,))
log(u"Failed to unpack subtitles in '%s'" % __temp__)
else:
log(u"Unpacked files in '%s'" % (__temp__,))
log(u"Unpacked files in '%s'" % __temp__)
for file in files:
# There could be more subtitle files in __temp__, so make
# sure we get the newly created subtitle file
if is_subs_file(file) and os.stat(pjoin(__temp__, file)).st_mtime > init_max_mtime:
# unpacked file is a newly created subtitle file
log(u"Unpacked subtitles file '%s'" % (file,))
log(u"Unpacked subtitles file '%s'" % file)
subs_file = pjoin(__temp__, file.decode("utf-8"))
subtitles_list.append(subs_file)
break
Expand All @@ -311,31 +311,27 @@ def Download(id, filename):
return subtitles_list


def normalizeString(str):
def normalize_string(str):
return unicodedata.normalize('NFKD', unicode(unicode(str, 'utf-8'))).encode('ascii', 'ignore')


def get_params():
def get_params(argv):
params = {}
arg = sys.argv[2]
if len(arg) >= 2:
value = arg
if value.endswith('/'):
value = value[:-1]
cleaned = value.replace('?', '')
for elem in cleaned.split('&'):
kv = elem.split('=')
if len(kv) == 2:
params[kv[0]] = kv[1]

qs = argv[2].lstrip('?')
if qs:
if qs.endswith('/'):
qs = qs[:-1]
parsed = parse_qs(qs)
for k, v in parsed.iteritems():
params[k] = v[0]
return params


def main():
"""Main entry point of the script when it is invoked by XBMC."""

# Get parameters from XBMC and launch actions
params = get_params()
params = get_params(sys.argv)

if params['action'] == 'search':
item = {}
Expand All @@ -344,9 +340,9 @@ def main():
item['year'] = xbmc.getInfoLabel("VideoPlayer.Year")
item['season'] = str(xbmc.getInfoLabel("VideoPlayer.Season"))
item['episode'] = str(xbmc.getInfoLabel("VideoPlayer.Episode"))
item['tvshow'] = normalizeString(xbmc.getInfoLabel("VideoPlayer.TVshowtitle"))
item['tvshow'] = normalize_string(xbmc.getInfoLabel("VideoPlayer.TVshowtitle"))
# Try to get original title
item['title'] = normalizeString(xbmc.getInfoLabel("VideoPlayer.OriginalTitle"))
item['title'] = normalize_string(xbmc.getInfoLabel("VideoPlayer.OriginalTitle"))
# Full path of a playing file
item['file_original_path'] = urllib.unquote(xbmc.Player().getPlayingFile().decode('utf-8'))
item['3let_language'] = []
Expand All @@ -358,7 +354,7 @@ def main():

if not item['title']:
# No original title, get just Title
item['title'] = normalizeString(xbmc.getInfoLabel("VideoPlayer.Title"))
item['title'] = normalize_string(xbmc.getInfoLabel("VideoPlayer.Title"))

if "s" in item['episode'].lower():
# Check if season is "Special"
Expand All @@ -379,7 +375,7 @@ def main():
Search(item)

elif params['action'] == 'download':
# We pickup all our arguments sent from def Search()
# We pickup all our arguments sent from the Search() function
subs = Download(params["id"], params["filename"])
# We can return more than one subtitle for multi CD versions, for now
# we are still working out how to handle that in XBMC core
Expand Down

0 comments on commit 27e200e

Please sign in to comment.