-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.py
542 lines (471 loc) · 18.7 KB
/
main.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
import BeautifulSoup
import demjson
import functools
import hashlib
import logging
import os
import uuid
import urllib
from google.appengine.dist import use_library
use_library("django", "1.0")
from django.conf import settings
settings._target = None
os.environ["DJANGO_SETTINGS_MODULE"] = "settings"
from django.template.defaultfilters import slugify
from django.utils import feedgenerator
from django.utils import simplejson
from google.appengine.api import memcache
from google.appengine.api import urlfetch
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.db import djangoforms
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
DOPPLR_TOKEN = getattr(settings, "DOPPLR_TOKEN", None)
MAPS_API_KEY = getattr(settings, "MAPS_API_KEY", None)
SHOW_CURRENT_CITY = getattr(settings, "SHOW_CURRENT_CITY", False)
TITLE = getattr(settings, "TITLE", "Blog")
OLD_WORDPRESS_BLOG = getattr(settings, "OLD_WORDPRESS_BLOG", None)
NUM_RECENT = getattr(settings, "NUM_RECENT", 5)
NUM_MAIN = getattr(settings, "NUM_MAIN", 10)
NUM_FLICKR = getattr(settings, "NUM_FLICKR", 1)
FLICKR_ID = getattr(settings, "FLICKR_ID", None)
webapp.template.register_template_library("filters")
def admin(method):
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
user = users.get_current_user()
if not user:
if self.request.method == "GET":
return self.redirect(users.create_login_url(self.request.uri))
return self.error(403)
elif not users.is_current_user_admin():
return self.error(403)
else:
return method(self, *args, **kwargs)
return wrapper
class MediaRSSFeed(feedgenerator.Atom1Feed):
def root_attributes(self):
attrs = super(MediaRSSFeed, self).root_attributes()
attrs["xmlns:media"] = "http://search.yahoo.com/mrss/"
return attrs
def add_item_elements(self, handler, item):
super(MediaRSSFeed, self).add_item_elements(handler, item)
self.add_thumbnails_element(handler, item)
def add_thumbnail_element(self, handler, item):
thumbnail = item.get("thumbnail", None)
if thumbnail:
if thumbnail["title"]:
handler.addQuickElement("media:title", title)
handler.addQuickElement("media:thumbnail", "", {
"url": thumbnail["url"],
})
def add_thumbnails_element(self, handler, item):
thumbnails = item.get("thumbnails", [])
for thumbnail in thumbnails:
handler.startElement("media:group", {})
if thumbnail["title"]:
handler.addQuickElement("media:title", thumbnail["title"])
handler.addQuickElement("media:thumbnail", "", thumbnail)
handler.endElement("media:group")
class Entry(db.Model):
author = db.UserProperty()
title = db.StringProperty(required=True)
slug = db.StringProperty(required=True)
body = db.TextProperty(required=True)
published = db.DateTimeProperty(auto_now_add=True)
updated = db.DateTimeProperty(auto_now=True)
tags = db.ListProperty(db.Category)
class EntryForm(djangoforms.ModelForm):
class Meta:
model = Entry
exclude = ["author", "slug", "published", "updated", "tags"]
class BaseRequestHandler(webapp.RequestHandler):
def initialize(self, request, response):
webapp.RequestHandler.initialize(self, request, response)
if request.path.endswith("/") and not request.path == "/":
redirect = request.path[:-1]
if request.query_string:
redirect += "?" + request.query_string
return self.redirect(redirect, permanent=True)
def head(self, *args, **kwargs):
pass
def raise_error(self, code):
self.error(code)
self.render("%i.html" % code)
def get_current_city(self):
key = "current_city/now"
current_city = memcache.get(key)
if not current_city:
try:
response = urlfetch.fetch("https://www.dopplr.com/api/traveller_info?format=js&token=" + DOPPLR_TOKEN)
if response.status_code == 200:
data = simplejson.loads(response.content)
current_city = data["traveller"]["current_city"]
current_city["maps_api_key"] = MAPS_API_KEY
memcache.set(key, current_city, 60*60*5)
except (urlfetch.DownloadError, ValueError):
pass
return current_city
def get_recent_entries(self, num=NUM_RECENT):
key = "entries/recent/%d" % num
entries = memcache.get(key)
if not entries:
entries = db.Query(Entry).order("-published").fetch(limit=num)
memcache.set(key, list(entries))
return entries
def get_main_page_entries(self, num=NUM_MAIN):
key = "entries/main/%d" % num
entries = memcache.get(key)
if not entries:
entries = db.Query(Entry).order("-published").fetch(limit=num)
memcache.set(key, list(entries))
return entries
def get_archive_entries(self):
key = "entries/archive"
entries = memcache.get(key)
if not entries:
entries = db.Query(Entry).order("-published")
memcache.set(key, list(entries))
return entries
def get_entry_from_slug(self, slug):
key = "entry/%s" % slug
entry = memcache.get(key)
if not entry:
entry = db.Query(Entry).filter("slug =", slug).get()
if entry:
memcache.set(key, entry)
return entry
def get_tagged_entries(self, tag):
key = "entries/tag/%s" % tag
entries = memcache.get(key)
if not entries:
entries = db.Query(Entry).filter("tags =", tag).order("-published")
memcache.set(key, list(entries))
return entries
def kill_entries_cache(self, slug=None, tags=[]):
memcache.delete("entries/recent/%d" % NUM_RECENT)
memcache.delete("entries/main/%d" % NUM_MAIN)
memcache.delete("entries/archive")
if slug:
memcache.delete("entry/%s" % slug)
for tag in tags:
memcache.delete("entries/tag/%s" % tag)
def get_integer_argument(self, name, default):
try:
return int(self.request.get(name, default))
except (TypeError, ValueError):
return default
def fetch_headers(self, url):
key = "headers/" + url
headers = memcache.get(key)
if not headers:
try:
response = urlfetch.fetch(url, method=urlfetch.HEAD)
if response.status_code == 200:
headers = response.headers
memcache.set(key, headers)
except urlfetch.DownloadError:
pass
return headers
def find_enclosure(self, html):
soup = BeautifulSoup.BeautifulSoup(html)
img = soup.find("img")
if img:
headers = self.fetch_headers(img["src"])
if headers:
enclosure = feedgenerator.Enclosure(img["src"],
headers["Content-Length"], headers["Content-Type"])
return enclosure
return None
def find_thumbnails(self, html):
soup = BeautifulSoup.BeautifulSoup(html)
imgs = soup.findAll("img")
thumbnails = []
for img in imgs:
if "nomediarss" in img.get("class", "").split():
continue
thumbnails.append({
"url": img["src"],
"title": img.get("title", img.get("alt", "")),
"width": img.get("width", ""),
"height": img.get("height", ""),
})
return thumbnails
def generate_sup_id(self, url=None):
return hashlib.md5(url or self.request.url).hexdigest()[:10]
def set_sup_id_header(self):
sup_id = self.generate_sup_id()
self.response.headers["X-SUP-ID"] = \
"http://friendfeed.com/api/public-sup.json#%s" % sup_id
def render_feed(self, entries):
f = MediaRSSFeed(
title=TITLE,
link="http://" + self.request.host + "/",
description=TITLE,
language="en",
)
for entry in entries[:10]:
f.add_item(
title=entry.title,
link=self.entry_link(entry, absolute=True),
description=entry.body,
author_name=entry.author.nickname(),
pubdate=entry.published,
categories=entry.tags,
thumbnails=self.find_thumbnails(entry.body),
)
data = f.writeString("utf-8")
self.response.headers["Content-Type"] = "application/atom+xml"
self.set_sup_id_header()
self.response.out.write(data)
def render_json(self, entries):
json_entries = [{
"title": entry.title,
"slug": entry.slug,
"body": entry.body,
"author": entry.author.nickname(),
"published": entry.published.isoformat(),
"updated": entry.updated.isoformat(),
"tags": entry.tags,
"link": self.entry_link(entry, absolute=True),
} for entry in entries]
json = {"entries": json_entries}
self.response.headers["Content-Type"] = "text/javascript"
self.response.out.write(simplejson.dumps(json, sort_keys=True,
indent=self.get_integer_argument("pretty", None)))
def render(self, template_file, extra_context={}):
if "entries" in extra_context:
format = self.request.get("format", None)
if format == "atom":
return self.render_feed(extra_context["entries"])
elif format == "json":
return self.render_json(extra_context["entries"])
extra_context["request"] = self.request
extra_context["admin"] = users.is_current_user_admin()
extra_context["recent_entries"] = self.get_recent_entries()
if SHOW_CURRENT_CITY:
extra_context["current_city"] = self.get_current_city()
extra_context["flickr_feed"] = self.get_flickr_feed()
extra_context.update(settings._target.__dict__)
template_file = "templates/%s" % template_file
path = os.path.join(os.path.dirname(__file__), template_file)
self.response.out.write(template.render(path, extra_context))
def ping(self, entry=None):
feed = "http://" + self.request.host + "/?format=atom"
args = urllib.urlencode({
"name": TITLE,
"url": "http://" + self.request.host + "/",
"changesURL": feed,
})
response = urlfetch.fetch("http://blogsearch.google.com/ping?" + args)
args = urllib.urlencode({
"url": feed,
"supid": self.generate_sup_id(feed),
})
response = urlfetch.fetch("http://friendfeed.com/api/public-sup-ping?" \
+ args)
args = urllib.urlencode({
"bloglink": "http://" + self.request.host + "/",
})
response = urlfetch.fetch("http://www.feedburner.com/fb/a/pingSubmit?" \
+ args)
def is_valid_xhtml(self, entry):
args = urllib.urlencode({
"uri": self.entry_link(entry, absolute=True),
})
try:
response = urlfetch.fetch("http://validator.w3.org/check?" + args,
method=urlfetch.HEAD)
except urlfetch.DownloadError:
return True
return response.headers["X-W3C-Validator-Status"] == "Valid"
def entry_link(self, entry, query_args={}, absolute=False):
url = "/e/" + entry.slug
if absolute:
url = "http://" + self.request.host + url
if query_args:
url += "?" + urllib.urlencode(query_args)
return url
def get_flickr_feed(self):
if not FLICKR_ID:
return {}
key = "flickr_feed"
flickr_feed = memcache.get(key)
if not flickr_feed:
flickr_feed = {}
args = urllib.urlencode({
"id": FLICKR_ID,
"format": "json",
"nojsoncallback": 1,
})
try:
response = urlfetch.fetch(
"http://api.flickr.com/services/feeds/photos_public.gne?" \
+ args)
if response.status_code == 200:
try:
flickr_feed = demjson.decode(response.content)
memcache.set(key, flickr_feed, 60*5)
except ValueError:
flickr_feed = {}
except urlfetch.DownloadError:
pass
# Slice here to avoid doing it in the template
flickr_feed["items"] = flickr_feed.get("items", [])[:NUM_FLICKR]
return flickr_feed
class ArchivePageHandler(BaseRequestHandler):
def get(self):
extra_context = {
"entries": self.get_archive_entries(),
}
self.render("archive.html", extra_context)
class DeleteEntryHandler(BaseRequestHandler):
@admin
def post(self):
key = self.request.get("key")
try:
entry = db.get(key)
entry.delete()
self.kill_entries_cache(slug=entry.slug, tags=entry.tags)
data = {"success": True}
except db.BadKeyError:
data = {"success": False}
json = simplejson.dumps(data)
self.response.out.write(json)
class EntryPageHandler(BaseRequestHandler):
def head(self, slug):
entry = self.get_entry_from_slug(slug=slug)
if not entry:
self.error(404)
def get(self, slug):
entry = self.get_entry_from_slug(slug=slug)
if not entry:
return self.raise_error(404)
extra_context = {
"entries": [entry], # So we can use the same template for everything
"entry": entry, # To easily pull out the title
"invalid": self.request.get("invalid", False),
}
self.render("entry.html", extra_context)
class FeedRedirectHandler(BaseRequestHandler):
def get(self):
self.redirect("/?format=atom", permanent=True)
class MainPageHandler(BaseRequestHandler):
def head(self):
if self.request.get("format", None) == "atom":
self.set_sup_id_header()
def get(self):
offset = self.get_integer_argument("start", 0)
if not offset:
entries = self.get_main_page_entries()
else:
entries = db.Query(Entry).order("-published").fetch(limit=NUM_MAIN,
offset=offset)
if not entries and offset > 0:
return self.redirect("/")
extra_context = {
"entries": entries,
"next": max(offset - NUM_MAIN, 0),
"previous": offset + NUM_MAIN if len(entries) == NUM_MAIN else None,
"offset": offset,
}
self.render("main.html", extra_context)
class NewEntryHandler(BaseRequestHandler):
def get_tags_argument(self, name):
tags = [slugify(tag) for tag in self.request.get(name, "").split(",")]
tags = set([tag for tag in tags if tag])
return [db.Category(tag) for tag in tags]
@admin
def get(self, key=None):
extra_context = {}
form = EntryForm()
if key:
try:
entry = db.get(key)
extra_context["entry"] = entry
extra_context["tags"] = ", ".join(entry.tags)
form = EntryForm(instance=entry)
except db.BadKeyError:
return self.redirect("/new")
extra_context["form"] = form
self.render("edit.html" if key else "new.html", extra_context)
@admin
def post(self, key=None):
extra_context = {}
form = EntryForm(data=self.request.POST)
if form.is_valid():
if key:
try:
entry = db.get(key)
extra_context["entry"] = entry
except db.BadKeyError:
return self.raise_error(404)
entry.title = self.request.get("title")
entry.body = self.request.get("body")
else:
slug = str(slugify(self.request.get("title")))
if self.get_entry_from_slug(slug=slug):
slug += "-" + uuid.uuid4().hex[:4]
entry = Entry(
author=users.get_current_user(),
body=self.request.get("body"),
title=self.request.get("title"),
slug=slug,
)
entry.tags = self.get_tags_argument("tags")
entry.put()
self.kill_entries_cache(slug=entry.slug if key else None,
tags=entry.tags)
if not key:
self.ping(entry)
valid = self.is_valid_xhtml(entry)
return self.redirect(self.entry_link(entry,
query_args={"invalid": 1} if not valid else {}))
extra_context["form"] = form
self.render("edit.html" if key else "new.html", extra_context)
class NotFoundHandler(BaseRequestHandler):
def head(self):
self.error(404)
def get(self):
self.raise_error(404)
class OldBlogRedirectHandler(BaseRequestHandler):
def get(self, year, month, day, slug):
if not OLD_WORDPRESS_BLOG:
return self.raise_error(404)
self.redirect("http://%s/%s/%s/%s/%s/" %
(OLD_WORDPRESS_BLOG, year, month, day, slug), permanent=True)
class TagPageHandler(BaseRequestHandler):
def get(self, tag):
extra_context = {
"entries": self.get_tagged_entries(tag),
"tag": tag,
}
self.render("tag.html", extra_context)
class OpenSearchHandler(BaseRequestHandler):
def get(self):
self.response.headers["Content-Type"] = "application/xml"
self.render("opensearch.xml")
class SearchHandler(BaseRequestHandler):
def get(self):
self.render("search.html")
application = webapp.WSGIApplication([
("/", MainPageHandler),
("/archive/?", ArchivePageHandler),
("/delete/?", DeleteEntryHandler),
("/edit/([\w-]+)/?", NewEntryHandler),
("/e/([\w-]+)/?", EntryPageHandler),
("/new/?", NewEntryHandler),
("/t/([\w-]+)/?", TagPageHandler),
("/(\d+)/(\d+)/(\d+)/([\w-]+)/?", OldBlogRedirectHandler),
("/feed/?", FeedRedirectHandler),
("/opensearch.xml/?", OpenSearchHandler),
("/search/?", SearchHandler),
("/.*", NotFoundHandler),
], debug=True)
def main():
logging.getLogger().setLevel(logging.DEBUG)
run_wsgi_app(application)
if __name__ == "__main__":
main()