# -*- coding: utf-8 -*-
import os
import sys
import requests
# 通过资料库id获取文件夹
# url = '{}/api2/repos/{}/dir'.format(site, repo_id)
# dirs = requests.get(url, headers=header).json()
# 通过资料库id递归获取文件夹
# url = '{}/api2/repos/{}/dir/?t=d&recursive=1'.format(site, repo_id)
# print url
# all_dirs = requests.get(url, headers=header).json()
# print all_dirs
# all_file = []
# for i in all_dirs:
# file_path = '{}/{}'.format(i['parent_dir'], i['name'])
# all_file.append(file_path)
# print file_path
# 将文件夹分享给组 使用命令 -X put
# data = {'share_type': 'group',
# 'group_id': group_id,
# 'permission': 'rw'}
# path = r'\ani\chr\chujiu\chujiu.rig.rig_ani'.replace('\\', '/')
# url = '{}/api2/repos/{}/dir/shared_items/?p={}'.format(site, repo_id, path)
# response = requests.put(url, data=data, headers=header)
# print response
# print response.json()
# 下载文件
#
#
# print requests.get(download.json(), stream=True, verify=False)
# x = requests.get(download.json(), stream=True, verify=False)
# # 下载文件夹
# path = r'\ani\asb'.replace('\\', '/')
# ## 创建任务token
# download_path = '/cabin_des_asb'
# url = '{}/api/v2.1/repos/{}/zip-task/?parent_dir={}&dirents={}'.format(site, repo_id, path, download_path)
# download = requests.get(url, headers=header)
# ## 下载任务
# 获取token
def log_in(site, username, password):
url = '{}/api2/auth-token/'.format(site)
data ={'username': username,
'password': password
}
response = requests.post(url, data)
token = response.json()['token']
return token
# 获取组的id
def get_group_id(site, header, group_name):
url = '{}/api/v2.1/admin/search-group/?query={}'.format(site, group_name)
group_list = requests.get(url, headers=header).json()
group_id = group_list['group_list'][0]['id']
return group_id
# 获取资料库id
def get_repo_id(site, header, reponame):
url = '{}/api2/repos/'.format(site)
repo_infos = requests.get(url, headers=header).json()
repo_id = []
for repo_info in repo_infos:
if repo_info['name'] == reponame:
repo_id = repo_info['id']
return repo_id
def get_file_downurl(site, repo_id, path, header):
url = '{}/api2/repos/{}/file/?p={}&reuse=1'.format(site, repo_id, path)
download = requests.get(url, headers=header)
return download.json()
def download_func(url, file_path):
requests.packages.urllib3.disable_warnings() # 屏蔽warning信息
# 第一次请求是为了得到文件总大小
r1 = requests.get(url, stream=True, verify=False)
total_size = int(r1.headers['Content-Length'])
if os.path.exists(os.path.dirname(file_path)):
# 这重要了,先看看本地文件下载了多少
if os.path.exists(file_path):
temp_size = os.path.getsize(file_path) # 本地已经下载的文件大小
else:
temp_size = 0
else:
os.makedirs(os.path.dirname(file_path))
temp_size = 0
# 显示一下下载了多少
print(temp_size)
print(total_size)
# 核心部分,这个是请求下载时,从本地文件已经下载过的后面下载
headers = {'Range': 'bytes=%d-' % temp_size}
# 重新请求网址,加入新的请求头的
r = requests.get(url, stream=True, verify=False, headers=headers)
# 下面写入文件也要注意,看到"ab"了吗?
# "ab"表示追加形式写入文件
with open(file_path, "ab") as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
temp_size += len(chunk)
f.write(chunk)
f.flush()
###这是下载实现进度显示####
done = int(50 * temp_size / total_size)
sys.stdout.write("\r[%s%s] %d%%" % (u'█' * done, ' ' * (50 - done), 100 * temp_size / total_size))
sys.stdout.flush()
print() # 避免上面\r 回车符
site = 'http://192.168.2.23'
token = log_in(site, 'username', 'password')
header = {'Authorization': 'Token {}'.format(token),
'Accept': 'application/json',
'indent': '4'
}
group_name = 'pipline'
group_id = get_group_id(site, header, group_name)
reponame = 'mtz_vendor'
repo_id = get_repo_id(site, header, reponame)
path = r'\b45130.cmp.v004.part1.rar'.replace('\\', '/')
download = get_file_downurl(site, repo_id, path, header)
path = r'D:\SYY\temp\a.rar'
download_func(download, path)
上一篇
fastCopy命令行拷贝
2024-05-10
下一篇
vim
2024-05-10