-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeo_app.py
48 lines (40 loc) · 1.49 KB
/
geo_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
#!/usr/bin/env python3
'''handle database for api server'''
import os
import json
import sqlite3
from urllib.parse import unquote
GEO_DATA = os.path.abspath('src/in_geo1.json')
GEO_DB = os.path.abspath('src/geo_data.db')
def load_database():
'''load indian cities pincodes and GEO coords'''
with open(GEO_DATA, "r") as geo_file:
geo_dict = json.load(geo_file)
return geo_dict
GEO_DICT = load_database()
# def create_response_body(req_query):
# '''returns response body'''
# print(req_query)
# query = unquote(req_query.title())
# if not query:
# return "check the entered station code"
# fetched = GEO_DICT.get(query, False) or "NA"
# print(json.dumps({query:fetched}))
# return json.dumps({query:fetched})
def create_connection():
'''return sqlite3 db cursor obj '''
con = sqlite3.connect(GEO_DB)
cur = con.cursor()
return cur
CUR = create_connection()
def create_response_body(req_query):
'''returns query related response JS0N'''
CUR.execute("SELECT * FROM geo WHERE city LIKE '%{}%'".format(unquote(req_query)))
response_list = CUR.fetchall()
response_dict = {i[0]:{"city": i[0], "state": i[1], "pincode": i[2],
"latitude": i[3], "longitude": i[4]} for i in response_list}
return json.dumps(response_dict)
# with open("{}.json".format(req_query), "w+") as res:
# json.dump(response_dict, res)
# res_json = os.path.abspath("{}.json".format(req_query))
# return res_json