-
Notifications
You must be signed in to change notification settings - Fork 0
/
getData.py
85 lines (80 loc) · 3.09 KB
/
getData.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
import requests, json
from operator import itemgetter
from geopy import distance
from datetime import datetime
import _secret
testing = 0 # set this to 0 if you want to request data from the internet instead from file (used to reduce api requests)
def cerca_prezzo(location, carburante, distanza_max):
if testing == 0:
json_data = {
"points": [
{
"lat": str(location[0]),
"lng": str(location[1]),
},
],
"fuelType": "1-1",
"priceOrder": "asc",
"radius": distanza_max,
}
# r = requests.post(url=_secret.URL, data=raw_data, headers=_secret.HEADERS)
r = requests.post(
_secret.URL,
headers=_secret.HEADERS,
json=json_data,
)
data = (
bytes(r.content.decode("utf-8"), "utf-8")
.decode("unicode_escape")
.replace("\\", "/")
)
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print(dt_string + " --> getData.py --> Requested data from internet")
else:
f = open("data copy.json", "r")
data = f.read()
f.close()
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print(dt_string + " --> getData.py --> Requested data from file")
data = json.loads(data)["results"]
validi = []
for benzinaio in data:
distanza = distance.distance(
[benzinaio["location"].get("lat"), benzinaio["location"].get("lng")],
location,
).km
if distanza <= distanza_max:
for benzina in benzinaio["fuels"]:
if int(benzina.get("fuelId")) == int(carburante[0]):
dati = {
"id": benzinaio["id"],
"distanza": distanza,
"nome": benzinaio["name"],
"prezzo": benzina["price"],
"luogo": benzinaio["address"],
"marca": benzinaio["brand"],
"coord": benzinaio["location"],
"dist": distanza,
}
validi.append(dati)
break
if carburante[0] == "1" or carburante[0] == "2":
if benzina.get("isSelf") == True and int(
benzina.get("fuelId")
) == int(carburante[0]):
dati = {
"id": benzinaio["id"],
"distanza": distanza,
"nome": benzinaio["name"],
"prezzo": benzina["price"],
"luogo": benzinaio["address"],
"marca": benzinaio["brand"],
"coord": benzinaio["location"],
"dist": distanza,
}
validi.append(dati)
break
validi = sorted(validi, key=itemgetter("prezzo"))
return validi