-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.py
154 lines (120 loc) · 5.43 KB
/
plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import re
import os
import supybot.conf as conf
import supybot.utils as utils
from supybot.commands import *
import supybot.ircutils as ircutils
import supybot.callbacks as callbacks
import json
import datetime
from dateutil.tz import tzlocal
from babel.dates import format_timedelta
from dateutil.parser import parse
def fetch(show=False):
if show:
query_string = '?q=' + utils.web.urlquote(show) + '&embed[]=previousepisode&embed[]=nextepisode'
url = 'http://api.tvmaze.com/singlesearch/shows' + query_string
else:
url = 'http://api.tvmaze.com/schedule?country=US'
try:
resp = utils.web.getUrl(url)
except utils.web.Error as e:
return False
data = json.loads(resp.decode('utf-8'))
return data
class tvmaze(callbacks.Plugin):
threaded=True
def tv(self, irc, msg, args, opts, tvshow):
"""[-d | --detail] <tvshow>
"""
if not opts:
details = False
else:
for (stuff, arg) in opts:
if stuff == 'd':
details = True
elif stuff == 'detail':
details = True
show = fetch(tvshow)
if show:
if show['premiered']:
premiered = show['premiered']
else:
premiered = "SOON"
show_state = format('%s %s (%s).',
ircutils.bold(ircutils.underline(show['name'])),
premiered[:4], show['status'])
if ( '_embedded' in show and 'previousepisode' in show['_embedded']):
airtime = parse(show['_embedded']['previousepisode']['airstamp'])
timedelta = datetime.datetime.now(tzlocal()) - airtime
relative_time = format_timedelta(timedelta,
granularity='minutes')
last_episode = format('%s: [%s] %s on %s (%s).',
ircutils.underline('Previous Episode'),
ircutils.bold(str(show['_embedded']['previousepisode']['season'])
+ 'x' +
str(show['_embedded']['previousepisode']['number'])),
ircutils.bold(show['_embedded']['previousepisode']['name']),
ircutils.bold(show['_embedded']['previousepisode']['airdate']),
ircutils.mircColor(relative_time, 'red'))
else:
last_episode = ''
if ('_embedded' in show and 'nextepisode' in show['_embedded']):
airtime = parse(show['_embedded']['nextepisode']['airstamp'])
timedelta = datetime.datetime.now(tzlocal()) - airtime
relative_time = format_timedelta(timedelta, granularity='minutes')
next_episode = format('%s: [%s] %s on %s (%s).',
ircutils.underline('Next Episode'),
ircutils.bold(str(show['_embedded']['nextepisode']['season'])
+ 'x' +
str(show['_embedded']['nextepisode']['number'])),
ircutils.bold(show['_embedded']['nextepisode']['name']),
ircutils.bold(show['_embedded']['nextepisode']['airdate']),
ircutils.mircColor(relative_time, 'green'))
else:
next_episode = format('%s: %s.',
ircutils.underline('Next Episode'),
ircutils.bold('not yet scheduled'))
irc.reply(format('%s %s %s %s', show_state, last_episode, next_episode, show['url']))
else:
irc.reply(format('No show found named "%s"', ircutils.bold(tvshow)))
if details:
if show['network']:
show_network = format('%s',
ircutils.bold(show['network']['name']))
show_schedule = format('%s: %s @ %s',
ircutils.underline('Schedule'),
ircutils.bold(', '.join(show['schedule']['days'])),
ircutils.bold(show['schedule']['time']))
else:
show_network = format('%s',
ircutils.bold(show['webChannel']['name']))
show_schedule = format('%s: %s',
ircutils.underline('Premiered'),
ircutils.bold(show['premiered']))
show_genre = format('%s: %s/%s',
ircutils.underline('Genre'),
ircutils.bold(show['type']),
'/'.join(show['genres']))
irc.reply(format('%s on %s. %s', show_schedule, show_network,
show_genre))
tv = wrap(tv, [getopts({'d': '', 'detail': ''}), 'text'])
def schedule(self, irc, msg, args):
"""
"""
shows = fetch(False)
l = []
if shows:
for show in shows:
if show['show']['type'] == 'Scripted':
this_show = format('%s [%s] (%s)',
ircutils.bold(show['show']['name']),
str(show['season']) + 'x' + str(show['number']),
show['airtime'])
l.append(this_show)
tonight_shows = ', '.join(l)
irc.reply(format('%s: %s',
ircutils.underline("Tonight's Shows"),
tonight_shows))
Class = tvmaze
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: