-
Notifications
You must be signed in to change notification settings - Fork 1
/
weather.py
38 lines (32 loc) · 1.01 KB
/
weather.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
import json
import urllib
import os
import re
import requests
dskey = os.environ["DARKSKYKEY"]
lociq = os.environ["LOCIQ"]
locurl = "http://locationiq.org/v1/search.php?key="
locvar = "&limit=1&countrycodes=US&format=json&q="
dskyurl = "https://api.darksky.net/forecast/"
def encoding(text):
print "debug encoding"
json = weather(text).encode('utf-8')
return json
def just_coord(text):
text = re.findall(r'"([^"]*)"', text)
text = str(text)
text = text.translate(None, " ['] ")
r = requests.get(locurl + lociq + locvar + text)
parsed_json = json.loads(r.text)
for i in parsed_json:
return i['lat'] + ',' + i['lon']
def weather(text):
try:
coord = str(just_coord(text))
coord = coord.translate(None, ' u()')
url = dskyurl + dskey + "/" + coord
response = urllib.urlopen(url)
parsed_json = json.loads(response.read())
return (parsed_json['daily']['summary'])
except ValueError:
return 'Please use format: "City, St"'