-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
105 lines (83 loc) · 2.71 KB
/
main.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
import mysql.connector
from mysql.connector import errorcode
import urllib2, cookielib, json
import time
import datetime
lat = 46.066974
lon = 11.150047
ray = 30
delay = 60
config = {
'user': 'flight',
'password': 'hof5gisAbuqE',
'host': 'localhost',
'database': 'flight',
'raise_on_warnings': True
}
try:
cnx = mysql.connector.connect(**config)
cursor = cnx.cursor()
#query = "SELECT * FROM test"
#cursor.execute(query)
try:
counter = 1
while(True):
site = "https://public-api.adsbexchange.com/VirtualRadar/AircraftList.json?lat=" + str(lat) + "&lng=" + str(lon) + "&fDstL=0&fDstU=" + str(ray)
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive'}
req = urllib2.Request(site, headers=hdr)
page = urllib2.urlopen(req)
content = page.read()
content = content.replace("true", "True")
content = content.replace("false", "False")
tmp = eval(content)
x = []
ts = time.time()
timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
nplane = len(tmp["acList"])
if(nplane > 0):
for fn in tmp["acList"]:
keyforflight = fn.keys()
for mykey in keyforflight:
valido = True
for coppia in x:
if(coppia[0] == mykey):
valido = False
coppia[1] = coppia[1] + 1
if(valido):
x.append([mykey, 1])
val = []
for [a, b] in x:
val.append((a, b, timestamp, nplane, lat, lon, ray))
sql = "INSERT INTO fieldstat (Name, Value, TS, NPlane, Lat, Lon, Ray) VALUES (%s, %s, %s, %s, %s, %s, %s)"
for x in val:
cursor.execute(sql, x)
cnx.commit()
else:
sql = "INSERT INTO fieldstat (Name, Value, TS, NPlane, Lat, Lon, Ray) VALUES (%s, %s, %s, %s, %s, %s, %s)"
cursor.execute(sql, ("Zero", 0, timestamp, 0, lat, lon, ray))
cnx.commit()
print str(counter) + ") - Fatto"
counter = counter + 1
time.sleep(delay)
except urllib2.HTTPError, e:
print e.fp.read()
#print("Fatto")
#result = cursor.fetchall()
#for i in result:
# print i
#cursor.close()
#cnx.close()
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
else:
cnx.close()