Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
__pycache__/
*.pyc
*.pyo
response.html
debug_results.json
47 changes: 31 additions & 16 deletions GetWeather.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,23 +202,30 @@ def weather_alarm(alarm_list):
+ id["w11"]
)

def main_weather_process(output=0):
def main_weather_process(output=0, city_name=None):
try:
address, code = get_CityName()
if len(address) == 0:
address = input(" [?] 请手动输入所在地(例:广州)[输入为空即退出]:")
if address == "":
print(" [#] 退出脚本")
if city_name:
if CheckInput(city_name):
print(" [!]检测非地名字符,退出脚本")
sys.exit(1)
else:
if CheckInput(address):
print(" [!]检测非地名字符,退出脚本")
print(" [+] 使用指定城市:" + city_name)
code = get_city_code(city_name)
else:
address, code = get_CityName()
if len(address) == 0:
address = input(" [?] 请手动输入所在地(例:广州)[输入为空即退出]:")
if address == "":
print(" [#] 退出脚本")
sys.exit(1)
else:
print(" [+] 使用手动输入定位位置:"+address)
code = get_city_code(address)
else:
print(" [+] 自动定位位置:"+address)
if CheckInput(address):
print(" [!]检测非地名字符,退出脚本")
sys.exit(1)
else:
print(" [+] 使用手动输入定位位置:"+address)
code = get_city_code(address)
else:
print(" [+] 自动定位位置:"+address)

try:
weather_text = get_weather(code)
Expand Down Expand Up @@ -274,13 +281,21 @@ def debug_mode(city):
# 改动 2:支持命令行参数解析
parser = argparse.ArgumentParser(description="Weather Script with Debug Mode")
parser.add_argument("--debug", action="store_true", help="启用 Debug 模式,仅检查状态码")
parser.add_argument("--city", type=str, default="101280601", help="城市代码 (默认: 广州)")
parser.add_argument("--city", type=str, default=None, help="城市名称 (例:北京)")
parser.add_argument("--output", type=int, default=0, help="输出模式,0为shell输出,1为窗口输出(窗口仅输出天气信息)")
args = parser.parse_args()

# 改动 3:根据参数选择运行模式
if args.debug:
debug_mode(args.city)
if args.city:
try:
city_code = get_city_code(args.city)
except SystemExit:
print(f" [!] Debug 模式:未能找到城市 '{args.city}' 的城市代码,退出")
sys.exit(1)
Comment on lines +294 to +295
else:
city_code = "101280601"
debug_mode(city_code)
else:
output = args.output
main_weather_process(output)
main_weather_process(output, city_name=args.city)
77 changes: 77 additions & 0 deletions WeatherSkill.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/python
# _*_coding: utf-8 _*_
# WeatherSkill: 天气查询 Skill,基于 GetWeather.py

import sys
import argparse

from GetWeather import (
get_CityName,
get_city_code,
get_weather,
CheckInput,
)


def query_weather(city_name: str) -> str:
"""
查询指定城市的天气信息,返回天气文本。

参数:
city_name (str): 城市名称,例如 "北京"、"上海"

返回:
str: 格式化的天气信息文本;若查询失败则返回错误提示字符串。
"""
if not city_name or not city_name.strip():
return " [!] 城市名称不能为空"
city_name = city_name.strip()
if CheckInput(city_name):
return " [!] 检测到非地名字符(不能包含数字、纯空格或英文字母),请输入有效的城市名称(例:北京)"
try:
code = get_city_code(city_name)
weather_text = get_weather(code)
return weather_text
except SystemExit:
return f" [!] 未能找到城市 '{city_name}' 的天气信息"
Comment on lines +31 to +36
except Exception as e:
return f" [!] 查询天气时出错: {e}"


def query_weather_auto() -> str:
"""
自动定位并查询当前所在城市的天气信息,返回天气文本。

返回:
str: 格式化的天气信息文本;若定位或查询失败则返回错误提示字符串。
"""
try:
address, code = get_CityName()
if not address:
return " [!] 无法自动定位城市,请使用 query_weather(city_name) 指定城市"
weather_text = get_weather(code)
return weather_text
except SystemExit:
return " [!] 未能获取当前位置的天气信息"
Comment on lines +48 to +55
except Exception as e:
return f" [!] 查询天气时出错: {e}"


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="天气查询 Skill — 通过城市名称查询实时天气"
)
parser.add_argument(
"--city",
type=str,
default=None,
help="城市名称 (例:北京);不指定时自动定位",
)
args = parser.parse_args()

if args.city:
result = query_weather(args.city)
else:
result = query_weather_auto()

print("\n" + result + "\n")
Loading