简单的 OpenBMCLAPI 节点流量统计
转载前请务必事先联系并取得本人授权,同时需保留原文链接及明确的转载声明。未经许可,不得擅自用于商业用途。
Copyright © 2024-2025 SALTWOOD. All rights reserved.

最近疯开了几个节点,但是一个一个查询流量很麻烦,所以索性写了个简单的玩意统计用户流量。

先把代码贴在这里:

import requests
import math

def to_readable_hits(hits):
units = ['次', '万次', '亿次', '兆次']
pows = math.floor(math.log(hits, 10000))
unit = units[pows]
return f"{hits / (10000 ** pows):.2f}{unit}"

def to_readable_bytes(bytes):
units = ['B', 'KB', 'MB', 'GB', 'TB']
pows = math.floor(math.log(bytes, 1024))
unit = units[pows]
return f"{bytes / (1024 ** pows):.2f} {unit}"

resp = requests.get("https://bd.bangbang93.com/openbmclapi/metric/rank", headers={"Cookie": "openbmclapi-jwt=YOUR_TOKEN_HERE"})

hits = 0
bytes = 0

for i in resp.json():
if i["user"]["name"] != "SALTWO∅D":
continue
metric = i["metric"]
hits += metric["hits"]
bytes += metric["bytes"]

print(to_readable_hits(hits))
print(to_readable_bytes(bytes))

写着写着,感觉这玩意可以有更多的玩法,于是又改了改成了这样:

from dataclasses import dataclass
import requests
import math

@dataclass
class User():
hits: int = 0
bytes: int = 0
clusters: int = 0

@dataclass
class Result():
rank: int
name: str
hits: int
bytes: int
clusters: int

def __repr__(self) -> str:
return f""

def to_readable_hits(hits):
units = ['次', '万次', '亿次', '兆次']
pows = math.floor(math.log(hits, 10000)) if hits != 0 else 0
unit = units[pows]
return f"{hits / (10000 ** pows):.2f}{unit}"

def to_readable_bytes(bytes):
units = ['B', 'KB', 'MB', 'GB', 'TB']
pows = math.floor(math.log(bytes, 1024)) if bytes != 0 else 0
unit = units[pows]
return f"{bytes / (1024 ** pows):.2f} {unit}"

if __name__ == '__main__':
resp = requests.get("https://bd.bangbang93.com/openbmclapi/metric/rank", headers={"Cookie": "openbmclapi-jwt=YOUR_TOKEN_HERE"})
users: dict[str, User] = {}

for i in resp.json():
if "metric" not in i: continue

user = i["user"]["name"]
entity = users.get(user, None)
entity = entity if entity is not None else User()

metric = i["metric"]

hits = metric["hits"]
entity.hits += hits
bytes = metric["bytes"]
entity.bytes += bytes

if hits != 0 and bytes != 0: entity.clusters += 1

users[user] = entity

entries = list(users.items())
entries.sort(key = lambda item: item[1].bytes, reverse=True)

count = 0
results = []
for i in entries:
count += 1
results.append(Result(count, i[0], i[1].hits, i[1].bytes, i[1].clusters))
for i in results: print(i)

这样就实现了一个简单的用户节点流量排序。

顺带把执行的输出给一个示例:

1. MoeMelon —— 3615.05万次 20.43 TB
2. muxiaohan —— 3132.53万次 18.32 TB
3. weyeah —— 2401.30万次 14.74 TB
4. ZeroWolf —— 2329.03万次 13.34 TB
5. Huangsam04 —— 1734.03万次 9.35 TB
6. Frkovo —— 1486.50万次 8.41 TB
7. SALTWO∅D —— 1302.18万次 7.48 TB
8. FOXBALL-ONE —— 1145.32万次 6.29 TB
9. 冬烟mio —— 1129.84万次 6.26 TB
10. ApliNi —— 933.48万次 5.39 TB
11. bangbang93 —— 683.83万次 3.38 TB
12. ByLongGe —— 440.29万次 3.24 TB
13. Sora Shion —— 563.33万次 3.23 TB
14. boxdl —— 579.42万次 3.19 TB
15. Mari2233 —— 568.57万次 3.09 TB
16. ghitori —— 547.54万次 3.04 TB
17. 听风 —— 496.70万次 2.83 TB
18. 8Mi_Yile —— 465.09万次 2.54 TB
19. 幽梦琉璃 —— 168.15万次 1.64 TB
20. liuyuhang9066 —— 259.47万次 1.59 TB

评论

  1. Dongyanmio
    Windows Chrome
    2 月前
    2025-2-16 0:00:55

    盐木大佬好厉害,我真的好羡慕啊~
    你就是我的神,在黑夜中指引我方向,你就是那个太阳,温暖着我~

    • 博主
      Dongyanmio
      Windows Edge
      2 月前
      2025-2-16 0:01:42

      死研磨别叫

  2. Caius
    Windows Edge
    2 月前
    2025-2-15 23:57:46

    qwq

发送评论 编辑评论

|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇