-
Notifications
You must be signed in to change notification settings - Fork 0
/
tvservice.py
290 lines (226 loc) · 7.53 KB
/
tvservice.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import nanoweb
import routes
from routes.middleware import RoutesMiddleware
from webob import Response
from webob.dec import wsgify
from webob.exc import *
from contextlib import contextmanager
from pyquery import PyQuery
import requests
from static import Cling
import json
import os
import re
PASSWORD_FILE = os.environ['PASSWORD_FILE']
FEED_URL = os.environ['FEED_URL']
DB_ROOT = os.environ['WRITABLE_ROOT']
DB_FILE = os.path.join(DB_ROOT, "shows.json")
EPISODE_DB_FILE = os.path.join(DB_ROOT, "episodes.json")
##
# Models
##
@contextmanager
def db(db_file,
initial_value,
load=lambda x:x,
dump=lambda x:x):
if os.path.exists(db_file):
data = load(json.load(open(db_file)))
else:
data = initial_value
yield data
json.dump(dump(data), open(db_file, "w"))
def shows_db():
return db(DB_FILE, {})
def detect_show(show_list, title):
"""Takes a title and returns a (show, episode) tuple or None if not found.
Args:
show_list
A list of show titles to match on
title
title CDATA from a RSS field
Types:
show
A string matching the show name in the database
episode
A string in the format S\d\dE\d\d"""
def normalize(string):
return re.sub(r"\W+", " ", string).lower()
pats =\
[(name, re.compile(r"\b%s\b" % re.escape(normalize(name)), re.I))
for name in show_list]
episode_pat = re.compile("(S\d\dE\d\d|\d{4} \d{2} \d{2})", re.I)
normailized_title = normalize(title)
for name, pat in pats:
if pat.search(normailized_title):
# extract the episode slug
match = episode_pat.search(title)
if match:
return (name, match.group(0).upper())
def init_episodes():
"""The episode data structure is a map of sets.
The key of the map is the show's canonical name
Each element in the set is (episode_slug, rss_title)"""
return {}
def dump_episodes(data):
"""The show's seen episode set is serialized as a JSON list"""
newdata = {}
for key, value in data.items():
newdata[key] = list(value)
return newdata
def load_episodes(data):
"""The show's seen episode JSON list is deserialized as a python set"""
newdata = {}
for key, value in data.items():
# Convert the elements to a hashable value
newvalue = (tuple(e) for e in value)
newdata[key] = set(newvalue)
return newdata
def episodes_db():
return db(EPISODE_DB_FILE,
init_episodes,
dump=dump_episodes,
load=load_episodes)
def episode_seen(episodes, name, episode_slug, rss_title):
show_data = episodes.get(name, set([]))
show_data.add((episode_slug, rss_title))
episodes[name] = show_data
return episodes
def episode_is_dupe(episodes, name, episode_slug, rss_title):
show_data = episodes.get(name, set([]))
was_seen = (episode_slug, rss_title) in show_data
# Any unknown episodes are obviously not a dupe
# Any rss titles that have already been seen are not dupes
if name not in episodes or was_seen:
return False
else:
# Anything else is a dupe
return True
##
# WSGI Apps
##
@wsgify
def shows(request):
nanoweb.allowed(request, ["GET"])
content_type = nanoweb.agent_accepts(request, ["application/json"])
with shows_db() as shows:
response = Response(nanoweb.encode_body(content_type, shows),
content_type=content_type)
return response
@wsgify
def show(request):
nanoweb.allowed(request, ["GET", "PUT", "DELETE"])
content_type = nanoweb.agent_accepts(request, ["text/plain",
"application/json"])
# Set up the encoders
encoders = {"text/plain": lambda d: d['title']}
encoders.update(nanoweb.encoders)
# Set up the decoders
decoders = {"text/plain": lambda s: {"title": s}}
decoders.update(nanoweb.decoders)
url = request.environ['routes.url']
slug = request.urlvars['slug']
if request.method == "PUT":
data = nanoweb.decode_body(request, decoders=decoders)
with shows_db() as shows:
shows[slug] = data['title']
body = nanoweb.encode_body(content_type, data,
encoders=encoders)
return Response(body, content_type=content_type)
elif request.method == "DELETE":
try:
with shows_db() as shows:
del shows[slug]
return HTTPNoContent()
except KeyError:
return HTTPNotFound()
elif request.method == "GET":
try:
with shows_db() as shows:
name = shows[slug]
except KeyError:
return HTTPNotFound()
data = {"title": name, "slug": slug}
body = nanoweb.encode_body(content_type, data,
encoders=encoders)
return Response(name, content_type="text/plain")
def get_feed():
r = requests.get(FEED_URL)
return r.content
@wsgify
def feed(request, get_feed=get_feed):
with shows_db() as shows:
show_list = shows.values()
d = PyQuery(get_feed(), parser="xml")
for item in d("item"):
ditem = PyQuery(item)
title = ditem.find("title").text()
match = detect_show(show_list, title)
if match:
name, episode = match
# TODO: Record episode in the feed so that future versions of this episod will be ignored
else:
ditem.remove()
response = Response()
response.content_type = "application/rss+xml"
response.ubody = unicode(d)
response.cache_control = "no-cache"
return response
##
## WSGI Middleware
##
class BasicAuth(object):
def __init__(self, app, password_map, realm):
self.app = app
self.password_map = password_map
self.realm = realm
@wsgify
def __call__(self, request):
badauth_response = HTTPUnauthorized("Use basic authorization",
www_authenticate='Basic realm="%s"' % (self.realm, ))
if request.authorization is None \
or request.authorization[0].lower() != "basic":
raise badauth_response
authtype, param = request.authorization
username, password = param.decode("base64").split(":")
if username in self.password_map\
and self.password_map[username] == password:
return request.get_response(self.app)
else:
raise badauth_response
##
## Shows sub app config
##
def shows_subapp():
"""This is a factory for building the shows app that is mapped to
/shows/
This subapp is needed because it requires authentication"""
password_map = json.load(open(PASSWORD_FILE))['passwords']
apps = {
"shows": shows,
"show": show,
}
mapper = routes.Mapper()
mapper.connect("/:slug", application="show")
mapper.connect("/", application="shows")
subapp = nanoweb.FrontController(apps)
subapp = RoutesMiddleware(subapp, mapper)
subapp = BasicAuth(subapp, password_map, "shows")
return subapp
##
## TVService apps
##
apps = {
"shows": shows_subapp(),
"feed": feed,
}
mapper = routes.Mapper()
mapper.connect("/shows/{path_info:.*}", application="shows")
mapper.connect("/feed/", application="feed")
STATIC_ROOT = os.environ.get('STATIC_ROOT')
if STATIC_ROOT:
static = Cling(STATIC_ROOT)
apps['static'] = static
mapper.connect("/{path_info:.*}", application="static")
application = nanoweb.FrontController(apps)
application = RoutesMiddleware(application, mapper)