This repository has been archived on 2022-12-28. You can view files and clone it, but cannot push or open issues or pull requests.
gmclcore/__main__.py
Bigsk 0d7117ea91 第一次更新
目前只有获取内存与下载文件功能
2020-08-07 18:30:20 +08:00

217 lines
11 KiB
Python

import os,requests,json,time,sys,argparse,psutil
#-------------------------------------------------------#
#Environment Setup Area.
#Set Global Var.
Log='[Logs Output]'
#Read Config File.
config_open=open("config.json", mode='r')
config_json=config_open.read()
config_open.close()
try:
config=json.loads(config_json)
config_state=True
except:
config_state=False
Log=Log + '\n' + "[Program]" + '[' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ']' + '[INFO]Starting to read config file.'
if(config_state==True):
Log=Log + '\n' + "[" + config['name'] + "]" + '[' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ']' + '[INFO]Read config successfully.'
else:
Log=Log + '\n' + "[Program]" + '[' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ']' + '[ERROR]Read config failed.'
Log=Log + '\n' + "[Program]" + '[' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ']' + '[WARN]There is a deadly error,the program will stop soon.'
Log=Log + '\n' + "[Program]" + '[' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ']' + '[INFO]Starting to save log.'
if os.path.exists('logs'):
Log=Log + '\n' + "[Program]" + '[' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ']' + '[INFO]The dir logs has already existed.Nothing to do.'
else:
Log=Log + '\n' + "[Program]" + '[' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ']' + '[INFO]The dir logs does not exist.The program will create one.'
os.makedirs('logs')
logs_output=open('logs/' + str(time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) + '.txt'),mode='w')
logs_output.write(Log + '\n' + "[Program]" + '[' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ']' + '[INFO]Save logs successfully.' + '\n' + "[Program]" + '[' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ']' + '[WARN]Please check if the json is right.')
logs_output.close()
sys.exit(0)
#Get Parameter.
parser=argparse.ArgumentParser()
parser.add_argument("--launcher_name", help="The name of the launcher.")
parser.add_argument("--launcher_version", help="The version of the launcher.")
parser.add_argument("--type", help="The type of task,{\"get_game_version_list\",\"get_java_path\",\"get_system_memory\",\"get_online_source_list\",\"install_online_source\",\"download_file\"}")
parser.add_argument("--copyright", help="The copyright of the game,true of false.")
#Launcher Game Parameter Area.
#True Copyright.
parser.add_argument("--email", help="The email of game account.(If your game is genuine,you have to input this parameter.)")
parser.add_argument("--password", help="The password of game account.(If your game is genuine,you have to input this parameter.")
#False Copyright.
parser.add_argument("--id", help="The id of game.(If your game is not genuine,you have to input this parameter.)")
#Get System Memory Area.
parser.add_argument("--memory_data_type", help="The data type of memory that will return.{\"free\",\"used\",\"total\"}")
parser.add_argument("--memory_data_unit", help="The data unit of memory that will return,the default unit is B.{\"MB\",\"KB\",\"GB\",\"B\",\"TB\"}")
#Get Online Source List Area.
parser.add_argument("--online_source_list_from", help="The from of online source list.{\"BMCLAPI\",\"GEMCAPI\",\"MOJANG\",\"MIX\"}")
parser.add_argument("--online_source_list_type", help="The online source type of list.")
parser.add_argument("--online_source_list_return_type", help="The return data type of return data of online source,the default is json.{\"json\"}")
#Install online Source Area.
parser.add_argument("--install_online_source_from", help="The from of online source which you want to install.{\"BMCLAPI\",\"GEMCAPI\",\"MOJANG\",\"MIX\"}")
parser.add_argument("--install_online_source_type", help="The online source type of which source you want to install,If you want to install one type with other source,you have to input a json there.")
parser.add_argument("--install_online_source_id", help="The online source id of which source you want to install.")
parser.add_argument("--install_online_source_dir", help="The game dir which you want to install to.")
#Download Source Area.
parser.add_argument("--download_url", help="The file url of source which you want download.If you want to download multi files,you have to input json like:{\"number\":10,\"url\":[\"example1.com\",\"example2.com\"]} and {\"number\":10,\"path\":[\"\\usr\\root\\home\\Desktop\\1\",\"\\usr\\root\\home\\Desktop\\2\"]}")
parser.add_argument("--download_path", help="The save path of source which you want download.")
#Global Parameter Area.
parser.add_argument("--memory", help="The memory that could be used by game.You should input with unit.")
parser.add_argument("--java", help="The java path of the system,if you send a null parameter,the core will get java path automatically.")
parser.add_argument("--parameter", help="The excess parameter that launcher game need.")
parser.add_argument("--dir", help="The game dir.")
parser.add_argument("--version", help="The game version.")
parser=parser.parse_args()
#Set Parameter into Var.
if(parser.launcher_name==None):
launcher_name=config['default_launcher_name']
else:
launcher_name=parser.launcher_name
if(parser.launcher_version==None):
launcher_version=config['version']
else:
launcher_version=parser.launcher_version
#Set Config Into Var.
UA={'User-Agent':launcher_name + '/' + launcher_version + config['useragent']}
name=config['name']
logs_head="[" + config['name'] + "]"
#-------------------------------------------------------#
#Sub_Program Area.
def log(log_type,logvar):
global Log
global name
global logs_head
if(log_type=="info"):
log_type="INFO"
elif(log_type=="warn"):
log_type="WARN"
elif(log_type=="error"):
log_type="ERROR"
Log=Log + '\n' + logs_head + '[' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ']' + '[' + log_type + ']' + logvar
def launcher_true(email,password,memory,java,parameter,dir,version):
null=""
def launcher_false(id,memory,java,parameter,dir,version):
null=""
def download_source(type,assembly,version):
null=""
def download(url, dirpath):
global Log
global UA
global name
log("info",'The ' + name + ' is downloading the file ' + filepath + '.')
try:
download=requests.get(url, headers=UA)
with open(filepath,"wb") as code:
code.write(download.content)
except:
log("error",'There has a error in task while downloading the file ' + filepath + '.')
else:
log("info",'The file ' + filepath + ' has been downloaded successfully.')
def make_dir_exist(dirpath):
global Log
global name
if os.path.exists(dirpath):
log("info",'The dir ' + dirpath + ' has already existed.Nothing to do.')
else:
log("info",'The dir ' + dirpath + ' does not exist.The ' + name + ' will create one.')
os.makedirs(dirpath)
def memory(return_type):
if(return_type=="free"):
return psutil.virtual_memory().free
elif(return_type=="used"):
return psutil.virtual_memory().used
elif(return_type=="total"):
return psutil.virtual_memory().total
def error_output_log(logpath):
global Log
global logs_head
logs_output=open(logpath + '/' + str(time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) + '.txt'),mode='w')
logs_output.write(Log + '\n' + logs_head + '[' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ']' + '[INFO]Save logs successfully.' + '\n' + logs_head + '[' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ']' + '[WARN]Please check if the parameter is right.')
logs_output.close()
def deadly_error():
log("warn","There is a deadly error,the program will stop soon.")
make_dir_exist(config['logs_path'])
error_output_log(config['logs_path'])
sys.exit(0)
#-------------------------------------------------------#
log("info","Start to deal the tasks that need to do.")
if(parser.type=="download_file"):
log("info","Download file(s) mode.")
try:
try:
urljson=json.loads(parser.download_url)
pathjson=json.loads(parser.download_path)
if(urljson['number']==pathjson['number']):
count=0
for i in range(0,urljson['number']):
try:
download(urljson['url'][count],pathjson['path'][count])
except:
log("error""Some errors happened while downloading files,please check and try again.")
count=count+1
else:
log("error","The file(s) number is not same!We can't download the file(s).")
deadly_error()
except:
try:
download(urljson['url'][count],pathjson['path'][count])
except:
log("error""Some errors happened while downloading files,please check and try again.")
deadly_error()
except:
log("error","Some errors happened.We can't download the file(s),please check if the parameter you input is correct.")
deadly_error()
elif(parser.type=="get_system_memory"):
log("info","Get system memory mode.")
try:
if(parser.memory_data_type=="free"):
memory_data=memory("free")
elif(parser.memory_data_type=="used"):
memory_data=memory("used")
elif(parser.memory_data_type=="total"):
memory_data=memory("total")
else:
log("error","We can't get the memory data,please check if the parameter you input is coreect.")
deadly_error()
if(parser.memory_data_unit==None):
print(memory_data)
log("info","Return:" + str(memory_data) + '.')
elif(parser.memory_data_unit=="B"):
print(memory_data)
log("info","Return:" + str(memory_data) + '.')
elif(parser.memory_data_unit=="KB"):
print(memory_data / 1024)
log("info","Return:" + str(memory_data / 1024) + '.')
elif(parser.memory_data_unit=="MB"):
print(memory_data / 1024 / 1024)
log("info","Return:" + str(memory_data / 1024 / 1024) + '.')
elif(parser.memory_data_unit=="GB"):
print(memory_data / 1024 / 1024 / 1024)
log("info","Return:" + str(memory_data / 1024 / 1024 / 1024) + '.')
elif(parser.memory_data_unit=="TB"):
print(memory_data / 1024 / 1024 / 1024 / 1024)
log("info","Return:" + str(memory_data / 1024 / 1024 / 1024 / 1024) + '.')
except:
log("error","Some errors happened.We can't get the memory data,please check if the parameter you input is correct.")
deadly_error()
#-------------------------------------------------------#
#Make Dir Area.
make_dir_exist("logs")
#-------------------------------------------------------#
#Starting To Save Logs.
log("info",'Starting to save logs.')
logs_output=open(config['logs_path'] + '/' + str(time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) + '.txt'),mode='w')
logs_output.write(Log + '\n' + logs_head + '[' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ']' + '[INFO]Save logs successfully.' + '\n' + logs_head + '[' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ']' + '[INFO]The task has done successfully.')
logs_output.close()