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

12 statements  

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

1""" 

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

3 

4Category: utility 

5Source: wttr.in 

6""" 

7 

8 

9 

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

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

12 

13 Args: 

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

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

16 

17 Returns: 

18 天气信息字符串 

19 """ 

20 import urllib.request 

21 import urllib.parse 

22 

23 encoded = urllib.parse.quote(city) 

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

25 try: 

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

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

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

29 except Exception as e: 

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

31 

32 

33__all__ = ["run"]