/
provider.py
82 lines (66 loc) · 2.07 KB
/
provider.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
import sys
import pandas as pandas
import http.client
import requests
import json
def unwrap(time):
if time[0] is "'":
time = time[1:len(time)]
if time[-1] is "'":
time = time[0:len(time) - 1]
return time
def get_coordinates(addr):
try:
response = requests.get('https://nominatim.openstreetmap.org/search/{}?format=json&limit=1'.format(addr))
response_json = response.json()[0]
lat = response_json['lat']
lon = response_json['lon']
return '{};{}'.format(lat, lon)
except Exception as e:
return ''
def set_requests_body(db):
out_list = []
for index, row in db.iterrows():
row["coordinates"] = get_coordinates(row["addres"])
event = {
"title": row["title"],
"coordinates": row["coordinates"],
"description": row["description"],
"isInfinite": row["isInfinite"],
"startTime": unwrap(row["startTime"]),
"endTime": unwrap(row["endTime"]),
"price": row["price"],
"peopleCount": row["peopleCount"]
}
if row["coordinates"]:
out_list.append(event)
body = {
"events": out_list
}
return body
def set_response(url, url_path, body):
try:
connection = http.client.HTTPConnection(url)
json_data = json.dumps(body)
headers = {'Content-type': 'application/json'}
connection.request('POST', url_path, json_data, headers)
response = connection.getresponse()
print("response: [", response.read().decode(), "]", sep="")
print("success")
except Exception as description:
print(description)
def main():
if len(sys.argv) < 3:
print("Error. Too few parameters.")
sys.exit(1)
if len(sys.argv) > 3:
print("Error. Too many parameters.")
sys.exit(1)
path = sys.argv[1]
url = sys.argv[2]
url_path = "/add_events"
events = pandas.read_excel(path)
body = set_requests_body(events)
set_response(url, url_path, body)
if __name__ == "__main__":
main()