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.
gmclapi/master/sync/sync.py
2021-06-08 23:26:48 +08:00

97 lines
2.8 KiB
Python

'''
Copyright Ghink Network Studio
Website: https://www.ghink.net
'''
import requests,os,time,json,threading,hashlib,re
from urllib.parse import urlparse
db=[]
workPath="gmclapi/"
dataPath=workPath+"data/"
userAgent={'User-Agent':'GMCLAPI/0.0.1'}
proxies = {
"http": "http://127.0.0.1:4780"
}
entrance=["http://launchermeta.mojang.com/mc/game/version_manifest.json"]
def log(info):
info="{}{}".format(time.strftime("%Y/%m/%d %H:%M:%S [Sync Thread]", time.localtime()),info)
print(info)
with open(workPath+"logs.log","a+") as fb:
fb.write(info)
fb.write("\r\n")
def database():
global db
with open(workPath+"database.db","r") as fb:
db=json.loads(fb.read())
while True:
with open(workPath+"database.db","r") as fb:
if json.loads(fb.read())==db:
continue
with open(workPath+"database.db","w+") as fb:
fb.write(json.dumps(db))
def syncMain():
global entrance
global userAgent
global proxies
for obj in entrance:
origin=requests.get(obj,headers=userAgent,proxies=proxies).content
md5=hashlib.md5()
md5.update(origin)
hash=md5.hexdigest()
switch=True
for h in db:
if h["hash"]==hash:
switch=False
break
if switch:
i=0
for h in db:
if h["path"]==urlparse(obj).path:
del switch[i]
i+=1
with open(dataPath+hash,"wb") as fb:
fb.write(origin)
db.append({
"hash":hash,
"source":obj,
"path":urlparse(obj).path
})
def syncMinecraft():
global userAgent
global proxies
for h in db:
if h["path"]=="/mc/game/version_manifest.json":
hash=h["hash"]
break
with open(dataPath+hash,"r") as fb:
fall=fb.read()
result=re.findall('(https?|ftp|file)|([-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|])',fall)
print(result)
res=[]
for i in result:
res.append("{}{}".format(i[0]+i[1]))
for obj in res:
switch=True
for h in db:
if h["source"]==obj:
switch=False
if switch:
origin=requests.get(obj,headers=userAgent,proxies=proxies).content
md5=hashlib.md5()
md5.update(origin)
hash=md5.hexdigest()
with open(dataPath+hash,"wb") as fb:
fb.write(origin)
db.append({
"hash":hash,
"source":obj,
"path":urlparse(obj).path
})
def main():
threading.Thread(target=database).start()
syncMain()
#main()
threading.Thread(target=database).start()
time.sleep(1)
syncMinecraft()