modify tvdb to be more in line with skybot coding style
This commit is contained in:
parent
93d507342d
commit
ba565a3eaa
140
plugins/tvdb.py
140
plugins/tvdb.py
|
@ -1,137 +1,65 @@
|
||||||
"""
|
"""
|
||||||
TV information, written by Lurchington 2010
|
TV information, written by Lurchington 2010
|
||||||
|
modified by rmmh 2010
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# python imports
|
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
from contextlib import closing
|
from util import hook, http
|
||||||
from itertools import ifilter
|
|
||||||
from util import hook
|
|
||||||
from urllib import urlencode
|
|
||||||
from urllib2 import urlopen
|
|
||||||
|
|
||||||
# third-party-imports
|
|
||||||
from lxml import etree as ET
|
|
||||||
|
|
||||||
base_url = "http://thetvdb.com"
|
base_url = "http://thetvdb.com/api/"
|
||||||
api_key = "D1EBA6781E2572BB"
|
api_key = "D1EBA6781E2572BB"
|
||||||
|
|
||||||
# @hook.command
|
|
||||||
# def tv(inp):
|
|
||||||
# return search(inp)
|
|
||||||
|
|
||||||
@hook.command
|
@hook.command
|
||||||
def tv_next(inp):
|
def tv_next(inp):
|
||||||
"""
|
".tv_next <series> -- get the next episode of <series> from thetvdb.com"
|
||||||
return the next episode of the provided seriesname
|
|
||||||
"""
|
|
||||||
return next_episode(inp)
|
|
||||||
|
|
||||||
def search(seriesname):
|
|
||||||
"""
|
|
||||||
search tvdb for series IDs corresponding to a provided seriesname and
|
|
||||||
return the first series ID found
|
|
||||||
"""
|
|
||||||
id = None
|
|
||||||
with closing(urlopen(_query_GetSeries(seriesname))) as show_query:
|
|
||||||
for event, element in ET.iterparse(show_query):
|
|
||||||
if element.tag == u"Series":
|
|
||||||
id = element.findtext("id")
|
|
||||||
break
|
|
||||||
return id
|
|
||||||
|
|
||||||
return None
|
|
||||||
|
|
||||||
def next_episode(seriesname):
|
|
||||||
|
|
||||||
id = search(seriesname)
|
# http://thetvdb.com/wiki/index.php/API:GetSeries
|
||||||
if id is None:
|
query = http.get_xml(base_url + 'GetSeries.php', seriesname=inp)
|
||||||
return "unknown tv series (using www.thetvdb.org)"
|
series_id = query.xpath('//seriesid/text()')
|
||||||
|
|
||||||
with closing(_get_full_series_record(id)) as fsr:
|
|
||||||
tree = ET.parse(fsr)
|
|
||||||
root = tree.getroot()
|
|
||||||
|
|
||||||
series = root.find("Series")
|
if not series_id:
|
||||||
|
return "unknown tv series (using www.thetvdb.com)"
|
||||||
|
|
||||||
status = series.findtext("Status")
|
series_id = series_id[0]
|
||||||
if status == u"Ended":
|
|
||||||
#short circuit evaluation to avoid
|
|
||||||
#having to find episodes for a finished show
|
|
||||||
return u"Sorry, %s is over" % series.findtext("SeriesName")
|
|
||||||
|
|
||||||
next_eps = []
|
|
||||||
|
|
||||||
for episode in root.iterchildren("Episode", reversed=True):
|
series = http.get_xml(base_url + '%s/series/%s/all/en.xml' %
|
||||||
first_aired = episode.findtext("FirstAired")
|
(api_key, series_id))
|
||||||
|
series_name = series.xpath('//SeriesName/text()')[0]
|
||||||
|
|
||||||
|
if series.xpath('//Status/text()')[0] == 'Ended':
|
||||||
|
return '%s has ended.' % series_name
|
||||||
|
|
||||||
|
next_eps = []
|
||||||
|
today = datetime.date.today()
|
||||||
|
|
||||||
|
for episode in reversed(series.xpath('//Episode')):
|
||||||
|
first_aired = episode.findtext("FirstAired")
|
||||||
try:
|
try:
|
||||||
y, m, d = map(int, first_aired.split("-"))
|
airdate = datetime.date(*map(int, first_aired.split('-')))
|
||||||
airdate = datetime.date(y, m, d)
|
except (ValueError, TypeError):
|
||||||
except Exception:
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
today = datetime.date.today()
|
episode_name = episode.findtext("EpisodeName") or "No Title Yet"
|
||||||
test = datetime.date(2010, 9, 20)
|
episode_num = "S%02dE%02d" % (int(episode.findtext("SeasonNumber")),
|
||||||
|
int(episode.findtext("EpisodeNumber")))
|
||||||
episode_name = episode.findtext("EpisodeName")
|
episode_desc = '%s "%s"' % (episode_num, episode_name)
|
||||||
if not episode_name:
|
|
||||||
episode_name = "No Title Yet"
|
|
||||||
|
|
||||||
if airdate > today:
|
if airdate > today:
|
||||||
next_eps = ['%s ("%s")' % (episode.findtext("FirstAired"),
|
next_eps = ['%s (%s)' % (first_aired, episode_desc)]
|
||||||
episode_name)]
|
|
||||||
elif airdate == today:
|
elif airdate == today:
|
||||||
#if today is the day of the newest episode, return it
|
next_eps = ['Today (%s)' % episode_desc] + next_eps
|
||||||
#along with the next episode after
|
|
||||||
next_eps = (['Today ("%s")' % episode_name] +
|
|
||||||
next_eps)
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
#we're iterating in reverse order with newest episodes last
|
#we're iterating in reverse order with newest episodes last
|
||||||
#so, as soon as we're past today, break out of loop
|
#so, as soon as we're past today, break out of loop
|
||||||
break
|
break
|
||||||
|
|
||||||
if not next_eps:
|
if not next_eps:
|
||||||
return "No new episodes scheduled for %s" % series.findtext("SeriesName")
|
return "there are no new episodes scheduled for %s" % series_name
|
||||||
|
|
||||||
return " -> ".join(next_eps)
|
if len(next_eps) == 1:
|
||||||
|
return "the next episode of %s airs %s" % (series_name, next_eps[0])
|
||||||
|
|
||||||
def _query_GetSeries(seriesname, language=None):
|
|
||||||
"""
|
|
||||||
http://thetvdb.com/wiki/index.php/API:GetSeries
|
|
||||||
"""
|
|
||||||
|
|
||||||
if language is None:
|
|
||||||
data = urlencode(dict(seriesname=seriesname), doseq=True)
|
|
||||||
else:
|
else:
|
||||||
data = urlencode(dict(language=language,
|
return "the next episodes of %s: %s" % (series_name, ", ".join(next_eps))
|
||||||
seriesname=seriesname),
|
|
||||||
doseq=True)
|
|
||||||
|
|
||||||
url = _make_url(base_url, "api",
|
|
||||||
"GetSeries.php?%s"%data)
|
|
||||||
|
|
||||||
return url
|
|
||||||
|
|
||||||
def _get_full_series_record(seriesid):
|
|
||||||
|
|
||||||
url = _make_url(base_url, "api", api_key, "series",
|
|
||||||
seriesid, "all", "en.xml")
|
|
||||||
return urlopen(url)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def _make_url(url_netloc, *url_path):
|
|
||||||
"""
|
|
||||||
Appends all parts of url_path to the given url_netloc using "/" as\
|
|
||||||
a delimeter
|
|
||||||
"""
|
|
||||||
|
|
||||||
url_sequence = [url_netloc]
|
|
||||||
url_sequence.extend(url_path)
|
|
||||||
|
|
||||||
return "/".join(url_sequence)
|
|
||||||
|
|
Loading…
Reference in New Issue