-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.py
303 lines (287 loc) · 10.6 KB
/
handlers.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
"""
Main Handlers and supporting methods for the app
"""
import datetime
import inspect
import string
import random
import sqlite3
import logging as logger
import tornado.ioloop
import tornado.web
import tornado.httpclient as httpclient
from bs4 import BeautifulSoup
from base import BaseHandler
from settings import SETTINGS as dummySettings
def _execute(query, fetchall=True):
"""Single method to execute all sql queries
"""
connection = sqlite3.connect(dummySettings['DBPATH'])
cursorobj = connection.cursor()
try:
cursorobj.execute(query)
if fetchall is True:
result = cursorobj.fetchall()
else:
result = cursorobj.fetchone()
connection.commit()
except Exception:
raise
connection.close()
return result
def get_url_title(url):
"""To fetch title of page
"""
http_client = httpclient.HTTPClient()
try:
response = http_client.fetch(url)
soup = BeautifulSoup(response.body, 'html.parser')
title = soup.title.string
except httpclient.HTTPError as e_exp:
# HTTPError is raised for non-200 responses; the response
# can be found in e.response.
msg = "something went wrong: %s" % str(e_exp)
logger.info(msg)
title = 0
except Exception as e_exp:
# Other errors are possible, such as IOError.
msg = "something went wrong: %s" % str(e_exp)
logger.info(msg)
title = 0
http_client.close()
return title
def update_url_title(url=None, row=None):
"""To update title of page in db
"""
try:
if row is None or url is None:
msg = "row to update title is not existant"
logger.info(msg)
else:
title = get_url_title(url)
updated_at = datetime.datetime.now()
query = ''' update urlsbase
set title = '%s', updated_at = '%s'
where id = %d ''' % (title, updated_at, row)
_execute(query)
except Exception as e_exp:
msg = "something went wrong: %s" % e_exp
logger.info(msg)
def check_url_existence(url=None, url_hash=None):
"""To if url hash already generated
"""
try:
if url != None:
query = '''select * from urlsbase WHERE url like '%s' ''' % (url)
elif url_hash != None:
query = '''select * from urlsbase WHERE shrink like '%s' ''' % (url_hash)
else:
return None
row = _execute(query, False)
if row != None:
return row
except Exception as e_exp:
msg = "something went wrong: %s" % e_exp
logger.info(msg)
return None
def update_url_hit(row=None, url=None, url_hash=None):
"""To update hit count on url
"""
try:
lasthit_at = datetime.datetime.now()
if row != None:
query = ''' update urlsbase
set hits = hits+1, lasthit_at = '%s'
where id = %d ''' % (lasthit_at, row)
elif url != None:
query = ''' update urlsbase
set hits = hits+1, lasthit_at = '%s'
where url = %s ''' % (lasthit_at, url)
elif url_hash != None:
query = ''' update urlsbase
set hits = hits+1, lasthit_at = '%s'
where shrink = %d ''' % (lasthit_at, url_hash)
else:
return None
row = _execute(query, False)
if row != None:
return True
except Exception as e_exp:
msg = "something went wrong: %s" % e_exp
logger.info(msg)
return None
def timestamp_parser(timestamp_str=None):
"""DB timestamp to datetime
"""
if timestamp_str != None:
return datetime.datetime.strptime(timestamp_str, "%Y-%m-%d %H:%M:%S.%f")
else:
return None
def timestamp_to_hooman(timestamp_str=None):
"""DB timestamp to human readable
"""
if timestamp_str != None:
date_time = timestamp_parser(timestamp_str)
return date_time.strftime("%A ,%d %b %Y, %I:%M:%S %p")
else:
return None
##Handlers below
class MainHandler(BaseHandler):
"""List of all urls and handler descriptions
"""
def get(self):
app_handlers = [(handler.regex.pattern, handler.handler_class) for handler in self.application.handlers[0][1]]
self.response = []
for handler in app_handlers:
temp_dict = {}
temp_dict['url'] = handler[0]
temp_dict['description'] = inspect.getdoc(handler[1])
self.response.append(temp_dict)
self.write_json()
class RedirectHandler(BaseHandler):
"""Redirect to url and asynchronously updates hit count
"""
@tornado.web.asynchronous
def get(self, url_hash):
if url_hash is None:
self.redirect("/")
else:
row = check_url_existence(None, url_hash)
if row is None:
self.send_error(404, message="Requested url not found") # Bad Request
else:
self.redirect(row[1])
update_url_hit(row[0])
return
class URLshrinkHandler(BaseHandler):
"""Checks url existence, creates short url and updates title to db entry of url asynchronously
"""
@tornado.web.asynchronous
def post(self):
try:
url = self.get_json_argument('u')
row = check_url_existence(url)
#check url existence and generate hash and insert to db
if row is None:
url_hash = self.create_hash()
created_at = datetime.datetime.now()
updated_at = datetime.datetime.now()
lasthit_at = datetime.datetime.now()
query = ''' insert into urlsbase
(url, shrink, created_at, updated_at, lasthit_at) values
('%s', '%s', '%s', '%s', '%s') ''' % (url, url_hash, created_at, updated_at, lasthit_at)
_execute(query)
else:
url_hash = row[3]
url = row[1]
created_at = row[5]
updated_at = row[6]
lasthit_at = row[7]
self.response['url'] = url
self.response['short_url'] = self.get_short_url(url_hash)
self.write_json()
self.finish()
if row is None:
#get title and update row
query = '''select id from urlsbase WHERE shrink = '%s' ''' % (url_hash)
row = _execute(query, False)
if row != None:
update_url_title(url, row[0])
logger.info("Tile of url fetched, added to DB")
except Exception as e_exp:
msg = "something went wrong: %s" % e_exp
logger.info(msg)
if not self._finished:
#if the connection is closed, it won't call this function
self.send_error(400, message="something went wrong") # Bad Request
else:
pass
def create_hash(self):
"""Creates short url as hash
"""
_hash = ''.join(random.choice(string.ascii_lowercase + string.ascii_uppercase + string.digits) for _ in range(7))
query = '''select id from urlsbase WHERE shrink = '%s' ''' % (_hash)
rows = _execute(query)
if len(rows) == 0:
return _hash
else:
return self.create_hash()
class TitleSearchHandler(BaseHandler):
"""
Search url using page title
"""
def post(self):
try:
escape_char = '%'
search_query = self.get_json_argument('q')
query = '''select * from urlsbase
where title like '%c%s%c' ''' % (escape_char, search_query, escape_char)
rows = _execute(query)
self.response = []
# collate results as json list
for entries in rows:
temp_dict = {}
temp_dict['title'] = entries[2]
temp_dict['url'] = entries[1]
temp_dict['short_url'] = self.get_short_url(entries[3])
self.response.append(temp_dict)
self.write_json()
except Exception as e_exp:
msg = "something went wrong: %s" % e_exp
logger.info(msg)
if not self._finished:
#if the connection is closed, it won't call this function
self.send_error(400, message="something went wrong") # Bad Request
else:
pass
class URLMetaListHandler(BaseHandler):
"""Lists all meta urls for links
"""
def get(self):
try:
query = '''select * from urlsbase'''
rows = _execute(query)
self.response = []
# collate results as json list
for entries in rows:
temp_dict = {}
temp_dict['meta_url'] = self.get_short_url("meta/%s" % (entries[3]))
self.response.append(temp_dict)
self.write_json()
except Exception as e_exp:
msg = "something went wrong: %s" % e_exp
logger.info(msg)
if not self._finished:
#if the connection is closed, it won't call this function
self.send_error(400, message="something went wrong") # Bad Request
else:
pass
class URLMetaHandler(BaseHandler):
"""Return meta details of short url
"""
def get(self, url_hash):
try:
if url_hash != None:
row = check_url_existence(None, url_hash)
# collate required fields
if row != None:
self.response['url'] = row[1]
self.response['title'] = row[2]
self.response['short_url'] = self.get_short_url(row[3])
self.response['no_hits'] = row[4]
self.response['created_at'] = timestamp_to_hooman(row[5])
self.response['updated_at'] = timestamp_to_hooman(row[6])
self.response['last_hit_at'] = timestamp_to_hooman(row[7])
self.write_json()
else:
self.send_error(404, message="Short url doesnt exist")
else:
self.send_error(404, message="Short url doesnt exist")
except Exception as e_exp:
msg = "something went wrong: %s" % e_exp
logger.info(msg)
if not self._finished:
#if the connection is closed, it won't call this function
self.send_error(400, message="something went wrong") # Bad Request
else:
pass