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.
gemcapi/old/5.0.0/download.py

39 lines
1.6 KiB
Python
Raw Permalink Normal View History

2020-08-25 14:40:22 +00:00
import os,requests,json,time,sys,argparse,psutil
Log=""
UA={'User-Agent':'GeMCAPI/5.0.0 (GeMC API Sync 5.0.0;Release)'}
parser=argparse.ArgumentParser()
parser.add_argument("--url", help="The online url of the file which need to download.")
parser.add_argument("--file", help="The file path of the file which need to download.")
parser.add_argument("--other", help="Other parameter which help some special file to download correctly.")
parser=parser.parse_args()
proxies={'http':'http://127.0.0.1:4780'}
def log(log_type,logvar):
global Log
if(log_type=="info"):
log_type="INFO"
elif(log_type=="warn"):
log_type="WARN"
elif(log_type=="error"):
log_type="ERROR"
Log=Log + '\n' + '[' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ']' + '[' + log_type + ']' + logvar
def download(url, dirpath):
global Log
global UA
log("info",'We are downloading the file ' + dirpath + '.')
try:
download=requests.get(url, headers=UA, proxies=proxies)
with open(dirpath,"wb") as code:
code.write(download.content)
except:
log("error",'There has a error in task while downloading the file ' + dirpath + '.')
else:
log("info",'The file ' + dirpath + ' has been downloaded successfully.')
def make_dir_exist(dirpath):
global Log
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.We will create one.')
os.makedirs(dirpath)
make_dir_exist(parser.file[::-1].split('/', 1)[-1][::-1])
download(parser.url,parser.file)