Created
January 7, 2010 04:18
-
-
Save sherbondy/270980 to your computer and use it in GitHub Desktop.
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 os, re | |
from datetime import datetime | |
from google.appengine.ext import webapp | |
from google.appengine.ext.webapp import template | |
from google.appengine.ext.webapp.util import run_wsgi_app | |
from google.appengine.api import urlfetch | |
from django.utils import simplejson as json | |
class Post(object): | |
def __init__(self, id, url, date, caption, link_url, | |
photo1280, photo500, photo250, tag): | |
self.id = id | |
self.url = url | |
self.date = date | |
self.caption = caption | |
self.link_url = link_url | |
self.photo1280 = photo1280 | |
self.photo500 = photo500 | |
self.photo250 = photo250 | |
self.tag = tag | |
class Photo(object): | |
MEMBERS = set('self description date title src url'.split()) | |
def __init__(self, **kwargs): | |
if not set(kwargs.keys()) == Photo.MEMBERS: | |
raise TypeError() | |
for k, v in kwargs.iteritems(): | |
setattr(self, k, v) | |
class Song(object): | |
def __init__(self, name, artist, album, src, url, playing): | |
self.name = name | |
self.artist = artist | |
self.album = album | |
self.src = src | |
self.url = url | |
self.playing = playing | |
def strip_tags_slashes(s): | |
r = re.sub(r"\\(n|r)", "\n", s) | |
r = re.sub(r"\\", "", r) | |
r = re.sub(r"<.*?>", "", r) | |
return r | |
def load_tweet(user): | |
url = "http://twitter.com/users/" + user + ".json" | |
try: | |
result = urlfetch.fetch(url) | |
js_object = json.loads(result.content) | |
tweet = js_object['status']['text'] | |
except DownloadError: | |
tweet = "Trouble fetching tweet." | |
return tweet | |
def load_tumblr(user): | |
url = "http://" + user + ".tumblr.com/api/read/json?type=photo&num=20&tagged=project" | |
result = urlfetch.fetch(url) | |
""" tumblr outputs the json as a javascript var, so we have to remove | |
junk at the beginning and the semicolon at the end.""" | |
content = result.content.replace("var tumblr_api_read = ", "") | |
content = content[:-2] | |
js_object = json.loads(content) | |
return js_object | |
def load_flickr(number): | |
url = "http://api.flickr.com/services/feeds/photos_public.gne?id=9656853@N08&lang=en-us&format=json" | |
result = urlfetch.fetch(url) | |
content = result.content.replace("jsonFlickrFeed(", "") | |
content = content[:-1] | |
content = strip_tags_slashes(content) | |
js_object = json.loads(content) | |
flickr = [] | |
i = 0 | |
for item in js_object["items"]: | |
if i < 5: | |
description = item["description"] | |
time_string = item["date_taken"].replace("T"," ") | |
time_string = time_string[:-6] | |
time_format = "%Y-%m-%d %H:%M:%S" | |
date = datetime.strptime(time_string, time_format) | |
title = item["title"] | |
src = item["media"]["m"] | |
url = item["link"] | |
this_photo = Photo(description, date, title, src, url) | |
flickr.append(this_photo) | |
i+=1 | |
return flickr | |
def load_lastfm(user): | |
url = "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=" + user + "&api_key=95a5c200a625641bdaa9b091849f59c9&limit=2&format=json" | |
result = urlfetch.fetch(url) | |
js_object = json.loads(result.content) | |
lastfm = [] | |
for item in js_object["recenttracks"]["track"]: | |
name = item["name"] | |
artist = item["artist"]["#text"] | |
album = item["album"]["#text"] | |
src = item["image"][2]["#text"] | |
url = item["url"] | |
playing = "@attr" in item | |
this_song = Song(name, artist, album, src, url, playing) | |
lastfm.append(this_song) | |
return lastfm | |
def get_posts(tumblr_object): | |
posts = [] | |
for item in tumblr_object["posts"]: | |
id = item["id"] | |
url = item["url"] | |
time_string = item["unix-timestamp"] | |
date = datetime.fromtimestamp(time_string) | |
caption = item["photo-caption"] | |
link_url = item["photo-link-url"] | |
photo1280 = item["photo-url-1280"] | |
photo500 = item["photo-url-500"] | |
photo250 = item["photo-url-250"] | |
tags = item["tags"] | |
tag = tags[0] | |
this_post = Post(id, url, date, caption, link_url, | |
photo1280, photo500, photo250, tag) | |
posts.append(this_post) | |
return posts | |
class MainPage(webapp.RequestHandler): | |
def get(self): | |
tumblr_object = load_tumblr("tehome") | |
description = tumblr_object['tumblelog']['description'] | |
total = tumblr_object['posts-total'] | |
rss = "http://" + tumblr_object['tumblelog']['name'] + ".tumblr.com/rss" | |
posts = get_posts(tumblr_object) | |
template_values = { | |
'tweet': load_tweet("sherbondy"), | |
'description': description, | |
'rss': rss, | |
'posts': posts, | |
'flickr': load_flickr(5), | |
'lastfm': load_lastfm("sherbondy") | |
} | |
path = os.path.join(os.path.dirname(__file__), 'index.html') | |
self.response.out.write(template.render(path, template_values)) | |
application = webapp.WSGIApplication([('/', MainPage)], | |
debug=True) | |
def main(): | |
run_wsgi_app(application) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment