def download_revisions(httpauth, service, fileID, title, path, counter, log_file):
if not os.path.exists(path + "/" + title):
os.makedirs(path + "/" + title)
url = "https://www.googleapis.com/drive/v3/files/" + fileID + "/revisions"
resp, content = httpauth.request(url, 'GET')
revisions = json.loads(content.decode('utf-8'))
revision_info = []
rev_num = 1
for revision in revisions["revisions"]:
revision_info.append([str(rev_num), revision["id"], revision["modifiedTime"]])
file_path = path + "/" + title + "/" + title + ".rev" + str(rev_num)
orig_title = str(title)
# to prevent duplicate file names being saved
if os.path.exists(file_path):
file_path, title = get_new_file_name(file_path)
log_and_print(log_file, counter + " File named '" + orig_title + "' already exists. Saving as '" + title + "' instead.")
log_and_print(log_file, counter + " Downloading '" + title + ".rev" + str(rev_num) + "'...")
request = service.revisions().get_media(fileId=fileID, revisionId=revision["id"])
fh = io.FileIO(file_path, mode='wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
# Print status of download (mainly for larger files)
print("%d%%\r" % int(status.progress() * 100), end="", flush=True)
fh.close()
log_and_print(log_file, counter + " Hashing '" + title + ".rev" + str(rev_num) + "'...")
with open(path + "/_hashes.txt", "a") as hashes_file:
hashes_file.write(title + ".rev" + str(rev_num) + "\n")
hashes_file.write("--MD5: " + hash_file(file_path, "md5") + "\n")
hashes_file.write("--SHA1: " + hash_file(file_path, "sha1") + "\n")
hashes_file.write("--SHA256: " + hash_file(file_path, "sha256") + "\n")
rev_num += 1
log_and_print(log_file, counter + " Writing revision info for '" + title + "'...")
with open(path + "/" + title + "/" + title + "_revisions.txt", "w") as saved_file:
for item in revision_info:
saved_file.write("Revision Number: " + item[0] + "\n")
saved_file.write("--Revision ID: " + item[1] + "\n")
saved_file.write("--Revision Last Modifed: " + item[2] + "\n")
# Check if there are revisions for a given fileID
gdrive.py 文件源码
python
阅读 25
收藏 0
点赞 0
评论 0
评论列表
文章目录