| Conditions | 9 |
| Total Lines | 69 |
| Code Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | # -*- coding: utf-8 -*- |
||
| 20 | def main(self): |
||
| 21 | """ |
||
| 22 | 获取天气信息。网址:https://www.sojson.com/blog/305.html |
||
| 23 | :return: |
||
| 24 | """ |
||
| 25 | try: |
||
| 26 | with open( |
||
| 27 | os.path.join(os.path.dirname(__file__), "city.json"), |
||
| 28 | "r", |
||
| 29 | encoding="utf-8", |
||
| 30 | ) as city_file: |
||
| 31 | city_map = json.loads(city_file.read()) |
||
| 32 | if not city_map: |
||
| 33 | raise FileNotFoundError |
||
| 34 | except FileNotFoundError: |
||
| 35 | resp = requests.get("https://fastly.jsdelivr.net/gh/Oreomeow/checkinpanel@master/city.json") |
||
| 36 | if resp.status_code == 200: |
||
| 37 | city_map = resp.json() |
||
| 38 | with open( |
||
| 39 | os.path.join(os.path.dirname(__file__), "city.json"), |
||
| 40 | "w", |
||
| 41 | encoding="utf-8", |
||
| 42 | ) as city_file: |
||
| 43 | json.dump(city_map, city_file,ensure_ascii=False) |
||
| 44 | else: |
||
| 45 | return "下载 city.json 失败!" |
||
| 46 | msg_all = "" |
||
| 47 | for city_name in self.check_items: |
||
| 48 | city_code = city_map.get(city_name, "101020100") |
||
| 49 | weather_url = f"http://t.weather.itboy.net/api/weather/city/{city_code}" |
||
| 50 | resp = requests.get(url=weather_url) |
||
| 51 | if resp.status_code == 200 and resp.json().get("status") == 200: |
||
| 52 | d = resp.json() |
||
| 53 | msg = ( |
||
| 54 | "\n城市:" |
||
| 55 | + d["cityInfo"]["parent"] |
||
| 56 | + " " |
||
| 57 | + d["cityInfo"]["city"] |
||
| 58 | + "\n日期:" |
||
| 59 | + d["data"]["forecast"][0]["ymd"] |
||
| 60 | + " " |
||
| 61 | + d["data"]["forecast"][0]["week"] |
||
| 62 | + "\n天气:" |
||
| 63 | + d["data"]["forecast"][0]["type"] |
||
| 64 | + "\n温度:" |
||
| 65 | + d["data"]["forecast"][0]["high"] |
||
| 66 | + " " |
||
| 67 | + d["data"]["forecast"][0]["low"] |
||
| 68 | + "\n湿度:" |
||
| 69 | + d["data"]["shidu"] |
||
| 70 | + "\n空气质量:" |
||
| 71 | + d["data"]["quality"] |
||
| 72 | + "\nPM2.5:" |
||
| 73 | + str(d["data"]["pm25"]) |
||
| 74 | + "\nPM10:" |
||
| 75 | + str(d["data"]["pm10"]) |
||
| 76 | + "\n风力风向:" |
||
| 77 | + d["data"]["forecast"][0]["fx"] |
||
| 78 | + " " |
||
| 79 | + d["data"]["forecast"][0]["fl"] |
||
| 80 | + "\n感冒指数:" |
||
| 81 | + d["data"]["ganmao"] |
||
| 82 | + "\n温馨提示:" |
||
| 83 | + d["data"]["forecast"][0]["notice"] |
||
| 84 | + "\n更新时间:" |
||
| 85 | + d["time"] |
||
| 86 | ) |
||
| 87 | msg_all += msg + "\n\n" |
||
| 88 | return msg_all |
||
| 89 | |||
| 96 |