-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.py
131 lines (109 loc) · 4.1 KB
/
api.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
118
119
120
121
122
123
124
125
126
127
128
129
130
# -*- coding: utf-8 -*-
#!/usr/bin/env python
"""
API Server.
Requires Python 2.7
"""
import tornado.auth
import tornado.ioloop
import tornado.escape
import tornado.httpclient
import tornado.web
import urllib2
from time import sleep
from exceptions import *
from notification_map import NotificationMultimap
from logger import Logger
from cache import Cache
from job_queue import JobQueue
from lookup import Lookup
from config import CONFIG
settings = {
"static_path": CONFIG.static_dir,
"twitter_consumer_key": "Q636WeJdWBEXSfMTTZbw",
"twitter_consumer_secret": "l3bVkk2H1e0KFlImQLQkuJRn9uICkzpNE36TJH59yjY"
}
queue = JobQueue()
class StatusHandler(tornado.web.RequestHandler):
def get(self):
self.write("ok")
# App: https://dev.twitter.com/apps/1357631/settings
class TwitterAuthHandler(tornado.web.RequestHandler, tornado.auth.TwitterMixin):
@tornado.web.authenticated
@tornado.web.asynchronous
def get(self, username, link):
self.twitter_request(
"/statuses/update",
post_args={"status": "Testing Tornado Web Server"}, # "d @" + username +" the visualization is ready: " + link
access_token=user["access_token"],
callback=self.async_callback(self._on_post))
def _on_post(self, new_entry):
if not new_entry:
# Call failed; perhaps missing permission?
self.authorize_redirect()
return
self.finish("Posted a message!")
class PushNodeHandler(tornado.web.RequestHandler):
def initialize(self):
self.__logger__ = Logger(self.__class__.__name__)
def get(self, node):
#TODO securiser l'accès via un token?
#try:
# ex node: "http://twitter.com/mentatseb"
# Check if the username exists
#request = urllib2.Request(node, headers={'User-Agent' : "VisuWeb 1.0"})
#connexion = urllib2.urlopen( request )
#connexion.read()
while queue.isLocked():
sleep(0.1)
queue.put(node)
self.write("ok")
#except IOError, e:
#self.__logger__.error(str(e)+" on "+node)
#if hasattr(e, 'reason'):
# print 'We failed to reach a server.'
#self.write(e.reason)
#elif hasattr(e, 'code'):
# print 'The server couldn\'t fulfill the request.'
#self.write(e.code)
class FetchNodeHandler(tornado.web.RequestHandler):
'''Fetch a node and its inbound links from Google SocialGraph API'''
__cache__ = Cache()
@tornado.web.asynchronous
def get(self, node):
self.__node__ = node
#TODO securiser l'accès via un token?
# If the node has been checked recently then pump in the cache,
# otherwise fetch fresh data on the Web.
if self.__cache__.isCached(node):
json = self.__cache__.load(node)
self.on_response_cache(json)
else:
http = tornado.httpclient.AsyncHTTPClient()
http.fetch("http://socialgraph.apis.google.com/lookup?q="+node+"&edi=1", callback=self.on_response)
def on_response(self, response):
if response.error:
raise tornado.web.HTTPError(500)
self.write(response.body)
self.__cache__.cache(self.__node__, response.body)
self.finish()
def on_response_cache(self, response):
self.write(response)
self.finish()
class PushNotificationHandler(tornado.web.RequestHandler):
def get(self, map_id, user):
nm = NotificationMultimap()
nm[map_id] = user
self.write("ok")
application = tornado.web.Application([
(r""+Lookup().PATTERN.status, StatusHandler),
(r""+Lookup().PATTERN.push_node, PushNodeHandler),
(r""+Lookup().PATTERN.fetch_node, FetchNodeHandler),
(r""+Lookup().PATTERN.push_notification, PushNotificationHandler),
(r"/api/tweet/([A-Za-z0-9]+)", TwitterAuthHandler), #TODO
(r"/", tornado.web.StaticFileHandler,
dict(path=settings['static_path']))
], **settings)
if __name__ == "__main__":
application.listen(CONFIG.API.port)
tornado.ioloop.IOLoop.instance().start()