简介

最近买了华为云的CDN加速,想要实时查看流量使用情况,但是又不想每次都去官网查看。

image

代码

import requests
import rumps
import json
import requests

global app

req_url = "https://console.huaweicloud.com/cdn/rest/rest/cbc/csbbillingfreeresourceservice/v1/freeresources?customerId=xxxxxxxx&cloudServiceTypes=hws.service.type.cdn&cloudServiceTypes=hws.service.type.wsa"
header = {
    "Cookie": "Your Cookie"
}


class StockApp(rumps.App):
    def __init__(self):
        super(StockApp, self).__init__(name="Stock")

    @rumps.timer(5)
    def updated(self, sender):
        flow_amount = 0
        flot_original_amount = 0
        request_amount = 0
        request_original_amount = 0
        rest = requests.get(req_url, headers=header)
        input_json = rest.text
        json_data = json.loads(input_json)
        for item in json_data['freeresUsageInfos']:
            product_name = item['productName']
            if '流量' in product_name:
                flow_amount += item['amount']
                flot_original_amount += item['originalAmont']
                pass
            elif '请求次数' in product_name:
                request_amount += item['amount']
                request_original_amount += item['originalAmont']
                pass
            else:
                print("error")
                exit(0)
        app.title = f"{round(flow_amount / flot_original_amount * 100, 4)}% {round(request_amount / request_original_amount * 100, 4)}%"


if __name__ == '__main__':
    app = StockApp()
    app.run()

效果

image-1689400059818