在 TG 中获取搬瓦工 vps 信息

2023年 6月 13日 作者 max

利用搬瓦工的 api 和 pyrogram,即可在任意对话中获取 vps 的信息。

安装 python3 和 pyrogram

apt install python3 python3-pip 
pip3 install -U pyrogram tgcrypto requests

脚本代码:

#!/usr/bin/env python3
#-*-coding:utf-8-*-

import os
import time
import requests
from pyrogram import Client, filters

# 在搬瓦工后台获取 veid 和 api_key
veid = '0000000'
api_key = 'private_1234567890zxcvbn'

# 在 https://my.telegram.org/ 登入自己的 tg 账号申请 api_id、api_hash
api_id = '00000000'
api_hash = '1234567890zxcvbnm'

def bwh_info():
  LiveServiceInfo = requests.get(url=f'https://api.64clouds.com/v1/getLiveServiceInfo?veid={veid}&api_key={api_key}')
  node_datacenter = LiveServiceInfo.json()['node_datacenter']
  ve_status = LiveServiceInfo.json()['ve_status']
  load_average = LiveServiceInfo.json()['load_average']
  mem_available = str(LiveServiceInfo.json()['mem_available_kb'] / 1000)
  plan_ram = str(LiveServiceInfo.json()['plan_ram'] / 1024 / 1024)
  ve_used_disk_space = str(round(LiveServiceInfo.json()['ve_used_disk_space_b'] / 1024 / 1024 / 1024,2))
  ve_disk_quota_gb = LiveServiceInfo.json()['ve_disk_quota_gb']
  monthly_data_multiplier = LiveServiceInfo.json()['monthly_data_multiplier']
  data_counter = str(round(LiveServiceInfo.json()['data_counter'] * monthly_data_multiplier / 1024 / 1024 / 1024,2))
  plan_monthly_data = str(round(LiveServiceInfo.json()['plan_monthly_data'] / 1024 / 1024 / 1024,2))
  data_next_reset = time.strftime("%Y-%m-%d", time.localtime(LiveServiceInfo.json()['data_next_reset']))
  text = f'**{node_datacenter}**\n\n'   
  text += f'状态:`{ve_status},{load_average}`\n'
  text += f'内存(可用):`{mem_available}/{plan_ram}MB`\n'
  text += f'硬盘(已用):`{ve_used_disk_space}/{ve_disk_quota_gb}GB`\n'
  text += f'流量(已用):`{data_counter}/{plan_monthly_data}GB`\n'
  text += f'流量重置日期:`{data_next_reset}`\n'
  return text

app = Client(f'{os.path.dirname(os.path.abspath(__file__))}/{os.path.splitext(os.path.basename(__file__))[0]}', api_id=api_id, api_hash=api_hash)

@app.on_message(filters.me & ~filters.forwarded & filters.command('bwh', prefixes='/'))
async def hello(client, message):
  await message.edit_text("获取中...")
  await message.edit_text(bwh_info())

app.run()

将以上内容保存为脚本如 bwh.py,然后运行、按照提示登录即可。