-
Notifications
You must be signed in to change notification settings - Fork 1
/
cbeebies.py
executable file
·77 lines (61 loc) · 2.01 KB
/
cbeebies.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
#!/usr/bin/env python
#
# cbeebies.py:
# Twitter/Toot when some CBeebies shows are on TV
#
# Copyright (c) 2018 Matthew Somerville.
# http://www.dracos.co.uk/
import json
import sys
import arrow
from bot import SchedulerBot, Event
from config import SHOWS
class Event(Event):
def __str__(self):
return '%s %s %s %s' % (self.bot.name, self.time, self.image_url, self.status)
@property
def image(self):
return self.bot.get_image(self.image_url)
@property
def status(self):
return '%s (s%se%s) starting shortly!\n\u201c%s\u201d' % (
self.programme['title'],
self.programme['programme']['position'],
self.programme['position'],
self.programme['short_synopsis'],
)
class CBeebiesBot(SchedulerBot):
localfile = '/home/sympl/scheduler/data/%s-schedule'
not_got = 'Maintenance mode'
def fetch(self):
pid = SHOWS[self.name]
url = 'http://www.bbc.co.uk/programmes/%s/episodes/upcoming.json'
new = self.get_contents(url % pid)
self.fetch_check_file(new)
return False
def fetch_diff(self, a, b):
return a != b
def parse(self, warn=0):
d = self.parse_get_file(warn)
if not d:
return []
try:
j = json.loads(d)
except ValueError:
return []
events = []
for broadcast in j['broadcasts']:
image_pid = broadcast['programme']['image']['pid']
image_url = 'http://ichef.bbci.co.uk/images/ic/192x108/%s.jpg' % image_pid
time = arrow.get(broadcast['start'])
events.append(
Event(bot=self, time=time, programme=broadcast['programme'],
image_url=image_url)
)
return sorted(events, key=lambda s: s.time)
def alert(self, event, now):
return now >= event.time.shift(minutes=-10) and now < event.time
for key in SHOWS.keys():
argv = sys.argv.copy()
CBeebiesBot(key).run()
sys.argv = argv