'''
Author: Thund1r thund1r@foxmail.com
Date: 2022-09-22 14:28:08
LastEditTime: 2022-09-22 15:16:43
Description: å¤©æ°”æ•°æ�®

Copyright (c) 2022 by Thund1r thund1r@foxmail.com, All Rights Reserved. 
'''
# -*- coding: utf8 -*-
import requests
import config
import re

qweather = config.get("qweather")
wea_city_list = config.get_list("city")


# è�·å�–å’Œé£�å¤©æ°”æ•°æ�®
def get_weather(city_name):
    try:
        city_id = None
        weather_tip = None
        city = city_name.split("-")[0]
        county = city_name.split("-")[1]
        city_url = f"https://geoapi.qweather.com/v2/city/lookup?&adm={city}&key={qweather}&location={county}"
        city_json = requests.get(city_url).json()
        city_code = city_json["code"]
        if city_code.__eq__("200"):
            city_id = city_json["location"][0]["id"]
        else:
            print(
                f"æ²¡æœ‰æ‰¾åˆ°{city_name}è¿™ä¸ªåœ°æ–¹ï¼Œè¯·æ£€æŸ¥cityå€¼æ˜¯å�¦æ­£ç¡®ï¼Œæ ¼å¼�æ˜¯å�¦ä¸º çœ�/å¸‚-å¸‚/åŒº/å�¿ ï¼Œä¾‹å¦‚ æˆ�éƒ½-å�Œæµ�&&æ±Ÿè‹�-æ±Ÿå®�")
        if city_id:
            # è�·å�–é€�å¤©å¤©æ°”é¢„æŠ¥ï¼Œæœ‰å¾ˆå¤šå¤©æ°”ç±»ä¿¡æ�¯ï¼Œå�¯ä»¥æ ¹æ�®è‡ªå·±éœ€è¦�è¿›è¡Œè�·å�–å’Œæ‹¼æ�¥
            # å…·ä½“è¯·å�‚è€ƒå’Œé£�å¤©æ°”é€�å¤©å¤©æ°”é¢„æŠ¥å¼€å�‘æ–‡æ¡£https://dev.qweather.com/docs/api/weather/weather-daily-forecast/
            weather_url = f"https://devapi.qweather.com/v7/weather/3d?key={qweather}&location={city_id}"
            weather_json = requests.get(weather_url).json()
            weather_code = weather_json["code"]
            weather_list = []
            if weather_code.__eq__("200"):
                temp = weather_json["daily"][0]
                textDay = temp["textDay"]
                tempMin = temp["tempMin"]
                tempMax = temp["tempMax"]
                weather_icon = get_weather_icon(textDay)
                weather_tip = weather_icon+" "+county+textDay+"ï¼Œ"+tempMin+"~"+tempMax+"â„ƒ"
                weather_list.append(weather_tip)
            # è�·å�–ç©¿è¡£æŒ‡æ•°ã€‚ç”Ÿæ´»æŒ‡æ•°æœ‰å¾ˆå¤šä¿¡æ�¯ï¼Œå�¯ä»¥æ ¹æ�®è‡ªå·±éœ€è¦�è¿›è¡Œè�·å�–å’Œæ‹¼æ�¥
            # å…·ä½“è¯·å�‚è€ƒå’Œé£�å¤©æ°”ç”Ÿæ´»æŒ‡æ•°å¼€å�‘æ–‡æ¡£https://dev.qweather.com/docs/api/indices/
            life_url = f"https://devapi.qweather.com/v7/indices/1d?type=3&location={city_id}&key={qweather}"
            life_json = requests.get(life_url).json()
            life_code = life_json["code"]
            if life_code.__eq__("200"):
                life_tip = "ğŸ‘” " + life_json["daily"][0]["text"]
                weather_list.append(life_tip)
            # éœ€è¦�å’Œé£�å¤©æ°”å…¶ä»–æ�¥å�£çš„ä¿¡æ�¯è¯·å�‚è€ƒä»¥ä¸Šä»£ç �æ ¼å¼�è¿›è¡Œè�·å�–å’Œæ·»åŠ ï¼Œæ‰€æœ‰å¼€å�‘æ–‡æ¡£https://dev.qweather.com/docs/api/

            weather_tip = '\n'.join(weather_list)
        else:
            print(
                f"è�·å�–{city_name}IDå¤±è´¥ï¼Œè¯·æ£€æŸ¥qweatherã€�cityå€¼æ˜¯å�¦æ­£ç¡®ï¼Œcityæ ¼å¼�æ˜¯å�¦ä¸º çœ�/å¸‚-å¸‚/åŒº/å�¿ ï¼Œä¾‹å¦‚ å››å·�-æˆ�éƒ½&&æ±Ÿè‹�-æ±Ÿå®�")
        return weather_tip
    except Exception as e:
        print(f"è�·å�–{city_name}å’Œé£�å¤©æ°”æ•°æ�®é”™è¯¯:", e)
        return None


# è�·å�–å¤©æ°”icon
def get_weather_icon(text):
    weather_icon = "ğŸŒ¤ï¸�"
    weather_icon_list = ["â˜€ï¸�",  "â˜�ï¸�", "â›…ï¸�",
                         "â˜ƒï¸�", "â›ˆï¸�", "ğŸ�œï¸�", "ğŸ�œï¸�", "ğŸŒ«ï¸�", "ğŸŒ«ï¸�", "ğŸŒªï¸�", "ğŸŒ§ï¸�"]
    weather_type = ["æ™´", "é˜´", "äº‘", "é›ª", "é›·", "æ²™", "å°˜", "é›¾", "éœ¾", "é£�", "é›¨"]
    for index, item in enumerate(weather_type):
        if re.search(item, text):
            weather_icon = weather_icon_list[index]
            break
    return weather_icon


# è�·å�–æ‰€æœ‰å¤©æ°”æ•°æ�®
def get_map_weather():
    if qweather and wea_city_list:
        map_weather_tip = None
        weather_list = list(map(get_weather, wea_city_list))
        weather_list = list(filter(None, weather_list))
        if weather_list:
            map_weather_tip = "\n".join(weather_list)
        return map_weather_tip
    else:
        print("å’Œé£�å¤©æ°”ç§˜é’¥qweatheræˆ–åŸ�å¸‚cityé…�ç½®ç¼ºå¤±")
        return None
