-
Notifications
You must be signed in to change notification settings - Fork 2
/
web_app.py
109 lines (93 loc) · 2.89 KB
/
web_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
from flask import Flask, request, render_template , jsonify
from geolite2 import geolite2
import urllib3.util.url as U_R_L
import socket
import re
import DB
app = Flask(__name__ )
table_dict = {
'car' : u'车辆',
'face' : u'人脸',
'fire' : u'火源',
'pacer' : u'行人'
} # from chinese to english
urls = {} # the url of last record
count = {} # the images num
area = {} # the region area
last_area = {'car':[0,0,0,0],'face':[0,0,0,0],'fire':[0,0,0,0],'pacer':[0,0,0,0]}
last_count = {'car':0,'face':0,'fire':0,'pacer':0}
last_urls = {'car':'','face':'' ,'fire':'' , 'pacer':'' }
@app.route('/', methods=['GET', 'POST'])
def home():
urls.clear()
count.clear()
db = DB.DB()
db.start()
sum = 0
for key in table_dict:
count[key] = db.query_count(key)
if int(count[key]) > 0:
last_count[key] = count[key]
sum += last_count[key]
last_count['total'] = sum
for key in table_dict:
urls[key] = db.query_last(key)
if urls[key] is not None:
last_urls[key] = urls[key]
loc = turn_ip_address(turn_url_ip(urls[key]))
if loc is not None:
area[key] = loc
last_area[key] = area[key]
db.commit()
return render_template('index.html' , count = last_count , urls = last_urls , areas = last_area)
# turn the ip into relevant address
def turn_ip_address(ip = None):
if ip == None:
return
try:
reader = geolite2.reader()
line = reader.get(ip)
if line == None:
return None
return (line['country']['names']['en'],line['continent']['names']['en'],line['location']['latitude'],line['location']['longitude'])
finally:
geolite2.close()
# turn the url into relevant ip
def turn_url_ip(url = None):
if url == None:
return
try:
# sparse the host name
parsed_url = U_R_L.parse_url(url)
host = parsed_url.hostname
ip = socket.gethostbyname(host)
return ip
except:
print("url: %s is wrong!"%(url))
@app.route('/data')
def dataFromAjax():
urls.clear()
count.clear()
db = DB.DB()
db.start()
sum = 0
for key in table_dict:
count[key] = db.query_count(key)
if int(count[key]) > 0:
last_count[key] = count[key]
sum += last_count[key]
last_count['total'] = sum
for key in table_dict:
urls[key] = db.query_last(key)
if urls[key] is not None:
last_urls[key] = urls[key]
loc = turn_ip_address(turn_url_ip(urls[key]))
if loc is not None:
area[key] = loc
last_area[key] = area[key]
db.commit()
data = {'count': last_count , 'url': last_urls , 'area':last_area}
return jsonify(data)
if __name__ == '__main__':
app.run()
#print(turn_ip_address(turn_url_ip('http://pic2.ooopic.com/11/98/31/31bOOOPIC12_1024.jpg')))