From ba565a3eaa9cfe965b94ce5e360e18ddffe88930 Mon Sep 17 00:00:00 2001 From: Ryan Hitchman Date: Sun, 29 Aug 2010 21:12:03 -0500 Subject: [PATCH] modify tvdb to be more in line with skybot coding style --- plugins/tvdb.py | 140 ++++++++++++------------------------------------ 1 file changed, 34 insertions(+), 106 deletions(-) diff --git a/plugins/tvdb.py b/plugins/tvdb.py index 17cc6b7..bd24060 100644 --- a/plugins/tvdb.py +++ b/plugins/tvdb.py @@ -1,137 +1,65 @@ """ TV information, written by Lurchington 2010 +modified by rmmh 2010 """ -# python imports import datetime -from contextlib import closing -from itertools import ifilter -from util import hook -from urllib import urlencode -from urllib2 import urlopen +from util import hook, http -# third-party-imports -from lxml import etree as ET -base_url = "http://thetvdb.com" +base_url = "http://thetvdb.com/api/" api_key = "D1EBA6781E2572BB" -# @hook.command -# def tv(inp): -# return search(inp) @hook.command def tv_next(inp): - """ - 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): + ".tv_next -- get the next episode of from thetvdb.com" - id = search(seriesname) - if id is None: - return "unknown tv series (using www.thetvdb.org)" - - with closing(_get_full_series_record(id)) as fsr: - tree = ET.parse(fsr) - root = tree.getroot() + # http://thetvdb.com/wiki/index.php/API:GetSeries + query = http.get_xml(base_url + 'GetSeries.php', seriesname=inp) + series_id = query.xpath('//seriesid/text()') - series = root.find("Series") + if not series_id: + return "unknown tv series (using www.thetvdb.com)" - status = series.findtext("Status") - 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 = [] + series_id = series_id[0] - for episode in root.iterchildren("Episode", reversed=True): - first_aired = episode.findtext("FirstAired") + series = http.get_xml(base_url + '%s/series/%s/all/en.xml' % + (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: - y, m, d = map(int, first_aired.split("-")) - airdate = datetime.date(y, m, d) - except Exception: + airdate = datetime.date(*map(int, first_aired.split('-'))) + except (ValueError, TypeError): continue - today = datetime.date.today() - test = datetime.date(2010, 9, 20) - - episode_name = episode.findtext("EpisodeName") - if not episode_name: - episode_name = "No Title Yet" + episode_name = episode.findtext("EpisodeName") or "No Title Yet" + episode_num = "S%02dE%02d" % (int(episode.findtext("SeasonNumber")), + int(episode.findtext("EpisodeNumber"))) + episode_desc = '%s "%s"' % (episode_num, episode_name) if airdate > today: - next_eps = ['%s ("%s")' % (episode.findtext("FirstAired"), - episode_name)] + next_eps = ['%s (%s)' % (first_aired, episode_desc)] elif airdate == today: - #if today is the day of the newest episode, return it - #along with the next episode after - next_eps = (['Today ("%s")' % episode_name] + - next_eps) - + next_eps = ['Today (%s)' % episode_desc] + next_eps else: #we're iterating in reverse order with newest episodes last #so, as soon as we're past today, break out of loop break 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) - - -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) + if len(next_eps) == 1: + return "the next episode of %s airs %s" % (series_name, next_eps[0]) else: - data = urlencode(dict(language=language, - 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) \ No newline at end of file + return "the next episodes of %s: %s" % (series_name, ", ".join(next_eps))