Lark AI Agent 🚀
This commit is contained in:
0
extension/__init__.py
Normal file
0
extension/__init__.py
Normal file
BIN
extension/weather_api/GEOIP.db
Normal file
BIN
extension/weather_api/GEOIP.db
Normal file
Binary file not shown.
74
extension/weather_api/api.py
Normal file
74
extension/weather_api/api.py
Normal file
@ -0,0 +1,74 @@
|
||||
from datetime import datetime
|
||||
|
||||
import requests
|
||||
|
||||
from .db import select_city_code, get_weather_code
|
||||
|
||||
|
||||
def get_city_geocode(city):
|
||||
"""
|
||||
Get the geocode of a city in a province.
|
||||
"""
|
||||
return select_city_code(city)
|
||||
|
||||
def get_city_weather(city):
|
||||
"""
|
||||
Get the weather of a city in a province.
|
||||
"""
|
||||
URL = f'https://weatherapi.market.xiaomi.com/wtr-v3/weather/all'
|
||||
city_code = select_city_code(city)
|
||||
|
||||
if city_code:
|
||||
result = f'地区:{city}的天气情况\n'
|
||||
date = datetime.today().strftime('%Y%m%d')
|
||||
params = {
|
||||
'latitude': 0,
|
||||
'longitude': 0,
|
||||
'locationKey': f'weathercn:{city_code}',
|
||||
'appKey': f'weather{date}',
|
||||
'sign': 'zUFJoAR2ZVrDy1vF3D07',
|
||||
'isGlobal': 'false',
|
||||
'locale': 'zh_CN',
|
||||
'days': '1'
|
||||
}
|
||||
response = requests.get(URL, params=params, verify=False)
|
||||
data = response.json()
|
||||
current = data['current']
|
||||
temperature = current['temperature']
|
||||
result += f'温度: {temperature["value"]}{temperature["unit"]}\n'
|
||||
weather_code = current['weather']
|
||||
weather = get_weather_code(weather_code)
|
||||
result += f'当前天气: {weather}\n'
|
||||
pressure = current['pressure']
|
||||
result += f'气压: {pressure["value"]}{pressure["unit"]}\n'
|
||||
humidity = current['humidity']
|
||||
result += f'湿度: {humidity["value"]}{humidity["unit"]}\n'
|
||||
visibility = current['visibility']
|
||||
if len(visibility['value']) > 0:
|
||||
result += f'能见度: {visibility["value"]}{visibility["unit"]}\n'
|
||||
wind = current['wind']
|
||||
result += f'风向: {wind["direction"]["value"]}{wind["direction"]["unit"]}\n'
|
||||
result += f'风力: {wind["speed"]["value"]}{wind["speed"]["unit"]}\n'
|
||||
forecastDaily = data['forecastDaily']
|
||||
aqi = forecastDaily['aqi']
|
||||
if aqi['status'] == 0:
|
||||
value = aqi['value']
|
||||
result += f'今天空气质量指数: {value[0]}\n'
|
||||
precipitationProbability = forecastDaily['precipitationProbability']
|
||||
if precipitationProbability['status'] == '0':
|
||||
value = precipitationProbability['value']
|
||||
result += f'\n今天降水概率: {value[0]}\n'
|
||||
result += f'未来几天降水概率: {value[1:]}\n'
|
||||
sunRiseSet = forecastDaily['sunRiseSet']
|
||||
if sunRiseSet['status'] == 0:
|
||||
value = sunRiseSet["value"]
|
||||
result += f'今日日出时间: {value[0]["from"]}\n'
|
||||
result += f'今日日落时间: {value[0]["to"]}\n'
|
||||
temperature = forecastDaily['temperature']
|
||||
if temperature['status'] ==0:
|
||||
value = temperature['value']
|
||||
result += f'今日气温:: {min(value[0]["from"], value[0]["to"])}-{max(value[0]["to"], value[1]["from"])}{temperature["unit"]}\n'
|
||||
return result
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(get_city_weather('北京'))
|
||||
178
extension/weather_api/db.py
Normal file
178
extension/weather_api/db.py
Normal file
@ -0,0 +1,178 @@
|
||||
import os
|
||||
import sqlite3
|
||||
|
||||
ROOT = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
DB_PATH = os.path.join(ROOT, 'GEOIP.db')
|
||||
|
||||
WEATHER_CODE = [
|
||||
{
|
||||
"code": 0,
|
||||
"wea": "晴"
|
||||
},
|
||||
{
|
||||
"code": 1,
|
||||
"wea": "多云"
|
||||
},
|
||||
{
|
||||
"code": 2,
|
||||
"wea": "阴"
|
||||
},
|
||||
{
|
||||
"code": 3,
|
||||
"wea": "阵雨"
|
||||
},
|
||||
{
|
||||
"code": 4,
|
||||
"wea": "雷阵雨"
|
||||
},
|
||||
{
|
||||
"code": 5,
|
||||
"wea": "雷阵雨并伴有冰雹"
|
||||
},
|
||||
{
|
||||
"code": 6,
|
||||
"wea": "雨夹雪"
|
||||
},
|
||||
{
|
||||
"code": 7,
|
||||
"wea": "小雨"
|
||||
},
|
||||
{
|
||||
"code": 8,
|
||||
"wea": "中雨"
|
||||
},
|
||||
{
|
||||
"code": 9,
|
||||
"wea": "大雨"
|
||||
},
|
||||
{
|
||||
"code": 10,
|
||||
"wea": "暴雨"
|
||||
},
|
||||
{
|
||||
"code": 11,
|
||||
"wea": "大暴雨"
|
||||
},
|
||||
{
|
||||
"code": 12,
|
||||
"wea": "特大暴雨"
|
||||
},
|
||||
{
|
||||
"code": 13,
|
||||
"wea": "阵雪"
|
||||
},
|
||||
{
|
||||
"code": 14,
|
||||
"wea": "小雪"
|
||||
},
|
||||
{
|
||||
"code": 15,
|
||||
"wea": "中雪"
|
||||
},
|
||||
{
|
||||
"code": 16,
|
||||
"wea": "大雪"
|
||||
},
|
||||
{
|
||||
"code": 17,
|
||||
"wea": "暴雪"
|
||||
},
|
||||
{
|
||||
"code": 18,
|
||||
"wea": "雾"
|
||||
},
|
||||
{
|
||||
"code": 19,
|
||||
"wea": "冻雨"
|
||||
},
|
||||
{
|
||||
"code": 20,
|
||||
"wea": "沙尘暴"
|
||||
},
|
||||
{
|
||||
"code": 21,
|
||||
"wea": "小雨-中雨"
|
||||
},
|
||||
{
|
||||
"code": 22,
|
||||
"wea": "中雨-大雨"
|
||||
},
|
||||
{
|
||||
"code": 23,
|
||||
"wea": "大雨-暴雨"
|
||||
},
|
||||
{
|
||||
"code": 24,
|
||||
"wea": "暴雨-大暴雨"
|
||||
},
|
||||
{
|
||||
"code": 25,
|
||||
"wea": "大暴雨-特大暴雨"
|
||||
},
|
||||
{
|
||||
"code": 26,
|
||||
"wea": "小雪-中雪"
|
||||
},
|
||||
{
|
||||
"code": 27,
|
||||
"wea": "中雪-大雪"
|
||||
},
|
||||
{
|
||||
"code": 28,
|
||||
"wea": "大雪-暴雪"
|
||||
},
|
||||
{
|
||||
"code": 29,
|
||||
"wea": "浮沉"
|
||||
},
|
||||
{
|
||||
"code": 30,
|
||||
"wea": "扬沙"
|
||||
},
|
||||
{
|
||||
"code": 31,
|
||||
"wea": "强沙尘暴"
|
||||
},
|
||||
{
|
||||
"code": 32,
|
||||
"wea": "飑"
|
||||
},
|
||||
{
|
||||
"code": 33,
|
||||
"wea": "龙卷风"
|
||||
},
|
||||
{
|
||||
"code": 34,
|
||||
"wea": "若高吹雪"
|
||||
},
|
||||
{
|
||||
"code": 35,
|
||||
"wea": "轻雾"
|
||||
},
|
||||
{
|
||||
"code": 53,
|
||||
"wea": "霾"
|
||||
},
|
||||
{
|
||||
"code": 99,
|
||||
"wea": "未知"
|
||||
}
|
||||
]
|
||||
|
||||
def get_weather_code(code):
|
||||
for item in WEATHER_CODE:
|
||||
if item['code'] == int(code):
|
||||
return item['wea']
|
||||
return '未知'
|
||||
|
||||
def select_city_code(city):
|
||||
city = city.replace('市','')
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
cursor = conn.cursor()
|
||||
result = cursor.execute("SELECT city_num FROM citys WHERE name like ?", (f'{city}',))
|
||||
city_num = result.fetchone()
|
||||
if city_num:
|
||||
return city_num[0]
|
||||
else:
|
||||
return None
|
||||
Reference in New Issue
Block a user