()
| 7 | # assumes the city name is unique |
| 8 | @lru_cache(maxsize=None) |
| 9 | def get_city2code(): |
| 10 | city2code = {} |
| 11 | |
| 12 | res = httpx.get('http://nmc.cn/rest/province') |
| 13 | provinces = json.loads(res.text) |
| 14 | for prov in provinces: |
| 15 | url = f'http://nmc.cn/rest/province/{prov["code"]}' |
| 16 | res = httpx.get(url) |
| 17 | cities = json.loads(res.text) |
| 18 | for c in cities: |
| 19 | city2code[c['city']] = c['code'] |
| 20 | |
| 21 | return city2code |
| 22 | |
| 23 | |
| 24 | class Weather(HttpTool): |