-
Notifications
You must be signed in to change notification settings - Fork 0
/
forecast.py
74 lines (70 loc) · 2.93 KB
/
forecast.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from sopel import web
from sopel.module import commands, example, NOLIMIT
import xmltodict
import urllib.request, urllib.parse, urllib.error
import urllib.request, urllib.error, urllib.parse
import json
@commands('forecast', 'fc')
@example('.forecast London')
def forecast(bot,trigger):
location=trigger.group(2)
woeid = ''
result={}
if not location:
return bot.msg(trigger.sender, "I don't know where you live. " +
'Give me a location, like .forecast London, or tell me where you live by saying .setlocation London, for example.')
else:
location = location.strip()
result=forecastsearch(location)
if not result:
return bot.reply("I don't know where that is.")
data = result
bot.say(data['channel']['title']+" "+data['channel']['item']['pubDate'])
for dat in data['channel']['item']['forecast'][:5]:
bot.say(dat['day']+" "+dat['date']+" "+dat['text']+" "+dat['low']+"/"+dat['high']+"C")
def forecastsearch(query):
baseurl = "https://query.yahooapis.com/v1/public/yql?"
yql_query = 'select * from geo.places where text="'+query+'"'
yql_url = baseurl + urllib.parse.urlencode({'q':yql_query}) + "&format=json"
print(yql_url)
result = urllib.request.urlopen(yql_url).read()
data = json.loads(result.decode())
#print(data['query']['results'])
if not 'results' in data['query']:
return
places=data['query']['results']['place']
for place in places:
#print(place)
if'woeid'==place or 'woeid' in place:
if 'woeid'==place:
woid=places['woeid']
else:
woid=place['woeid']
#print(place['woeid'])
baseurl="https://query.yahooapis.com/v1/public/yql?"
yql_query="select * from weather.forecast where u='c' and woeid="+woid
yql_url=baseurl+urllib.parse.urlencode({'q':yql_query})+"&format=json"
print(yql_url)
result=urllib.request.urlopen(yql_url).read()
data=json.loads(result.decode())
print()
if 'results' in data['query'] and data['query']['results']!=None:
print(data['query']['results'])
return data['query']['results']
# if not 'woeid' in data['query']['results']['place']:
# woid=data['query']['results']['place'][0]['woeid']
# else:
# woid=data['query']['results']['place']['woeid']
#if not woid:
# return
# print(woid)
# baseurl = "https://query.yahooapis.com/v1/public/yql?"
# yql_query = "select * from weather.forecast where u='c' and woeid="+woid
# yql_url = baseurl + urllib.parse.urlencode({'q':yql_query}) + "&format=json"
# print(yql_url)
# result = urllib.request.urlopen(yql_url).read()
# data = json.loads(result.decode())
# print(data)
# return data['query']['results']
return
#forecastsearch("Hong Kong")