102 lines
5.4 KiB
Python
102 lines
5.4 KiB
Python
import requests
|
|
import os
|
|
import time
|
|
import json
|
|
import sys
|
|
|
|
#------------------------------------------------------------------------#
|
|
|
|
#Part Of Load All Kinds Of Global Variables From Json File.
|
|
|
|
configopen=open(os.path.dirname(os.path.abspath(sys.argv[0])) + "/config.json", mode='r')
|
|
configjson=configopen.read()
|
|
configopen.close()
|
|
config=json.loads(configjson)
|
|
SystemName=config['systemname']
|
|
SavePath=os.path.dirname(os.path.abspath(sys.argv[0])) + "/" + 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'
|
|
|
|
#------------------------------------------------------------------------#
|
|
Log=Log + '------------------------------------------------------------------------' + '\n'
|
|
|
|
#The Part Of All Kinds Of Functions.
|
|
|
|
Log=Log + '[' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ']' + '[INFO]:Starting to load all kinds of modules.' + '\n'
|
|
#Module Which Be Used To Save Logs.
|
|
def log(logvar):
|
|
global Log
|
|
Log=Log + '[' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ']' + logvar + '\n'
|
|
#Module Which Be Used To Synchronize Single File.
|
|
def synchronize(url, filepath):
|
|
global Log
|
|
global UserAgent
|
|
log('[INFO]:The ' + SystemName + ' is synchronizing the file ' + SavePath + filepath + '.')
|
|
try:
|
|
synchronize=requests.get(url, headers=UserAgent)#Without Proxies
|
|
with open(SavePath + filepath,"wb") as code:
|
|
code.write(synchronize.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.')
|
|
#Module Which Be Used To Determine Whether The Dir Exists.
|
|
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)
|
|
log('[INFO]:Loading all kinds of modules successfully.')
|
|
|
|
#------------------------------------------------------------------------#
|
|
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'
|
|
|
|
#------------------------------------------------------------------------#
|
|
Log=Log + '------------------------------------------------------------------------' + '\n'
|
|
|
|
log('[INFO]:Starting to count how many MC versions in total.')
|
|
#Count How Many MC Versions in total.
|
|
mcversionsjsontext=mcversionsjson.text
|
|
mcversionsnumber=mcversionsjsontext.count('"url":')
|
|
log('[INFO]:There has ' + str(mcversionsnumber) + ' MC versions in total .')
|
|
log('[INFO]:Counting how many MC versions in total successfully.')
|
|
|
|
#Auto Synchronize Files And Dirs on https://launchermeta.mojang.com/.
|
|
log('[INFO]:Starting to synchronize files and dirs on https://launchermeta.mojang.com/.')
|
|
whiletimes=0#Set Var whiletimes To Avoid Error.
|
|
while(whiletimes < mcversionsnumber):
|
|
#Count While Times.
|
|
whiletimes=whiletimes + 1
|
|
#Read Json Of MC Versions List.
|
|
downloadurlload=json.loads(mcversionsjsontext)
|
|
whiletimesload=whiletimes - 1
|
|
onlineurl=downloadurlload['versions'][whiletimesload]['url']
|
|
onlineurlwithouthttps=onlineurl.replace('https://', 'http://')#Avoid Https Connetion Errors
|
|
fileurl='mojang/launchermeta/' + onlineurl.replace('https://launchermeta.mojang.com/','')
|
|
filetosync=fileurl[::-1].split('/', 1)[-1][::-1]
|
|
#Synchronize.
|
|
make_sure_dir_exists(filetosync)
|
|
synchronize(onlineurlwithouthttps, fileurl)
|
|
log('[INFO]:Synchronizing files and dirs on https://launchermeta.mojang.com/ successfully.')
|
|
|
|
#------------------------------------------------------------------------#
|
|
Log=Log + '------------------------------------------------------------------------' + '\n'
|
|
|
|
#Starting To Save Logs.
|
|
log('[INFO]:Starting to save logs.')
|
|
logwrite=open(os.path.dirname(os.path.abspath(sys.argv[0])) + config['logspath'] + 'part1-' + 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.')
|
|
logwrite.close()
|