Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Tree: 851b99b314
Fetching contributors…

Cannot retrieve contributors at this time

230 lines (180 sloc) 6.327 kB
#!/usr/bin/env python
from datetime import datetime
import collections
import logging
import os
import sys
import urllib, urllib2, urlparse
from django.utils import simplejson as json
from secret import Facebook
print """You need to setup the application secret.
Then put in your application id and secret."""
import wsgiref.handlers
import facebook
from google.appengine.api import memcache
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
class UserHandler(webapp.RequestHandler):
def getFBGraph(self):
user = facebook.get_user_from_cookie(self.request.cookies, Facebook.appId, Facebook.secret)
if user:
graph = facebook.GraphAPI(user["access_token"])
try :
me = graph.get_object("me")
except Exception, e:
return None
return graph
def getFriends(self):
graph = self.getFBGraph()
if graph:
friends = graph.get_connections("me", "friends")['data']
def getName(friend) :
if friend.has_key('name'):
return friend['name']
return ''
return sorted(friends, key=getName)
return []
def getMe(self):
graph = self.getFBGraph()
if graph:
return graph.get_object("me")
return {}
def getID(self):
return self.getMe()['id']
def isAttending(self, eid):
graph = self.getFBGraph()
if graph:
attending = graph.get_connections(eid, 'attending')['data']
my_id = self.getID()
for person in attending:
if person['id'] == my_id:
return True
return False
class AppHandler(webapp.RequestHandler):
def fetch(self, url, isJson=True, cached=60): # 1 minute'fetching: '+url)
if cached:
if memcache.get(url):
return memcache.get(url)'cache miss: '+url)
result = urllib2.urlopen(url)
except urllib2.HTTPError, e:
print '\n\n', e.geturl()
raise e
if isJson:
ret = json.loads(
else :
ret =
if cached:
memcache.add(url, ret, cached)
return ret
def getAppOAuthToken(self):
response = self.fetch('' + urllib.urlencode({
'grant_type': 'client_credentials',
'client_id': Facebook.appId,
'client_secret': Facebook.secret}), isJson=False)
for piece in response.split('&') :
k,v = piece.split('=')
if k == 'access_token':
return v
def getEvents(self, page_id):
events = self.fetch(''+page_id+'/events?access_token='+self.getAppOAuthToken())['data']
for event in events:
# too slow...
# event['count'] = self.getHackCount(event['id'])
event['start_time'] = datetime.strptime(event['start_time'], "%Y-%m-%dT%H:%M:%S+0000")
event['end_time'] = datetime.strptime(event['end_time'], "%Y-%m-%dT%H:%M:%S+0000")
return events
def getEvent(self, eid):
return self.fetch(''+eid)
def getRawHacks(self, eid):
data = []
url = ''+str(eid)+'/feed?access_token='+self.getAppOAuthToken()
while 1:
feed = self.fetch(url)
data += feed['data']
if feed.has_key('paging'):
if url == feed['paging']['next']:
# bug in facebook
url = feed['paging']['next']
return data
def getHacks(self, eid):
hacks = []
for hack in self.getRawHacks(eid):
if hack.has_key('application'):
if hack['application']['id'] == Facebook.appId:
ret = []
for hack in hacks:
if not hack.has_key('picture') or not hack.has_key('to') or not hack['to'].has_key('data') or not hack.has_key('id'):
people = filter(lambda x: x and x.has_key('id') and x['id'] != eid, hack['to']['data'])
other_eid, fbid = hack['id'].split('_')
# print '\n\n', hack
'description' : hack.get('description'),
'link' : ''+eid+'&story_fbid='+fbid,
'likes' : hack.get('likes', {'count' : 0}).get('count'),
'people' : people,
'screenshot' : hack.get('picture'),
'screenshot_raw' : hack.get('link'),
'title' : hack.get('name'),
return ret
def getHackCount(self, eid):
return len(self.getHacks(eid))
class IndexHandler(AppHandler):
def get(self):
template_values = {
'events' : self.getEvents('167580640987') + self.getEvents('114869201895800')
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))
def post(self):
return self.get()
class EventHandler(AppHandler):
def get(self, eid):
template_values = {
'hacks' : self.getHacks(eid),
'eid' : eid,
'event' : self.getEvent(eid),
path = os.path.join(os.path.dirname(__file__), 'event.html')
self.response.out.write(template.render(path, template_values))
def post(self, eid):
return self.get(eid)
class EventRedirectHandler(webapp.RequestHandler):
def get(self, eid):
def post(self, eid):
return self.get(eid)
class EventSubmissionHandler(AppHandler, UserHandler):
def post(self, eid):
return self.get(eid)
def get(self, eid):
template_values = {
'eid' : eid,
'event' : self.getEvent(eid),
'friends' : self.getFriends(),
'me' : self.getMe(),
'isAttending' : self.isAttending(eid),
path = os.path.join(os.path.dirname(__file__), 'event_submit.html')
self.response.out.write(template.render(path, template_values))
def main():
application = webapp.WSGIApplication([('/+', IndexHandler),
('/+([0-9]*)/*', EventHandler),
('/+([0-9]*)/+event', EventRedirectHandler),
('/+([0-9]*)/+submit', EventSubmissionHandler)],
if __name__ == '__main__':
Jump to Line
Something went wrong with that request. Please try again.