forked from snarfed/granary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
117 lines (96 loc) · 3.78 KB
/
app.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
"""Serves the the front page, discovery files, and OAuth flows.
"""
__author__ = 'Ryan Barrett <granary@ryanb.org>'
import json
import logging
import urllib
import urllib2
import appengine_config
from google.appengine.ext import ndb
from oauth_dropins import facebook
from oauth_dropins import flickr
from oauth_dropins import googleplus
from oauth_dropins import instagram
from oauth_dropins import twitter
from oauth_dropins.webutil import handlers
from oauth_dropins.webutil import util
import webapp2
import activitystreams
from granary import atom
from granary import microformats2
from granary import source
API_PARAMS = {
'access_token',
'access_token_key',
'access_token_secret',
'auth_entity',
'format',
}
class FrontPageHandler(handlers.TemplateHandler):
"""Renders and serves the front page."""
def template_file(self):
return 'granary/templates/index.html'
def template_vars(self):
vars = dict(self.request.params)
key = vars.get('auth_entity')
if key:
vars['entity'] = ndb.Key(urlsafe=key).get()
return vars
class DemoHandler(webapp2.RequestHandler):
"""Handles silo requests from the interactive demo form on the front page."""
def get(self):
site = util.get_required_param(self, 'site')
group = self.request.get('group_id', source.ALL)
if group == source.SEARCH:
search_query = self.request.get('search_query', '')
activity_id = ''
else:
activity_id = self.request.get('activity_id', '')
search_query = ''
params = {
'plaintext': 'true',
'search_query': search_query,
}
params.update({name: val for name, val in self.request.params.items()
if name in API_PARAMS})
return self.redirect('/%s/@me/%s/@app/%s?%s' % (
site, group, activity_id, urllib.urlencode(params)))
class UrlHandler(activitystreams.Handler):
"""Handles AS/mf2 requests from the interactive demo form on the front page."""
def get(self):
expected_inputs = ('activitystreams', 'html', 'json-mf2')
input = util.get_required_param(self, 'input')
if input not in expected_inputs:
raise exc.HTTPBadRequest('Invalid input: %s, expected one of %r' %
(input, expected_inputs))
# fetch url
url = util.get_required_param(self, 'url')
logging.info('Fetching %s', url)
resp = urllib2.urlopen(url, timeout=appengine_config.HTTP_TIMEOUT)
if url != resp.geturl():
logging.info('Redirected to %s', resp.geturl())
body = resp.read()
# decode data
if input == 'activitystreams':
activities = json.loads(body)
elif input == 'html':
activities = microformats2.html_to_activities(body, resp.geturl())
elif input == 'json-mf2':
activities = [microformats2.json_to_object(item)
for item in json.loads(body).get('items', [])]
self.write_response(source.Source.make_activities_base_response(activities))
application = webapp2.WSGIApplication([
('/', FrontPageHandler),
('/demo', DemoHandler),
('/facebook/start_auth', facebook.StartHandler.to('/facebook/oauth_callback')),
('/facebook/oauth_callback', facebook.CallbackHandler.to('/')),
('/flickr/start_auth', flickr.StartHandler.to('/flickr/oauth_callback')),
('/flickr/oauth_callback', flickr.CallbackHandler.to('/')),
('/google\\+/start_auth', googleplus.StartHandler.to('/google+/oauth_callback')),
('/google\\+/oauth_callback', googleplus.CallbackHandler.to('/')),
('/instagram/start_auth', instagram.StartHandler.to('/instagram/oauth_callback')),
('/instagram/oauth_callback', instagram.CallbackHandler.to('/')),
('/twitter/start_auth', twitter.StartHandler.to('/twitter/oauth_callback')),
('/twitter/oauth_callback', twitter.CallbackHandler.to('/')),
('/url', UrlHandler),
] + handlers.HOST_META_ROUTES, debug=appengine_config.DEBUG)