2020-07-26 04:19:26 +00:00
import requests
2020-07-20 01:56:48 +00:00
import os
import time
import json
print ( ' [ ' + time . strftime ( " % Y- % m- %d % H: % M: % S " , time . localtime ( ) ) + ' ] ' + ' [INFO]:Starting the synchronizing system. ' )
#------------------------------------------------------------------------#
print ( ' ------------------------------------------------------------------------ ' )
#Part Of Load All Kinds Of Global Variables From Json File.
print ( ' [ ' + time . strftime ( " % Y- % m- %d % H: % M: % S " , time . localtime ( ) ) + ' ] ' + ' [INFO]:Starting to specify global variables from json file. ' )
configopen = open ( " synchronize/config.json " , mode = ' r ' )
configjson = configopen . read ( )
configopen . close ( )
config = json . loads ( configjson )
SystemName = config [ ' systemname ' ]
SavePath = ' synchronize/ ' + config [ ' savepath ' ]
Version = config [ ' version ' ]
UserAgent = { ' User-Agent ' : config [ ' useragent ' ] }
Log = ' [ ' + time . strftime ( " % Y- % m- %d % H: % M: % S " , time . localtime ( ) ) + ' ] ' + ' [INFO]:Starting the synchronizing system. \n ' + ' ------------------------------------------------------------------------ \n ' + ' [ ' + time . strftime ( " % Y- % m- %d % H: % M: % S " , time . localtime ( ) ) + ' ] ' + ' [INFO]:Starting to specify global variables. \n ' + ' [ ' + time . strftime ( " % Y- % m- %d % H: % M: % S " , time . localtime ( ) ) + ' ] ' + ' [INFO]:Specifying global variables successful. \n '
print ( ' [ ' + time . strftime ( " % Y- % m- %d % H: % M: % S " , time . localtime ( ) ) + ' ] ' + ' [INFO]:Specifying global variables successful. ' )
#------------------------------------------------------------------------#
Log = Log + ' ------------------------------------------------------------------------ ' + ' \n '
#The Part Of All Kinds Of Functions.
2020-07-26 04:19:26 +00:00
Log = Log + ' [ ' + time . strftime ( " % Y- % m- %d % H: % M: % S " , time . localtime ( ) ) + ' ] ' + ' [INFO]:Starting to load all kinds of functions. ' + ' \n '
#Function Which Be Used To Save Logs.
2020-07-20 01:56:48 +00:00
def log ( logvar ) :
global Log
Log = Log + ' [ ' + time . strftime ( " % Y- % m- %d % H: % M: % S " , time . localtime ( ) ) + ' ] ' + logvar + ' \n '
2020-07-26 04:19:26 +00:00
#Function Which Be Used To Synchronize Single File.
def download ( url , filepath ) :
2020-07-20 01:56:48 +00:00
global Log
global UserAgent
log ( ' [INFO]:The ' + SystemName + ' is synchronizing the file ' + SavePath + filepath + ' . ' )
try :
2020-07-26 04:19:26 +00:00
download = requests . get ( url , headers = UserAgent )
2020-07-20 01:56:48 +00:00
with open ( SavePath + filepath , " wb " ) as code :
2020-07-26 04:19:26 +00:00
code . write ( download . content )
2020-07-20 01:56:48 +00:00
except :
log ( ' [ERROR]:There has a error in task of synchronizing the file ' + SavePath + filepath + ' . ' )
else :
log ( ' [INFO]:The file ' + SavePath + filepath + ' has been synchronized successfully. ' )
2020-07-26 04:19:26 +00:00
#Function Which Be Used To Make Sure The Dir Exists.
2020-07-20 01:56:48 +00:00
def make_sure_dir_exists ( dirpath ) :
global Log
if os . path . exists ( SavePath + dirpath ) :
log ( ' [INFO]:The dir ' + SavePath + dirpath + ' has already existed.Nothing to do. ' )
else :
log ( ' [INFO]:The dir ' + SavePath + dirpath + ' does not exist.The ' + SystemName + ' will create one. ' )
os . makedirs ( SavePath + dirpath )
2020-07-26 04:19:26 +00:00
#Function Which Be Used To Both Synchronize Single File And Make Sure The Dir Exists.
def sychronize ( url , filepath ) :
global Log
global UserAgent
dirpath = filepath [ : : - 1 ] . split ( ' / ' , 1 ) [ - 1 ] [ : : - 1 ]
if os . path . exists ( SavePath + dirpath ) :
log ( ' [INFO]:The dir ' + SavePath + dirpath + ' has already existed.Nothing to do. ' )
else :
log ( ' [INFO]:The dir ' + SavePath + dirpath + ' does not exist.The ' + SystemName + ' will create one. ' )
os . makedirs ( SavePath + dirpath )
log ( ' [INFO]:The ' + SystemName + ' is synchronizing the file ' + SavePath + filepath + ' . ' )
try :
download = requests . get ( url , headers = UserAgent )
with open ( SavePath + filepath , " wb " ) as code :
code . write ( download . content )
except :
log ( ' [ERROR]:There has a error in task of synchronizing the file ' + SavePath + filepath + ' . ' )
else :
log ( ' [INFO]:The file ' + SavePath + filepath + ' has been synchronized successfully. ' )
2020-07-26 11:25:12 +00:00
#Function Which Be Used To Get HTTP Site With Requests Avoid Error.
def httpget ( url ) :
try :
return requests . get ( url = url , headers = UserAgent ) . text
except :
return ' error '
2020-07-26 04:19:26 +00:00
log ( ' [INFO]:Loading all kinds of functions successfully. ' )
2020-07-20 01:56:48 +00:00
#------------------------------------------------------------------------#
Log = Log + ' ------------------------------------------------------------------------ ' + ' \n '
#Save JSON In Global Variables
log ( ' [INFO]:Starting to get the json of MC versions list. ' )
Log = Log + ' [ ' + time . strftime ( " % Y- % m- %d % H: % M: % S " , time . localtime ( ) ) + ' ] ' + ' [INFO]:Starting to get the json of MC versions list. ' + ' \n '
#Save MC Versions List Json In Global Variables
mcversionsjson = requests . get ( url = ' http://launchermeta.mojang.com/mc/game/version_manifest.json ' )
log ( ' [INFO]:Get json of MC versions successfully. ' )
Log = Log + ' [ ' + time . strftime ( " % Y- % m- %d % H: % M: % S " , time . localtime ( ) ) + ' ] ' + ' [INFO]:Get json of MC versions successfully. ' + ' \n '
#------------------------------------------------------------------------#
print ( ' ------------------------------------------------------------------------ ' )
Log = Log + ' ------------------------------------------------------------------------ ' + ' \n '
2020-07-26 04:19:26 +00:00
#Make Sure The Log Dir Exists.
2020-07-20 01:56:48 +00:00
log ( ' [INFO]:Starting to determine whether the log dir exists. ' )
dirpath = ' synchronize/ ' + config [ ' logspath ' ]
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 ' + SystemName + ' will create one. ' )
os . makedirs ( dirpath )
2020-07-26 04:19:26 +00:00
log ( ' [INFO]:Make sure the log dir exists successfully. ' )
2020-07-20 01:56:48 +00:00
#------------------------------------------------------------------------#
print ( ' ------------------------------------------------------------------------ ' )
Log = Log + ' ------------------------------------------------------------------------ ' + ' \n '
#The Part of Synchronizing Single Important Files.
log ( ' [INFO]:Starting to synchronize single files. ' )
#Synchronizing The Liteloader Versions List File.
log ( ' [INFO]:Starting to synchronize Liteloader versions files. ' )
synchronize ( ' http://dl.liteloader.com/versions/versions.json ' , ' liteloader/dl/versions/versions.json ' )
log ( ' [INFO]:Synchronizing Liteloader versions files successfully. ' )
#------------------------------------------------------------------------#
print ( ' ------------------------------------------------------------------------ ' )
Log = Log + ' ------------------------------------------------------------------------ ' + ' \n '
log ( ' [INFO]:Starting to synchronize more files. ' )
log ( ' [WARN]:Attention!In the version new than 2.3.0,the synchronize tools will synchronize files in the background to improve the speed of synchronizing,that means you will not see the log output in the shell/cmd/powershell.If you want to see all logs and not care about the speed,you can download the old version on gitee,but we will not provide update for it. ' )
#MC Versions Synchronizing Part.
log ( ' [INFO]:Starting synchronize all of MC versions. ' )
#Part 1.
log ( ' [INFO]:Starting synchronize part 1. ' )
os . popen ( ' python synchronize/part1.py ' , ' r ' )
#Part 2.
log ( ' [INFO]:Starting synchronize part 2. ' )
os . popen ( ' python synchronize/part2.py ' , ' r ' )
2020-07-26 04:19:26 +00:00
#Part 3.
log ( ' [INFO]:Starting synchronize part 3. ' )
os . popen ( ' python synchronize/part3.py ' , ' r ' )
2020-07-20 01:56:48 +00:00
#------------------------------------------------------------------------#
print ( ' ------------------------------------------------------------------------ ' )
Log = Log + ' ------------------------------------------------------------------------ ' + ' \n '
#Starting To Save Logs.
log ( ' [INFO]:Starting to save logs. ' )
logwrite = open ( ' synchronize/ ' + config [ ' logspath ' ] + " main- " + str ( time . strftime ( " % Y- % m- %d - % H- % M- % S " , time . localtime ( ) ) + ' .txt ' ) , mode = ' w ' )
logwrite . write ( Log + ' [ ' + time . strftime ( " % Y- % m- %d % H: % M: % S " , time . localtime ( ) ) + ' ] ' + ' [INFO]:Save logs successfully. ' + ' \n ' + ' [ ' + time . strftime ( " % Y- % m- %d % H: % M: % S " , time . localtime ( ) ) + ' ] ' + ' [INFO]:All tasks of synchronizing have done successfully. ' )
print ( ' [ ' + time . strftime ( " % Y- % m- %d % H: % M: % S " , time . localtime ( ) ) + ' ] ' + ' [INFO]:Save logs successfully. ' + ' \n ' + ' [ ' + time . strftime ( " % Y- % m- %d % H: % M: % S " , time . localtime ( ) ) + ' ] ' + ' [INFO]:All tasks of synchronizing have done successfully. ' )
logwrite . close ( )