'''
Author: Thund1r thund1r@foxmail.com
Date: 2022-09-22 14:16:12
LastEditTime: 2022-10-03 04:31:16
Description: APIæ•°æ�®

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


pictype_list = config.get_list("pictype")
tian = config.get("tian")


# è�·å�–å¤©è¡Œå½©è™¹å±�
def get_chp():
    if not tian:
        return None
    try:
        chp_url = f"http://api.tianapi.com/caihongpi/index?key={tian}"
        chp_res = requests.get(chp_url).json()
        return "ğŸŒˆ " + chp_res["newslist"][0]["content"]
    except Exception as e:
        print("è�·å�–å½©è™¹å±�æ•°æ�®é”™è¯¯ï¼Œè¯·æ£€æŸ¥æ˜¯å�¦æ­£ç¡®å¡«å†™å¤©è¡ŒKeyï¼Œæ˜¯å�¦ç”³è¯·å½©è™¹å±�æ�¥å�£:", e)
        return None


# è�·å�–éš�æœºå›¾ç‰‡é“¾æ�¥æ•°æ�®
# æ�¥è‡ªæ��å¤©API:https://api.btstu.cn/
def get_random_pic():
    p_type_list = pictype_list
    p_type = "fengjing"
    if p_type_list and isinstance(p_type_list, list):
        p_type = random.choice(p_type_list)
    try:
        pic_url = f"https://api.btstu.cn/sjbz/api.php?format=json&lx={p_type}"
        pic_res = requests.get(pic_url).json()["imgurl"]
        return pic_res
    except Exception as e:
        print("è�·å�–éš�æœºå›¾ç‰‡æ•°æ�®é”™è¯¯:", e)
        return None


# è�·å�–bingæ¯�æ—¥å£�çº¸æ•°æ�®
def get_bing():
    try:
        bing_url = "https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1"
        bing_res = requests.get(bing_url).json()
        bing_pic = "https://cn.bing.com/" + bing_res["images"][0]["url"]
        bing_title = bing_res["images"][0]["title"]
        bing_content = re.sub(
            "\\(.*?\\)", "", bing_res["images"][0]["copyright"])
        bing_tip = f"{bing_title}â€”â€”{bing_content}"
        return {
            "bing_pic": bing_pic,
            "bing_tip": bing_tip
        }
    except Exception as e:
        print("è�·å�–å¿…åº”æ•°æ�®é”™è¯¯:", e)
        return None


# è�·å�–é‡‘å±±è¯�éœ¸æ•°æ�®
def get_ciba():
    try:
        ciba_url = "http://open.iciba.com/dsapi/"
        ciba_res = requests.get(ciba_url).json()
        ciba_en = ciba_res["content"]
        ciba_zh = ciba_res["note"]
        ciba_pic = ciba_res["fenxiang_img"]
        ciba_tip = f"ğŸ”¤ {ciba_en}\nğŸ€„ï¸� {ciba_zh}"
        return {
            "ciba_tip": ciba_tip,
            "ciba_pic": ciba_pic
        }
    except Exception as e:
        print("è�·å�–é‡‘å±±è¯�éœ¸æ•°æ�®é”™è¯¯:", e)
        return None


# è�·å�–ONEä¸€ä¸ªå›¾æ–‡æ•°æ�®
def get_one():
    try:
        one_url = "https://apier.youngam.cn/essay/one"
        one_res = requests.get(one_url).json()['dataList'][0]
        one_id = "VOL."+one_res['id']
        one_pic = one_res['src']
        one_tip = f"âœ’ï¸� {one_id} {one_res['text']}"
        return {
            "one_pic": one_pic,
            "one_tip": one_tip
        }
    except Exception as e:
        print("è�·å�–ONEä¸€ä¸ªå›¾æ–‡æ•°æ�®é”™è¯¯:", e)
        return None


# # è�·å�–XXXè‡ªå®šä¹‰å›¾ç‰‡ä¸�æ–‡å­—
# def get_XXX():
#     try:
#         XXX_url = "https://XXXX.XXX"
#         XXX_res = requests.get(XXX_url).json()
#         print("è�·å�–XXXè‡ªå®šä¹‰å›¾ç‰‡ä¸�æ–‡å­—jsonæ•°æ�®:", XXX_res)
#         XXX_item0 = XXX_res["é”®å��"][n]["éœ€è¦�çš„æ•°æ�®é”®å��"]
#         XXX_item1 = XXX_res["é”®å��"][n]["éœ€è¦�çš„æ•°æ�®é”®å��"]
#         XXX_pic = XXX_res["é”®å��"][n]["éœ€è¦�çš„æ•°æ�®é”®å��"]
#         XXX_tip = "âœ’ï¸� " + XXX_item0 + "\n" + "ğŸ—“ï¸� " + XXX_item1
#         res = {
#             # æ²¡æœ‰å›¾ç‰‡å°±åˆ é™¤ä¸‹é�¢è¿™ä¸€å�¥
#             "XXX_pic": XXX_pic,
#             "XXX_tip": XXX_tip
#         }
#         print("è�·å�–XXXæ•°æ�®:", res)
#         return res
#     except Exception as e:
#         print("è�·å�–XXXæ•°æ�®é”™è¯¯:", e)
#         return None
