Skip to content

Commit

Permalink
update weather request
Browse files Browse the repository at this point in the history
  • Loading branch information
yuguanggao committed Jan 21, 2021
1 parent 4ce4ca5 commit 6fd6f2f
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 702 deletions.
Binary file modified .DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# A simple example of Chinese chatbot using RASA framework (developing...)
# A simple Chinese chatbot with weather request functionality using RASA framework

## Getting Started
Command line demo:
![](asset/example.png)
![](asset/example_1)

## Usage

Expand Down
Binary file added actions/.DS_Store
Binary file not shown.
30 changes: 22 additions & 8 deletions actions/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from rasa_sdk.forms import FormAction

from actions import third_chat
from actions.third_weather import get_weather_by_day
# from actions.third_weather import get_weather_by_day
from actions.qweather import get_weather_by_day
from requests import (
ConnectionError,
HTTPError,
Expand Down Expand Up @@ -105,17 +106,30 @@ def get_text_weather_date(address, date_time, date_time_number):
except (ConnectionError, HTTPError, TooManyRedirects, Timeout) as e:
text_message = "{}".format(e)
else:
# text_message_tpl = """
# {} {} ({}) 的天气情况为:白天:{};夜晚:{};气温:{}-{} °C
# """
# text_message = text_message_tpl.format(
# result['location']['name'],
# date_time,
# result['result']['date'],
# result['result']['text_day'],
# result['result']['text_night'],
# result['result']["high"],
# result['result']["low"],
# )
text_message_tpl = """
{} {} ({}) 的天气情况为:白天:{};夜晚:{};气温:{}-{} °C
我掐指一算,{} {} ({}) 的天气情况为:白天:{};夜晚:{};气温:{}-{} °C;湿度:{}
"""
text_message = text_message_tpl.format(
result['location']['name'],
result['location'],
date_time,
result['result']['date'],
result['result']['text_day'],
result['result']['text_night'],
result['result']["high"],
result['result']["low"],
result['fxDate'],
result['textDay'],
result['textNight'],
result["tempMin"],
result["tempMax"],
result["humidity"]
)

return text_message
Expand Down
51 changes: 51 additions & 0 deletions actions/qweather.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-
"""
qweather.py
~~~~~~~~~
使用和风天气数据查询天气
"""

import requests
import json

KEY = '882e721338b4441dbf1b7e514029aaa8' # API key(私钥)
UID = "" # 用户ID, @todo: 当前并没有使用这个值,签名验证方式将使用到这个值

# LOCATION = 'beijing' # 所查询的位置,可以使用城市拼音、v3 ID、经纬度等
API_loc_id = 'https://geoapi.qweather.com/v2/city/lookup?' # 城市id查询
API = 'https://devapi.qweather.com/v7/weather/3d?' # API URL,可替换为其他 URL
# UNIT = 'c' # 单位
# LANGUAGE = 'zh-Hans' # 查询结果的返回语言

def fetch_location_id(location):
result = requests.get(API_loc_id, params={
'key': KEY,
'location': location,
})
return result.json()

def fetch_weather(location_id):
result = requests.get(API, params={
'key': KEY,
'location': location_id,
})
return result.json()

def get_weather_by_day(location, day=1):
location_id = fetch_location_id(location)["location"][0]["id"]
result = fetch_weather(location_id)
normal_result = {
"location": location,
"fxDate": result["daily"][day]["fxDate"],
"tempMax": result["daily"][day]["tempMax"],
"tempMin": result["daily"][day]["tempMin"],
"textDay": result["daily"][day]["textDay"],
"textNight": result["daily"][day]["textNight"],
"humidity": result["daily"][day]["humidity"]
}

return normal_result

if __name__ == "__main__":
print(get_weather_by_day('上海', 1))
print(json.dumps(get_weather_by_day('上海', 1), ensure_ascii=False))
Binary file added asset/.DS_Store
Binary file not shown.
Binary file added asset/example_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified data/.DS_Store
Binary file not shown.
Binary file added data/nlu/.DS_Store
Binary file not shown.
163 changes: 0 additions & 163 deletions data/nlu/number.yml

This file was deleted.

Binary file added data/story/.DS_Store
Binary file not shown.
Loading

0 comments on commit 6fd6f2f

Please sign in to comment.