kryptex矿池根据钱包地址币种获取矿工算力代码

This commit is contained in:
fengche
2025-12-05 17:06:05 +08:00
parent 6cfb1bfea3
commit 07f7b690b7

View File

@@ -0,0 +1,113 @@
import redis
import requests
from bs4 import BeautifulSoup
import pymysql
from datetime import datetime, timedelta
import time
# Redis 配置
r = redis.Redis(host='127.0.0.1', port=6379, db=7, decode_responses=True)
# MySQL 配置
conn = pymysql.connect(
host='127.0.0.1',
user='root',
password='123456',
database='pool',
port=25600,
charset='utf8mb4'
)
cursor = conn.cursor()
# URL 模板
BASE_URL = "https://pool.kryptex.com/zh-cn/{coin}/miner/stats/{wallet}"
HEADERS = {"User-Agent": "Mozilla/5.0"}
def to_mhs(value: str):
"""自动把算力字符串转换成 MH/s 纯数字"""
value = value.strip().lower()
if "gh/s" in value:
return float(value.replace("gh/s", "").strip()) * 1000
if "mh/s" in value:
return float(value.replace("mh/s", "").strip())
if "kh/s" in value:
return float(value.replace("kh/s", "").strip()) / 1000
if "h/s" in value:
return float(value.replace("h/s", "").strip()) / 1_000_000
return 0.0
def get_half_hour_time(now):
"""取最近半点时间"""
minute = 0 if now.minute < 30 else 30
return now.replace(minute=minute, second=0, microsecond=0)
def query_and_insert():
keys = r.keys("*") # 遍历所有 key
half_hour_time = get_half_hour_time(datetime.now())
for key in keys:
try:
parts = key.split(":")
if len(parts) != 4:
continue
pool_name, wallet, coin, algo = parts
coin_lower = coin.lower()
# xtm 特殊处理
coin_for_url = "xtm-c29" if coin_lower == "xtm" else coin_lower
url = BASE_URL.format(coin=coin_for_url, wallet=wallet.lower())
resp = requests.get(url, headers=HEADERS, timeout=10)
resp.raise_for_status()
soup = BeautifulSoup(resp.text, "html.parser")
tbody = soup.find("tbody")
if not tbody:
print(f"池:{pool_name} 币种:{coin} 钱包:{wallet} 没有矿工数据")
continue
rows = tbody.find_all("tr")
if not rows:
print(f"池:{pool_name} 币种:{coin} 钱包:{wallet} 没有矿工数据")
continue
for row in rows:
try:
worker = row.find("th").find("a").text.strip()
tds = row.find_all("td")
# 10 分钟算力列
hashrate_raw = tds[5].text.strip()
mhs = to_mhs(hashrate_raw)
# 输出日志
print(f"池:{pool_name} 币种:{coin} 钱包:{wallet} 矿工:{worker} -> {mhs:.2f} MH/s")
# 写入数据库
sql = """
INSERT INTO `pool.kryptex`
(datetime, pool_name, wallet, miner, hashrate, coin, algorithm)
VALUES (%s,%s,%s,%s,%s,%s,%s)
"""
cursor.execute(sql, (half_hour_time, pool_name, wallet, worker, mhs, coin.upper(), algo))
conn.commit()
except Exception as e:
print("解析矿工数据失败:", e)
except Exception as e:
print("请求或解析失败:", e)
if __name__ == "__main__":
try:
while True:
now = datetime.now()
next_minute = 30 if now.minute < 30 else 60
wait_seconds = (next_minute - now.minute) * 60 - now.second
if wait_seconds > 0:
print(f"等待 {wait_seconds} 秒到下一个半点...")
time.sleep(wait_seconds)
query_and_insert()
except KeyboardInterrupt:
print("程序结束")
finally:
cursor.close()
conn.close()