/
update_config_file.py
48 lines (40 loc) · 1.47 KB
/
update_config_file.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
import urllib2
import json
from ConfigParser import ConfigParser
url = "https://ipinfo.io/json"
config_file = "./config_file.cfg"
def parse_response(response_json):
del(response_json['ip'])
# del(response_json['hostname'])
response_json['City'] = response_json.pop('city')
response_json['State'] = response_json.pop('region')
response_json['Country'] = response_json.pop('country')
if response_json['Country'] == 'IN':
response_json['Country'] = 'India'
latitude, longitude = response_json['loc'].split(',')
response_json['Latitude'] = float(latitude)
response_json['Longitude'] = float(longitude)
del(response_json['loc'])
del(response_json['postal'])
del(response_json['org'])
response_json['UnitPreference'] = "METRIC"
return response_json
response = 0
try:
response = urllib2.urlopen(url)
response_json = json.loads(response.read())
except Exception as e:
print "Error : Error retieving location (Check Connection) , ", e
'''
Update the config file once the location is recieved.
'''
if response:
config = ConfigParser()
config.read(config_file)
requestinfo = parse_response(response_json)
for key in response_json.keys():
config.set('Location', key, response_json[key])
config.set('RequestInfo', 'requestinfo', json.dumps(requestinfo))
with open(config_file, 'w') as f:
config.write(f)
print "INFO : Location Updated."