2010-08-29 19:20:13 +00:00
|
|
|
"""
|
|
|
|
TV information, written by Lurchington 2010
|
2010-08-30 02:12:03 +00:00
|
|
|
modified by rmmh 2010
|
2010-08-29 19:20:13 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
import datetime
|
2010-09-03 02:30:25 +00:00
|
|
|
from urllib2 import URLError
|
2010-09-09 02:22:25 +00:00
|
|
|
from zipfile import ZipFile
|
2010-09-11 02:23:17 +00:00
|
|
|
from cStringIO import StringIO
|
2010-08-29 19:20:13 +00:00
|
|
|
|
2010-09-09 02:22:25 +00:00
|
|
|
from lxml import etree
|
2010-08-30 02:12:03 +00:00
|
|
|
from util import hook, http
|
2010-08-29 19:20:13 +00:00
|
|
|
|
|
|
|
|
2010-08-30 02:12:03 +00:00
|
|
|
base_url = "http://thetvdb.com/api/"
|
2010-09-03 02:30:25 +00:00
|
|
|
api_key = "469B73127CA0C411"
|
2010-08-29 19:20:13 +00:00
|
|
|
|
2010-09-11 03:01:20 +00:00
|
|
|
|
2010-09-09 02:22:25 +00:00
|
|
|
def get_zipped_xml(*args, **kwargs):
|
|
|
|
try:
|
|
|
|
path = kwargs.pop("path")
|
|
|
|
except KeyError:
|
|
|
|
raise KeyError("must specify a path for the zipped file to be read")
|
2010-09-11 03:01:20 +00:00
|
|
|
|
2010-09-11 02:23:17 +00:00
|
|
|
zip_buffer = StringIO(http.get(*args, **kwargs))
|
|
|
|
return etree.parse(ZipFile(zip_buffer, "r").open(path))
|
2010-08-29 19:20:13 +00:00
|
|
|
|
2010-09-11 03:01:20 +00:00
|
|
|
|
2010-08-29 19:20:13 +00:00
|
|
|
@hook.command
|
|
|
|
def tv_next(inp):
|
2010-08-30 02:12:03 +00:00
|
|
|
".tv_next <series> -- get the next episode of <series> from thetvdb.com"
|
2010-08-30 03:35:27 +00:00
|
|
|
|
2010-08-30 02:12:03 +00:00
|
|
|
# http://thetvdb.com/wiki/index.php/API:GetSeries
|
2010-09-03 02:30:25 +00:00
|
|
|
try:
|
|
|
|
query = http.get_xml(base_url + 'GetSeries.php', seriesname=inp)
|
|
|
|
except URLError:
|
|
|
|
return "error contacting thetvdb.com"
|
2010-09-11 03:01:20 +00:00
|
|
|
|
2010-08-30 02:12:03 +00:00
|
|
|
series_id = query.xpath('//seriesid/text()')
|
2010-08-29 19:20:13 +00:00
|
|
|
|
2010-08-30 02:12:03 +00:00
|
|
|
if not series_id:
|
|
|
|
return "unknown tv series (using www.thetvdb.com)"
|
2010-08-29 19:20:13 +00:00
|
|
|
|
2010-08-30 02:12:03 +00:00
|
|
|
series_id = series_id[0]
|
2010-08-29 19:20:13 +00:00
|
|
|
|
2010-09-03 02:30:25 +00:00
|
|
|
try:
|
2010-09-09 02:22:25 +00:00
|
|
|
series = get_zipped_xml(base_url + '%s/series/%s/all/en.zip' %
|
|
|
|
(api_key, series_id), path="en.xml")
|
2010-09-11 03:01:20 +00:00
|
|
|
except URLError:
|
2010-09-03 02:30:25 +00:00
|
|
|
return "error contacting thetvdb.com"
|
2010-09-11 03:01:20 +00:00
|
|
|
|
2010-08-30 02:12:03 +00:00
|
|
|
series_name = series.xpath('//SeriesName/text()')[0]
|
2010-08-29 19:20:13 +00:00
|
|
|
|
2010-08-30 02:12:03 +00:00
|
|
|
if series.xpath('//Status/text()')[0] == 'Ended':
|
|
|
|
return '%s has ended.' % series_name
|
2010-08-29 19:20:13 +00:00
|
|
|
|
2010-08-30 02:12:03 +00:00
|
|
|
next_eps = []
|
|
|
|
today = datetime.date.today()
|
2010-08-30 03:35:27 +00:00
|
|
|
|
2010-08-30 02:12:03 +00:00
|
|
|
for episode in reversed(series.xpath('//Episode')):
|
2010-08-30 03:35:27 +00:00
|
|
|
first_aired = episode.findtext("FirstAired")
|
2010-09-11 03:01:20 +00:00
|
|
|
|
2010-08-29 19:20:13 +00:00
|
|
|
try:
|
2010-08-30 02:12:03 +00:00
|
|
|
airdate = datetime.date(*map(int, first_aired.split('-')))
|
|
|
|
except (ValueError, TypeError):
|
2010-08-29 19:20:13 +00:00
|
|
|
continue
|
2010-09-11 03:01:20 +00:00
|
|
|
|
2010-09-11 02:23:17 +00:00
|
|
|
episode_num = "S%02dE%02d" % (int(episode.findtext("SeasonNumber")),
|
|
|
|
int(episode.findtext("EpisodeNumber")))
|
2010-08-30 03:35:27 +00:00
|
|
|
|
2010-09-09 02:42:24 +00:00
|
|
|
episode_name = episode.findtext("EpisodeName")
|
2010-09-11 02:23:17 +00:00
|
|
|
# in the event of an unannounced episode title, users either leave the
|
|
|
|
# field out (None) or fill it with TBA
|
2010-09-09 02:42:24 +00:00
|
|
|
if episode_name == "TBA":
|
|
|
|
episode_name = None
|
2010-09-11 02:23:17 +00:00
|
|
|
|
|
|
|
episode_desc = '%s' % episode_num
|
|
|
|
if episode_name:
|
|
|
|
episode_desc += ' - %s' % episode_name
|
2010-08-30 03:35:27 +00:00
|
|
|
|
2010-08-29 19:20:13 +00:00
|
|
|
if airdate > today:
|
2010-08-30 02:12:03 +00:00
|
|
|
next_eps = ['%s (%s)' % (first_aired, episode_desc)]
|
2010-08-29 19:20:13 +00:00
|
|
|
elif airdate == today:
|
2010-08-30 02:12:03 +00:00
|
|
|
next_eps = ['Today (%s)' % episode_desc] + next_eps
|
2010-08-29 19:20:13 +00:00
|
|
|
else:
|
|
|
|
#we're iterating in reverse order with newest episodes last
|
|
|
|
#so, as soon as we're past today, break out of loop
|
|
|
|
break
|
2010-08-30 03:35:27 +00:00
|
|
|
|
2010-08-29 19:20:13 +00:00
|
|
|
if not next_eps:
|
2010-08-30 02:12:03 +00:00
|
|
|
return "there are no new episodes scheduled for %s" % series_name
|
2010-08-30 03:35:27 +00:00
|
|
|
|
2010-08-30 02:12:03 +00:00
|
|
|
if len(next_eps) == 1:
|
|
|
|
return "the next episode of %s airs %s" % (series_name, next_eps[0])
|
2010-08-29 19:20:13 +00:00
|
|
|
else:
|
2010-08-30 03:35:27 +00:00
|
|
|
next_eps = ', '.join(next_eps)
|
|
|
|
return "the next episodes of %s: %s" % (series_name, next_eps)
|