Coverage for agentos/marketplace/skills/weather/weather.py: 23%

13 statements  

« prev     ^ index     » next       coverage.py v7.14.3, created at 2026-07-06 08:01 +0800

1""" 

2weather — 全球天气查询(基于 wttr.in,免费无需 API Key)。 

3 

4Category: utility 

5Source: wttr.in 

6""" 

7 

8from typing import Any 

9 

10 

11def run(city: str = "Beijing", format_str: str = "3") -> str: 

12 """查询指定城市的天气。 

13 

14 Args: 

15 city: 城市名(支持中文如"北京"、英文如"London"、机场代码如"JFK") 

16 format_str: 格式 1-4,1=ANSI 终端版,2=纯文本,3=简洁版,4=JSON 

17 

18 Returns: 

19 天气信息字符串 

20 """ 

21 import urllib.request 

22 import urllib.parse 

23 

24 encoded = urllib.parse.quote(city) 

25 url = f"https://wttr.in/{encoded}?format={format_str}&lang=zh" 

26 try: 

27 req = urllib.request.Request(url, headers={"User-Agent": "curl/8.0"}) 

28 with urllib.request.urlopen(req, timeout=10) as resp: 

29 return resp.read().decode("utf-8") 

30 except Exception as e: 

31 return f"[weather] 查询失败: {e}" 

32 

33 

34__all__ = ["run"]