From 2397213709bf1bad85d44de27c73e4640354ec50 Mon Sep 17 00:00:00 2001 From: baby20162016 <2185823427@qq.com> Date: Sat, 24 Jan 2026 09:34:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=A4=A9=E6=B0=94=E6=8F=92?= =?UTF-8?q?=E4=BB=B6v1.0.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/weather.py | 240 +++++++++++++++++++++++++++++++ templates/weather.html | 312 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 552 insertions(+) create mode 100644 plugins/weather.py create mode 100644 templates/weather.html diff --git a/plugins/weather.py b/plugins/weather.py new file mode 100644 index 0000000..ec1ceea --- /dev/null +++ b/plugins/weather.py @@ -0,0 +1,240 @@ +# -*- coding: utf-8 -*- +import re +from datetime import datetime +from typing import Any, Dict, List + +import requests + +from core.managers.command_manager import matcher +from core.managers.image_manager import image_manager +from core.utils.logger import logger +from models import MessageEvent, MessageSegment + +# 插件元数据 +__plugin_meta__ = { + "name": "weather", + "description": "查询天气信息,支持中国天气网数据。", + "usage": "/天气 [城市代码] - 查询指定城市的天气信息\n例如:/天气 101190207 (南京)", +} + +# 城市代码映射(可以扩展) +CITY_CODES = { + "北京": "101010100", + "上海": "101020100", + "广州": "101280101", + "深圳": "101280601", + "南京": "101190101", + "苏州": "101190401", + "杭州": "101210101", + "武汉": "101200101", + "成都": "101270101", + "重庆": "101040100", + "西安": "101110101", + "天津": "101030100", + "沈阳": "101070101", + "大连": "101070201", + "青岛": "101120201", + "济南": "101120101", + "郑州": "101180101", + "长沙": "101250101", + "南昌": "101240101", + "合肥": "101220101", + "福州": "101230101", + "厦门": "101230201", + "南宁": "101300101", + "海口": "101310101", + "昆明": "101290101", + "贵阳": "101260101", + "拉萨": "101140101", + "兰州": "101160101", + "西宁": "101150101", + "银川": "101170101", + "乌鲁木齐": "101130101", + "哈尔滨": "101050101", + "长春": "101060101", + "呼和浩特": "101080101", + "太原": "101100101", + "石家庄": "101090101", +} + +HEADERS = { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" +} + + +def get_weather_data(city_code: str) -> Dict[str, Any]: + """ + 获取天气数据 + + Args: + city_code (str): 城市代码 + + Returns: + Dict[str, Any]: 包含城市信息和天气数据的字典 + """ + try: + url = f"https://www.weather.com.cn/weather/{city_code}.shtml" + response = requests.get(url, headers=HEADERS, timeout=10) + response.encoding = "utf-8" + html_content = response.text + + # 提取城市信息 + city_info = ( + html_content.split('")[0] + .strip() + ) + + city_parts = [] + city_parts.append(city_info.split("")[-1].split("")[0]) + + if city_info.count("_blank") == 1: + city_parts.append( + city_info.split(">")[-1] + .replace("", "") + .replace("", "") + .strip() + ) + else: + additional_parts = ( + city_info.split('target="_blank">')[-1] + .replace("> ", "") + .replace("", "") + .split("") + ) + city_parts.extend(additional_parts) + + city_name = " ".join([part for part in city_parts if part.strip()]) + + # 提取天气信息 + weather_data = [] + for i in range(7): + try: + weather_info = ( + html_content.split('