Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/modules/server/server.py
2020-08-18 21:12:38 +08:00

71 lines
3.1 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.

import aiohttp
import json
import re
from .be import main
async def server(address):
matchObj = re.match(r'(.*):(.*)', address, re.M | re.I)
servers = []
try:
if matchObj:
serip = matchObj.group(1)
port1 = matchObj.group(2)
port2 = matchObj.group(2)
else:
serip = address
port1 = '25565'
port2 = '19132'
try:
url = 'http://motd.wd-api.com/?ip=' + serip + '&port=' + port1 + '&mode=info'
async with aiohttp.ClientSession() as session:
async with session.get(url, timeout=aiohttp.ClientTimeout(total=20)) as req:
if req.status != 200:
print(f"请求时发生错误:{req.status}")
else:
motd = await req.text()
file = json.loads(motd)
try:
if file['code'] == 200:
x = re.sub(r'§\w', "", file['data']['description']['text'])
if not x:
extra = file['data']['description']['extra']
text = []
qwq = ''
for item in extra[:]:
text.append(item['text'])
servers.append('[JE]\n' + qwq.join(text) + "\n" + "在线玩家:" + str(
file['data']['players']['online']) + "/" + str(
file['data']['players']['max']) + "\n" + "游戏版本:" + file['data']['version']['name'])
else:
servers.append(
'[JE]\n' + x + "\n" + "在线玩家:" + str(file['data']['players']['online']) + "/" + str(
file['data']['players']['max']) + "\n" + "游戏版本:" + file['data']['version']['name'])
else:
print('获取JE服务器信息失败。')
except Exception:
try:
x = re.sub(r'§\w', "", file['data']['description'])
servers.append('[JE]\n' + x + "\n" + "在线玩家:" + str(file['data']['players']['online']) + "/" + str(
file['data']['players']['max']) + "\n" + "游戏版本:" + file['data']['version']['name'])
except Exception as e:
print('获取JE服务器信息失败。' + str(e))
servers.append("[JE]\n发生错误调用API时发生错误。")
except Exception as e:
print('获取JE服务器信息失败。' + str(e))
try:
BE = await main(serip, port2)
BER = re.sub(r'§\w', "", BE)
servers.append(BER)
except Exception as e:
print('获取BE服务器信息失败。' + str(e))
if str(servers) == '[]':
return ('连接失败,没有检测到任何服务器。')
else:
awa = '\n'
return (awa.join(servers))
except Exception as e:
return ("发生错误:" + str(e) + ".")