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

12 statements  

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

1""" 

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

3 

4Category: utility 

5Source: wttr.in 

6""" 

7 

8 

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

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

11 

12 Args: 

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

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

15 

16 Returns: 

17 天气信息字符串 

18 """ 

19 import urllib.parse 

20 import urllib.request 

21 

22 encoded = urllib.parse.quote(city) 

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

24 try: 

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

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

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

28 except Exception as e: 

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

30 

31 

32__all__ = ["run"]