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/server/serverraw.py
2020-06-13 20:43:43 +08:00

49 lines
No EOL
2.7 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 re
import requests
import json
def serverraw(address):
matchObj = re.match(r'(.*):(.*).*', address, re.M|re.I)
try:
if matchObj:
url = 'http://motd.wd-api.com/?ip='+matchObj.group(1)+'&port='+matchObj.group(2)+'&mode=info'
motd = requests.get(url,timeout=10)
file = json.loads(motd.text)
try:
if file['code'] == 200:
x = file['data']['description']['text']
if not x:
extra = file['data']['description']['extra']
return(str(extra)+"\n"+"在线玩家:"+str(file['data']['players']['online'])+"/"+str(file['data']['players']['max'])+"\n"+"游戏版本:"+file['data']['version']['name'])
else:
return(x+"\n"+"在线玩家:"+str(file['data']['players']['online'])+"/"+str(file['data']['players']['max'])+"\n"+"游戏版本:"+file['data']['version']['name'])
else:
return("连接服务器失败。")
except Exception:
try:
x = file['data']['description']
return(x+"\n"+"在线玩家:"+str(file['data']['players']['online'])+"/"+str(file['data']['players']['max'])+"\n"+"游戏版本:"+file['data']['version']['name'])
except Exception:
return("发生错误调用API时发生错误。")
else:
url = 'http://motd.wd-api.com/?ip='+address+'&port=25565&mode=info'
motd = requests.get(url,timeout=10)
file = json.loads(motd.text)
try:
if file['code'] == 200:
x = file['data']['description']['text']
if not x:
extra = file['data']['description']['extra']
return(str(extra)+"\n"+"在线玩家:"+str(file['data']['players']['online'])+"/"+str(file['data']['players']['max'])+"\n"+"游戏版本:"+file['data']['version']['name'])
else:
return(x+"\n"+"在线玩家:"+str(file['data']['players']['online'])+"/"+str(file['data']['players']['max'])+"\n"+"游戏版本:"+file['data']['version']['name'])
else:
return("连接服务器失败。")
except Exception:
try:
x = file['data']['description']
return(x+"\n"+"在线玩家:"+str(file['data']['players']['online'])+"/"+str(file['data']['players']['max'])+"\n"+"游戏版本:"+file['data']['version']['name'])
except Exception:
return("发生错误调用API时发生错误。")
except Exception:
return("发生错误:未知原因。")