优化代码结构,新增Forge同步

This commit is contained in:
Bigsk 2020-07-26 12:19:26 +08:00 committed by Gitee
parent 019c334186
commit 950bcaf418

View File

@ -1,4 +1,4 @@
import requests
import requests
import os
import time
import json
@ -22,32 +22,29 @@ Log='[' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ']' + '[INFO]:
print('[' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ']' + '[INFO]:Specifying global variables successful.')
#------------------------------------------------------------------------#
print('------------------------------------------------------------------------')
Log=Log + '------------------------------------------------------------------------' + '\n'
#The Part Of All Kinds Of Functions.
print('[' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ']' + '[INFO]:Starting to load all kinds of modules.')
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 Print And Save Logs.
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.
def log(logvar):
global Log
print('[' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ']' + logvar)
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):
#Function Which Be Used To Synchronize Single File.
def download(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
download=requests.get(url, headers=UserAgent)
with open(SavePath + filepath,"wb") as code:
code.write(synchronize.content)
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.')
#Module Which Be Used To Determine Whether The Dir Exists.
#Function Which Be Used To Make Sure The Dir Exists.
def make_sure_dir_exists(dirpath):
global Log
if os.path.exists(SavePath + dirpath):
@ -55,10 +52,28 @@ def make_sure_dir_exists(dirpath):
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.')
#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.')
log('[INFO]:Loading all kinds of functions successfully.')
#------------------------------------------------------------------------#
print('------------------------------------------------------------------------')
Log=Log + '------------------------------------------------------------------------' + '\n'
#Save JSON In Global Variables
@ -74,9 +89,7 @@ Log=Log + '[' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ']' + '[
print('------------------------------------------------------------------------')
Log=Log + '------------------------------------------------------------------------' + '\n'
#The Part of Determining Whether The Dir Exists.
#Determining Whether The Log Dir Exists.
#Make Sure The Log Dir Exists.
log('[INFO]:Starting to determine whether the log dir exists.')
dirpath='synchronize/' + config['logspath']
if os.path.exists(dirpath):
@ -84,17 +97,7 @@ if os.path.exists(dirpath):
else:
log('[INFO]:The dir ' + dirpath + ' does not exist.The ' + SystemName + ' will create one.')
os.makedirs(dirpath)
log('[INFO]:Determining whether the log dir exists successfully.')
log('[INFO]:Starting to determine whether all of dirs exists.')
#Determining Whether The MC Versions Dir Exists.
log('[INFO]:Starting to determine whether the MC versions dir exists.')
make_sure_dir_exists('mojang/launchermeta/mc/game')
log('[INFO]:Determining whether the MC versions dir exists successfully.')
#Determining Whether The Liteloader Versions Dir Exists.
log('[INFO]:Starting to determine whether the Liteloader versions dir exists.')
make_sure_dir_exists('liteloader/dl/versions')
log('[INFO]:Determining whether the Liteloader versions dir exists successfully.')
log('[INFO]:Determining whether all of dirs exists successfully.')
log('[INFO]:Make sure the log dir exists successfully.')
#------------------------------------------------------------------------#
print('------------------------------------------------------------------------')
@ -103,10 +106,6 @@ Log=Log + '---------------------------------------------------------------------
#The Part of Synchronizing Single Important Files.
log('[INFO]:Starting to synchronize single files.')
#Synchronizing The Minecraft Versions List File.
log('[INFO]:Starting to synchronize MC versions files.')
synchronize('http://launchermeta.mojang.com/mc/game/version_manifest.json', 'mojang/launchermeta/mc/game/version_manifest.json')
log('[INFO]:Synchronizing MC versions files successfully.')
#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')
@ -119,25 +118,21 @@ Log=Log + '---------------------------------------------------------------------
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.')
#------------------------------------------------------------------------#
print('------------------------------------------------------------------------')
Log=Log + '------------------------------------------------------------------------' + '\n'
#MC Versions Synchronizing Part.
log('[INFO]:Starting synchronize all of MC versions.')
#Part 1.
log('[INFO]:Starting synchronize part 1.')
log('[INFO]:Starting to synchronize files and dirs on https://launchermeta.mojang.com/.')
os.popen('python synchronize/part1.py','r')
time.sleep(5)
#Part 2.
log('[INFO]:Starting synchronize part 2.')
log('[INFO]:Starting to synchronize files and dirs on https://libraries.minecraft.net/.')
os.popen('python synchronize/part2.py','r')
#Part 3.
log('[INFO]:Starting synchronize part 3.')
os.popen('python synchronize/part3.py','r')
#------------------------------------------------------------------------#
print('------------------------------------------------------------------------')
Log=Log + '------------------------------------------------------------------------' + '\n'