-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
55 lines (42 loc) · 1.34 KB
/
index.py
File metadata and controls
55 lines (42 loc) · 1.34 KB
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
import requests
import json
from flask import Flask, request
from geopy.geocoders import Nominatim
from timezonefinder import TimezoneFinder
geolocator = Nominatim(
user_agent="Anthem Time-API https://github.com/Anthem-Bot/time-api")
app = Flask(__name__)
config = open('config.json')
@app.route('/')
def main():
location = request.args.get('location')
if location is None:
return {"msg": "Please specify a location"}
result = getLocation(location)
time = getTime(result)
return {
"abbreviation": time['abbreviation'],
"time": time['datetime'],
"unixtime": time['unixtime'],
"timezone": time['timezone'],
"dst": {
"observes_dst": time['dst'],
"dst_from": time['dst_from'],
"dst_until": time['dst_until'],
"dst_offset": time['dst_offset']
},
"utc": {
"time": time['utc_datetime'],
"offset": time['utc_offset']
}
}
app.run('0.0.0.0', json.load(config)['port'])
def getLocation(location):
cords = geolocator.geocode(location)
obj = TimezoneFinder()
result = obj.timezone_at(lng=cords.longitude, lat=cords.latitude)
return result
def getTime(timezone):
res = requests.get(f"http://worldtimeapi.org/api/timezone/{timezone}")
data = res.text
return json.loads(data)