Created
September 7, 2011 21:18
-
-
Save symroe/1201766 to your computer and use it in GitHub Desktop.
FMT to JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
Written in reply to this post on the mySociety mailing list: | |
https://secure.mysociety.org/admin/lists/pipermail/developers-public/2011-September/007570.html | |
Can be deployed somewhere if someone would just like to get some JSON back from | |
an HTTP request, just contact me here or @symroe on twitter. | |
""" | |
import urllib2 | |
import json | |
import BeautifulSoup | |
class FmtToJson(): | |
""" | |
Takes a FMT slug and retuns a JSON representation of that page. | |
""" | |
def __init__(self, slug): | |
self.FMT_BASE = "http://www.fixmytransport.com" | |
self.slug = slug | |
self.url = "%s/campaigns/%s" % (self.FMT_BASE, self.slug) | |
def get_page(self): | |
page = urllib2.urlopen(self.url).read() | |
self.soup = BeautifulSoup.BeautifulSoup(page) | |
return page | |
def as_dict(self): | |
if not hasattr(self, 'page'): | |
self.get_page() | |
obj = {} | |
obj['title'] = self.soup.find('div', {'class' : "leftcol-banner"}).h1.string | |
obj['url'] = self.url | |
obj['meta'] = { | |
'started' : self.soup.find('ul', {'class' : 'campaign-meta'}).findAll('li')[0].span.string, | |
'supporters' : self.soup.find('ul', {'class' : 'campaign-meta'}).findAll('li')[1].string.split(' ')[-1] | |
} | |
obj['description'] = unicode(self.soup.find('div', {'class' : 'box campaign-description'}).div) | |
return obj | |
def as_json(self): | |
return json.dumps(self.as_dict()) | |
if __name__ == "__main__": | |
F = FmtToJson('fix-the-dire-cycle-parking-shortage-at-cambridge-s') | |
print F.as_json() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import web | |
from fmt_to_json import FmtToJson | |
urls = ( | |
'/(.*)', 'to_json' | |
) | |
app = web.application(urls, globals()) | |
app_wsgi = app.wsgifunc() | |
class to_json: | |
def GET(self, slug): | |
F = FmtToJson(slug) | |
return F.as_json() | |
if __name__ == "__main__": | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment