-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebserver.py
170 lines (145 loc) · 6.33 KB
/
webserver.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# -*- coding: utf-8 -*-
#!/usr/bin/env python
"""
Main Web Server.
Requires Python 2.7
"""
import os
import tornado.ioloop
import tornado.escape
#import tornado.httpclient
import tornado.web
from tornado.web import HTTPError
import urllib2
from recaptcha.client import captcha
from exceptions import *
from logger import Logger
from lookup import Lookup
from config import CONFIG
settings = {
"static_path": CONFIG.static_dir,
"cookie_secret": CONFIG.cookie_secret,
# "login_url": "/login",
"xsrf_cookies": True,
}
class HomeHandler(tornado.web.RequestHandler):
def get(self):
recaptcha = captcha.displayhtml(CONFIG.WEB.captcha_public_key)
self.render("template/home.html", captcha=recaptcha, error=None)
class ViewTwitterMapHandler(tornado.web.RequestHandler):
def get(self, username):
gexf = "twitter_com_"+username.lower()+".gexf"
path = os.path.join("static", gexf)
if os.path.exists(path):
self.render("template/view-twitter.html", username=username, gexf=gexf)
else:
recaptcha = captcha.displayhtml(CONFIG.WEB.captcha_public_key)
self.render("template/request-twitter.html", username=username, error=None, captcha=recaptcha)
class CreateTwitterMapHandler(tornado.web.RequestHandler):
def initialize(self):
self.__logger__ = Logger(self.__class__.__name__)
def post(self):
try:
username = self.get_argument("username")
except HTTPError, e:
recaptcha = captcha.displayhtml(CONFIG.WEB.captcha_public_key)
self.render("template/home.html", captcha=recaptcha, error="The username is required.")
# try:
# recaptcha_challenge = self.get_argument("recaptcha_challenge_field")
# recaptcha_response = self.get_argument("recaptcha_response_field")
# except HTTPError, e:
# recaptcha = captcha.displayhtml(CONFIG.WEB.captcha_public_key)
# self.render("template/home.html", captcha=recaptcha, error="The captcha must be filled.")
#
# response = captcha.submit(
# recaptcha_challenge,
# recaptcha_response,
# WEB.captcha_private_key,
# self.request.remote_ip
# )
#
# if not response.is_valid:
# recaptcha = captcha.displayhtml(CONFIG.WEB.captcha_public_key)
# self.render("template/home.html", captcha=recaptcha, error="The captcha is incorrect.")
# else:
#CAPTCHA valid
gexf = "twitter_com_"+username.lower()+".gexf"
# Does the gexf exist?
path = os.path.join("static", gexf)
if os.path.exists(path):
self.redirect('/view/twitter/'+username)
else:
# Push a new job through the API
try:
answer = Lookup().pushNode(tornado.escape.url_escape("http://twitter.com/" + username))
if answer == "ok":
self.redirect('/view/twitter/'+username)
else:
recaptcha = captcha.displayhtml(CONFIG.WEB.captcha_public_key)
self.render("template/request-twitter.html", username=username, error=answer, captcha=recaptcha)
except IOError, e:
self.__logger__.error(str(e))
error = str(e)
recaptcha = captcha.displayhtml(CONFIG.WEB.captcha_public_key)
self.render("template/request-twitter.html", username=username, error=error, captcha=recaptcha)
class OrderTwitterPdfHandler(tornado.web.RequestHandler):
def get(self):
self.render("template/order-twitter-pdf.html")
class OrderPdfHandler(tornado.web.RequestHandler):
def get(self):
self.redirect('/order/twitter/pdf/', permanent=True)
class FinishOrderTwitterPdfHandler(tornado.web.RequestHandler):
def get(self):
self.render("template/finish-order-twitter-pdf.html", captcha=None)
class AddNotificationHandler(tornado.web.RequestHandler):
def initialize(self):
self.__logger__ = Logger(self.__class__.__name__)
def post(self):
try:
map_id = self.get_argument("map_id")
notified_username = self.get_argument("notified_username")
# try:
# recaptcha_challenge = self.get_argument("recaptcha_challenge_field")
# recaptcha_response = self.get_argument("recaptcha_response_field")
# except HTTPError, e:
# recaptcha = captcha.displayhtml(CONFIG.WEB.captcha_public_key)
# self.render("template/request-twitter.html", captcha=recaptcha, error="The captcha must be filled.")
#
# response = captcha.submit(
# recaptcha_challenge,
# recaptcha_response,
# WEB.captcha_private_key,
# self.request.remote_ip
# )
#
# if not response.is_valid:
# recaptcha = captcha.displayhtml(CONFIG.WEB.captcha_public_key)
# self.render("template/request-twitter.html", captcha=recaptcha, error="The captcha is incorrect.")
# else:
#CAPTCHA valid
# Push a new notification through the API
try:
answer = Lookup().pushNotification(tornado.escape.url_escape(map_id), notified_username)
if answer == "ok":
self.render("template/generic-msg.html", msg="@visu_web will send a direct message to @"+notified_username+" once the job is done.")
else:
self.render("template/generic-msg.html", msg=answer)
except IOError, e:
self.__logger__.error(str(e))
self.render("template/generic-msg.html", msg=str(e))
except HTTPError, e:
self.render("template/generic-msg.html", msg="Your Twitter username is required.")
application = tornado.web.Application([
(r"/", HomeHandler),
(r"/view/twitter/([A-Za-z0-9]+)", ViewTwitterMapHandler),
(r"/create/twitter/", CreateTwitterMapHandler),
(r"/order/twitter/pdf/", OrderTwitterPdfHandler),
(r"/finish-order/twitter/pdf/", FinishOrderTwitterPdfHandler),
(r"/order/pdf/", OrderPdfHandler),
(r"/add-notification/", AddNotificationHandler),
(r"/", tornado.web.StaticFileHandler,
dict(path=settings['static_path']))
], **settings)
if __name__ == "__main__":
application.listen(CONFIG.WEB.port)
tornado.ioloop.IOLoop.instance().start()