22from flask import json , redirect
33from UrlGenerator import encode_url
44import time
5-
5+ import requests
66
77class model :
88
9- def is_safe (key , url ):
9+ def is_safe (self , key , url ):
10+ URL = 'https://sb-ssl.google.com/safebrowsing/api/lookup?client=Safe&key={key}&appver=1.5.2&pver=3.1&url={url}'
1011 response = requests .get (URL .format (key = key , url = url ))
1112
13+ #print response.text
1214 return response .text != 'malware'
1315
1416 def get_url_stats (self , short_url ):
@@ -30,14 +32,9 @@ def get_url_stats(self, short_url):
3032
3133
3234 def make_short_url (self , url ):
33-
34- import requests
3535
3636 key = 'AIzaSyBwCHhPcVAdwZJH-hlTU4WM_sHe8-_SGYU'
37- URL = 'https://sb-ssl.google.com/safebrowsing/api/lookup?client=Safe&key={key}&appver=1.5.2&pver=3.1&url={url}'
38-
39-
40- if (is_safe (key , url ))
37+ if (self .is_safe (key , url ) == True ):
4138
4239 db = DB ()
4340
@@ -63,6 +60,7 @@ def make_short_url(self, url):
6360 db .query (sql , (url , short_url , 0 , 2 , time .strftime ('%Y-%m-%d %H:%M:%S' ) ))
6461
6562 return self .format_result ("http://goo.rs/" + short_url , 1 , 'Url created' )
63+ return self .format_result ({}, 0 , "This url contains malware and can not be shortened." )
6664
6765
6866
@@ -72,7 +70,6 @@ def format_result(self, result, status, message):
7270
7371 def redirect (self , url ):
7472
75-
7673 db = DB ()
7774
7875 sql = '''SELECT id, long_url FROM links WHERE short_url=%s'''
@@ -86,9 +83,10 @@ def redirect(self, url):
8683 db .query (sql , (link ['id' ], ))
8784 db .close ()
8885
89-
90- return (link ['long_url' ])
91-
86+ key = 'AIzaSyBwCHhPcVAdwZJH-hlTU4WM_sHe8-_SGYU'
87+ if (self .is_safe (key , link ['long_url' ]) == True ):
88+ return (link ['long_url' ])
89+ return False
9290 return False
9391
9492
0 commit comments