-
Notifications
You must be signed in to change notification settings - Fork 6
/
geonames_api.py
48 lines (39 loc) · 1.54 KB
/
geonames_api.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 python
# -*- coding:utf-8 -*-
import requests
import re
from datetime import datetime
FIND_URL = "http://api.geonames.org/searchJSON?q={place}&maxRows=1&username=gcibot"
TIME_URL = "http://api.geonames.org/timezoneJSON?formatted=true&lat={lat}&lng={lng}&username=gcibot&style=full"
WIKIPEDIA_URL = "http://api.geonames.org/wikipediaSearchJSON?formatted=true&q={word}&maxRows=1&username=gcibot&style=full"
def get_date_time(place):
place_info = requests.get(FIND_URL.format(place=place)).json()
if not place_info["totalResultsCount"]:
msg = "%s - city not found <--" % (place)
return msg
data = place_info["geonames"][0]
lat = data["lat"]
lng = data["lng"]
timezone_info = requests.get(TIME_URL.format(lat=lat, lng=lng)).json()
time = datetime.strptime(timezone_info["time"], "%Y-%m-%d %H:%M")
city = data["name"]
both = city
try:
country = data["countryName"]
both = "%s, %s" % (city, country)
if city == country:
both = country
except BaseException:
pass
msg = "%s - %s %s" % (both, time.strftime(
"%H:%M"), time.strftime("(%d/%m/%y)"))
return msg
def city(city):
wikipedia_result = requests.get(WIKIPEDIA_URL.format(word=city)).json()
if not len(wikipedia_result["geonames"]):
msg = "--> %s <-> city not found <--" % (city)
return msg
result = wikipedia_result["geonames"][0]
result = "--> %s <-> %s -- http://%s" % (
result["title"], result["summary"], result["wikipediaUrl"])
return result