# -*- coding: utf-8 -*-

import datetime
import json
import re
import time
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from pprint import pprint
import json
import requests
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import pandas as pd

import smtplib
from email.mime.text import MIMEText
from email.header import Header

# EmailReceivers = ['kevin.xie@chinaentropy.com']  # 邮件收件人

EmailReceivers = ["xxxx@chinaentropy.com", 'xxxx@chinaentropy.com', "xxxx@chinaentropy.com",
                  "xxxx@chinaentropy.com"]  # 邮件收件人
localFileName = "1234.xlsx"


def send_email_to_d(text, receivers):
    # 第三方 SMTP 服务
    mail_host = "smtp.exmail.qq.com"  # 设置服务器
    mail_user = "xxxx@chinaentropy.com"  # 用户名
    mail_pass = "qqqqqqqqqqq"  # 口令

    sender = 'xxxx@chinaentropy.com'

    message = MIMEMultipart('mixed')  # 邮件内容,mixed为混合形式可传附件

    message['From'] = Header("xxxx@chinaentropy.com", 'utf-8')
    message['To'] = Header(";".join(EmailReceivers), 'utf-8')
    message['Subject'] = Header('和风API调用费用统计', 'utf-8')

    # print(text)  # 邮件文本内容编辑
    text_plain = MIMEText(text, 'plain', 'utf-8')
    message.attach(text_plain)  # 实例化文本

    docxpart = MIMEApplication(open(localFileName, 'rb').read())
    docxpart.add_header('Content-Disposition', 'attachment', filename=localFileName)
    message.attach(docxpart)  # 要发送的exl实例化

    try:
        smtpObj = smtplib.SMTP_SSL(mail_host, 465)
        smtpObj.login(mail_user, mail_pass)
        smtpObj.sendmail(sender, receivers, message.as_string())
        smtpObj.quit()
        print("邮件发送成功")

    except smtplib.SMTPException:
        print("Error: 无法发送邮件")


class scrapyler:
    def __init__(self):
        pass

    def getCookie(self):
        userName = "xxxx"
        userPassword = "xxxxx"
        caps = {
            'browserName': 'chrome',
            'version': '',
            'platform': 'ANY',
            'goog:loggingPrefs': {'performance': 'ALL'},  # 记录性能日志
            'goog:chromeOptions': {'extensions': [], 'args': ['--headless']}  # 无界面模式
        }

        # driver = webdriver.Chrome(executable_path='chromedriver.exe', desired_capabilities=caps)

        driver = webdriver.Chrome(executable_path='/home/pythonPackage/chromedriver', desired_capabilities=caps)

        driver.get('https://id.qweather.com/#/login?redirect=https%3A%2F%2Fconsole.qweather.com%2F#/expenses')

        browser = driver

        print("网页响应中。。。")
        wait = WebDriverWait(browser, 30)

        browser.find_element(by="xpath",
                             value=r"""//*[@id="app"]/div/div/div[2]/div/div[3]/div/div[3]/form/div[1]/div/div/input""").clear()
        browser.find_element(by="xpath",
                             value=r"""//*[@id="app"]/div/div/div[2]/div/div[3]/div/div[3]/form/div[1]/div/div/input""").send_keys(
            userName)
        browser.find_element(by="xpath",
                             value=r"""//*[@id="app"]/div/div/div[2]/div/div[3]/div/div[3]/form/div[2]/div/div/input""").clear()
        browser.find_element(by="xpath",
                             value=r"""//*[@id="app"]/div/div/div[2]/div/div[3]/div/div[3]/form/div[2]/div/div/input""").send_keys(
            userPassword)

        browser.find_element(by="xpath",
                             value=r"""//*[@id="app"]/div/div/div[2]/div/div[3]/div/div[5]/button""").click()

        time.sleep(5)

        balance = float(browser.find_element(by="xpath",
                                             value=r"""//*[@id="page"]/section/main/div/div/div[2]/div[1]/div/div[3]/div[1]/div/span[2]""").text)

        logs = driver.get_log("performance")

        cookies = None
        for i in logs:
            if re.search(r'''"cookie":"[\s\S]{10,200}__HE_CORE__=[\s\S]{10,200}JSESSIONID=[0-9A-Za-z]{10,200}"''',
                         str(i)) is not None:
                cookies = re.search(
                    r'''"cookie":"[\s\S]{10,200}__HE_CORE__=[\s\S]{10,200}JSESSIONID=[0-9A-Za-z]{10,200}"''',
                    str(i)).group()
                break

        assert cookies is not None
        cookie = cookies[10:-1]

        browser.quit()
        # browser.close()
        return cookie, balance

    def getAppBinding(self, cookie):
        prog = requests.get(headers={"cookie": cookie}, url="https://console.qweather.com/v1/rest/app").json()["data"]
        idname = {lt["name"]: lt["id"] for lt in prog}
        print(idname)
        return idname

    def getPurchaseHistory(self, cookie, appId: str,
                           yearmonth: str):  # "https://console.qweather.com/v1/rest/fee/app/95491/type/2?date=2021-11"
        url = f"https://console.qweather.com/v1/rest/fee/app/{appId}/type/2?date={yearmonth}"
        prog = requests.get(headers={"cookie": cookie}, url=url).json()["data"]
        print(prog)
        return prog

    def run(self):
        year = datetime.datetime.now().year - 1 if datetime.datetime.now().month - 1 == 0 else datetime.datetime.now().year
        month = 12 if datetime.datetime.now().month - 1 == 0 else datetime.datetime.now().month - 1
        yearmonthstr = str(year) + "-" + str(month).zfill(2)
        cookie, balance = self.getCookie()
        time.sleep(0.5)
        idname = self.getAppBinding(cookie)
        time.sleep(0.5)

        resultdf = []
        need = ["森林消防", "二块板天气数据库专用", "环保生产"]
        for nd in need:
            prog = self.getPurchaseHistory(cookie, idname[nd], yearmonthstr)
            resultdf.append(pd.DataFrame(prog))

        df = pd.concat(resultdf)
        df["cost"] = df["cost"].apply(float)

        totalMonney = df["cost"].sum()

        df.to_excel(localFileName)

        return totalMonney, need, balance


if __name__ == '__main__':
    # 爬虫获取 总金额,行业名列表,本地文件“和风API调用费用明细.xlsx”
    scrapyler = scrapyler()
    totalMonney, need, balance = scrapyler.run()

    # 发邮件
    if datetime.datetime.now().day == 6 or balance <= 5:
        year = datetime.datetime.now().year - 1 if datetime.datetime.now().month - 1 == 0 else datetime.datetime.now().year
        month = 12 if datetime.datetime.now().month - 1 == 0 else datetime.datetime.now().month - 1
        yearmonthstr = str(year) + "-" + str(month).zfill(2)
        send_email_to_d(f"和风API调用当前余额还剩余  {balance}  元。\n"
                        f"\n"
                        f"\n"
                        f"和风API调用费用统计见附件,{yearmonthstr}月份总消费金额  {totalMonney}   元。\n"
                        f"此统计只包含这些项目行业{str(need)}。\n"
                        f"\n"
                        f"\n"
                        f"--------------------------------------------------------------------------"
                        f"\n"
                        f"此邮件是跑在    http://10.10.90.54:8888/dolphinscheduler/ui/#/resource/file/edit/280     的定时脚本。\n"
                        f"每月6号发一次邮件。每天检测一次余额,余额低于5元也会发本邮件。"
                        f"和风api登录地址: https://id.qweather.com/#/login?redirect=https%3A%2F%2Fconsole.qweather.com%2F#/expenses"
                        f"\n"
                        f"  "
                        ,
                        EmailReceivers)

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐