minecraft-monitor/backend/utils.py
2024-03-02 10:54:27 +08:00

36 lines
No EOL
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from mcstatus import JavaServer
from mcstatus.status_response import JavaStatusResponse
from datetime import datetime, timedelta
from dateutil import parser
from pytz import timezone
from db import MCDB
async def getJavaServerStatus(host: str) -> JavaStatusResponse:
return await (await JavaServer.async_lookup(host)).async_status()
async def getJavaServerLatency(host: str) -> float:
status = await getJavaServerStatus(host)
return status.latency
async def schedule_job(host: str, mcdb: MCDB):
current_time = datetime.now(timezone("Asia/Shanghai"))
try:
latency = await getJavaServerLatency(host)
except Exception as e:
# 服务器连接失败可能宕机了把延迟设为10s秒
latency = 10000
print(e)
mcdb.insert_time_and_latency(current_time, latency)
def get_last_hour_date_info():
return (datetime.now(timezone("Asia/Shanghai")) - timedelta(hours=1))
def get_current_day_time_utc8():
return datetime.now(timezone("Asia/Shanghai")).strftime('%H:%M:%S')
def parse_date_to_h_m_s(timestamp_str) -> str:
timestamp = parser.parse(timestamp_str)
formatted_timestamp = timestamp.strftime("%H:%M:%S")
return formatted_timestamp