这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion modules/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
'help',
'joke',
'movie',
'request'
'request',
'weather'
]
20 changes: 20 additions & 0 deletions modules/src/weather.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import re
import config
import os
import requests

OPEN_WEATHER_MAP_ACCESS_TOKEN = os.environ.get('OPEN_WEATHER_MAP_ACCESS_TOKEN', config.OPEN_WEATHER_MAP_ACCESS_TOKEN)
def process(input, entities=None):
output = {}

try:
location = entities['location'][0]['value']
r = requests.get('http://api.openweathermap.org/data/2.5/weather?q=' + location + '&appid=' + OPEN_WEATHER_MAP_ACCESS_TOKEN)
data = r.json()
output['input'] = input
output['output'] = 'Current weather: ' + data['main']['temp'] + data['weather'][0]['description'] + \
" information provided by OpenWeatherMap"
output['success'] = True
except:
output['success'] = False
return output
7 changes: 7 additions & 0 deletions modules/tests/test_weather.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import modules

def test_weather():
assert('weather' == modules.process_query('tell me the weather in London')[0])
assert('weather' == modules.process_query('weather London')[0])
assert('weather' == modules.process_query('whats the weather in London')[0])
assert('weather' != modules.process_query('something random')[0])