Wednesday, 24 February 2010

First usefull appengine app

I wrote my first useful appengine app!

http://smremde-rssfeeds.appspot.com/chpics

This takes the CH Picture feed and creates a new feed without all the crap and inserts the full resolution picture in instead of the thumbnails. I'm gonna do one for the videos too.

Feel free to add this feed to your reader :)


Update: Video one here http://smremde-rssfeeds.appspot.com/chvids

Code:

#!/usr/bin/env python

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.api import urlfetch
from google.appengine.api import memcache

import feedparser
import datetime
import PyRSS2Gen
import re

class chpics(webapp.RequestHandler):

def get(self):

# we will cache it for 5 minutes
rss = memcache.get('rss')
if rss is not None:
rss.write_xml(self.response.out)
return

# download the rss feed
feed = feedparser.parse('http://feeds.feedburner.com/collegehumor/pictures')

# create a new rss feed
rss = PyRSS2Gen.RSS2( title = feed.feed.title,
link = '', description = '', pubDate = None)

for x in feed['entries']:

# see if we have cached this url
u = x['feedburner_origlink'];
data = memcache.get(u)

if data is None:
# go get the CH page and extract the full res image url
f = urlfetch.fetch(u)

if f.status_code == 200:
m = re.search('\<link rel="image_src" href="(.*)"', f.content)

if m is not None:
# create the rssitem and cache it

data = PyRSS2Gen.RSSItem(
title =x.title,
link = x.link,
description = "<img src='" + m.group(1) + "' />")

memcache.set(u, data)

if data is not None:
rss.items.append(data)

# cache the feed for 5 mins and output it
memcache.set('rss', rss, 5 * 60)

rss.write_xml(self.response.out)


def main():
application = webapp.WSGIApplication([('/chpics', chpics)],
debug=True)
util.run_wsgi_app(application)


if __name__ == '__main__':
main()

No comments: