Skip to content

Instantly share code, notes, and snippets.

@iosx
Created November 23, 2021 03:56
Show Gist options
  • Save iosx/0f9cad663136838fc72f9ea44d720fea to your computer and use it in GitHub Desktop.
Save iosx/0f9cad663136838fc72f9ea44d720fea to your computer and use it in GitHub Desktop.

Revisions

  1. iosx created this gist Nov 23, 2021.
    72 changes: 72 additions & 0 deletions justmysocks-wxwork-notice.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    #!/usr/bin/python3
    # -*- coding:utf-8 -*-

    import json
    import requests
    from requests.packages import urllib3
    import math
    import time
    from corpwechatbot.chatbot import CorpWechatBot

    url = "https://justmysocks3.net/members/getbwcounter.php?service=XXXXXX&id=XXXXXXX-XXXXXXX"

    def pybyte(size, dot=2):
    size = float(size)
    # 位 比特 bit
    if 0 <= size < 1:
    human_size = str(round(size / 0.125, dot)) + 'b'
    # 字节 字节 Byte
    elif 1 <= size < 1024:
    human_size = str(round(size, dot)) + 'B'
    # 千字节 千字节 Kilo Byte
    elif math.pow(1024, 1) <= size < math.pow(1024, 2):
    human_size = str(round(size / math.pow(1024, 1), dot)) + 'KB'
    # 兆字节 兆 Mega Byte
    elif math.pow(1024, 2) <= size < math.pow(1024, 3):
    human_size = str(round(size / math.pow(1024, 2), dot)) + 'MB'
    # 吉字节 吉 Giga Byte
    elif math.pow(1024, 3) <= size < math.pow(1024, 4):
    human_size = str(round(size / math.pow(1024, 3), dot)) + 'GB'
    # 太字节 太 Tera Byte
    elif math.pow(1024, 4) <= size < math.pow(1024, 5):
    human_size = str(round(size / math.pow(1024, 4), dot)) + 'TB'
    # 拍字节 拍 Peta Byte
    elif math.pow(1024, 5) <= size < math.pow(1024, 6):
    human_size = str(round(size / math.pow(1024, 5), dot)) + 'PB'
    # 艾字节 艾 Exa Byte
    elif math.pow(1024, 6) <= size < math.pow(1024, 7):
    human_size = str(round(size / math.pow(1024, 6), dot)) + 'EB'
    # 泽它字节 泽 Zetta Byte
    elif math.pow(1024, 7) <= size < math.pow(1024, 8):
    human_size = str(round(size / math.pow(1024, 7), dot)) + 'ZB'
    # 尧它字节 尧 Yotta Byte
    elif math.pow(1024, 8) <= size < math.pow(1024, 9):
    human_size = str(round(size / math.pow(1024, 8), dot)) + 'YB'
    # 千亿亿亿字节 Bront Byte
    elif math.pow(1024, 9) <= size < math.pow(1024, 10):
    human_size = str(round(size / math.pow(1024, 9), dot)) + 'BB'
    # 百万亿亿亿字节 Dogga Byte
    elif math.pow(1024, 10) <= size < math.pow(1024, 11):
    human_size = str(round(size / math.pow(1024, 10), dot)) + 'NB'
    # 十亿亿亿亿字节 Dogga Byte
    elif math.pow(1024, 11) <= size < math.pow(1024, 12):
    human_size = str(round(size / math.pow(1024, 11), dot)) + 'DB'
    # 万亿亿亿亿字节 Corydon Byte
    elif math.pow(1024, 12) <= size:
    human_size = str(round(size / math.pow(1024, 12), dot)) + 'CB'
    # 负数
    else:
    raise ValueError('{}() takes number than or equal to 0, but less than 0 given.'.format(pybyte.__name__))
    return human_size

    proxies = { "http": None, "https": None}
    urllib3.disable_warnings()
    r = requests.get(url, verify=False, proxies=proxies)
    data = json.loads(json.dumps(r.json()))

    msg = "# 流量提醒 \n > 本月总量:**%s** \n > 已用流量:**%s** \n > 重置日期:**%s月%s日** \n" \
    % (pybyte(int(data['monthly_bw_limit_b'])), pybyte(int(data['bw_counter_b'])), time.strftime("%m", time.localtime()), data['bw_reset_day_of_month'])
    print(msg)

    bot = CorpWechatBot(key='YYYYYYYYYYY-YYYYYYYYYYYYY')
    bot.send_markdown(content=msg)