我试图在脚本中从谷歌驱动器下载一个文件,我这样做有点麻烦。我要下载的文件在这里。
我在网上搜了很多,终于下载了其中一个。我得到了文件的uid,较小的文件(1.6MB)下载正常,但较大的文件(3.7GB)总是重定向到一个页面,询问我是否想在不进行病毒扫描的情况下继续下载。谁能帮我跳过那个屏幕?
下面是我如何让第一个文件工作-
curl -L "https://docs.google.com/uc?export=download&id=0Bz-w5tutuZIYeDU0VDRFWG9IVUE" > phlat-1.0.tar.gz
当我对另一个文件进行同样操作时,
curl -L "https://docs.google.com/uc?export=download&id=0Bz-w5tutuZIYY3h5YlMzTjhnbGM" > index4phlat.tar.gz
我得到以下输出-
我注意到在链接的第三行到最后一行,有一个&confirm=JwkK,这是一个随机的4个字符的字符串,但建议有一种方法添加到我的URL确认。我访问的一个链接建议&confirm=no_antivirus,但这不起作用。
我希望这里有人能帮忙!
我无法让Nanoix的perl脚本工作,或者我看到的其他curl示例,所以我开始自己用python研究api。这适用于小文件,但大文件阻塞了可用的ram,所以我找到了一些其他不错的分块代码,使用api的部分下载功能。要点:
https://gist.github.com/csik/c4c90987224150e4a0b2
注意从API接口下载client_secret json文件到本地目录的部分。
源
$ cat gdrive_dl.py
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
"""API calls to download a very large google drive file. The drive API only allows downloading to ram
(unlike, say, the Requests library's streaming option) so the files has to be partially downloaded
and chunked. Authentication requires a google api key, and a local download of client_secrets.json
Thanks to Radek for the key functions: http://stackoverflow.com/questions/27617258/memoryerror-how-to-download-large-file-via-google-drive-sdk-using-python
"""
def partial(total_byte_len, part_size_limit):
s = []
for p in range(0, total_byte_len, part_size_limit):
last = min(total_byte_len - 1, p + part_size_limit - 1)
s.append([p, last])
return s
def GD_download_file(service, file_id):
drive_file = service.files().get(fileId=file_id).execute()
download_url = drive_file.get('downloadUrl')
total_size = int(drive_file.get('fileSize'))
s = partial(total_size, 100000000) # I'm downloading BIG files, so 100M chunk size is fine for me
title = drive_file.get('title')
originalFilename = drive_file.get('originalFilename')
filename = './' + originalFilename
if download_url:
with open(filename, 'wb') as file:
print "Bytes downloaded: "
for bytes in s:
headers = {"Range" : 'bytes=%s-%s' % (bytes[0], bytes[1])}
resp, content = service._http.request(download_url, headers=headers)
if resp.status == 206 :
file.write(content)
file.flush()
else:
print 'An error occurred: %s' % resp
return None
print str(bytes[1])+"..."
return title, filename
else:
return None
gauth = GoogleAuth()
gauth.CommandLineAuth() #requires cut and paste from a browser
FILE_ID = 'SOMEID' #FileID is the simple file hash, like 0B1NzlxZ5RpdKS0NOS0x0Ym9kR0U
drive = GoogleDrive(gauth)
service = gauth.service
#file = drive.CreateFile({'id':FILE_ID}) # Use this to get file metadata
GD_download_file(service, FILE_ID)
解决方案只使用谷歌驱动器API
在运行下面的代码之前,您必须激活谷歌驱动器API,安装依赖项并验证您的帐户。说明可以在原来的谷歌驱动器API指南页面上找到
import io
import os
import pickle
import sys, argparse
from googleapiclient.discovery import build
from google.auth.transport.requests import Request
from googleapiclient.http import MediaIoBaseDownload
from google_auth_oauthlib.flow import InstalledAppFlow
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/drive.readonly']
def _main(file_id, output):
""" Shows basic usage of the Drive v3 API.
Prints the names and ids of the first 10 files the user has access to.
"""
if not file_id:
sys.exit('\nMissing arguments. Correct usage:\ndrive_api_download.py --file_id <file_id> [--output output_name]\n')
elif not output:
output = "./" + file_id
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('drive', 'v3', credentials=creds)
# Downloads file
request = service.files().get_media(fileId=file_id)
fp = open(output, "wb")
downloader = MediaIoBaseDownload(fp, request)
done = False
while done is False:
status, done = downloader.next_chunk(num_retries=3)
print("Download %d%%." % int(status.progress() * 100))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--file_id')
parser.add_argument('-o', '--output')
args = parser.parse_args()
_main(args.file_id, args.output)
2018年5月工作
嗨,根据这些评论…我创建一个bash导出URL列表从文件URL .txt到URLS_DECODED.txt
在一些加速器如flashget中使用(我使用cygwin来结合Windows和Linux)
引入命令爬行器是为了避免下载并(直接)获得最终链接
命令GREP HEAD和CUT,处理并获得最终链接,是基于西班牙语,也许你可以移植到英语语言
echo -e "$URL_TO_DOWNLOAD\r"可能\r只是cywin,必须用\n(换行符)代替
**********user***********为用户文件夹
*******Localización***********是西班牙语,清除星号,让英语单词定位和适应头部和切割数字适当的方法。
rm -rf /home/**********user***********/URLS_DECODED.txt
COUNTER=0
while read p; do
string=$p
hash="${string#*id=}"
hash="${hash%&*}"
hash="${hash#*file/d/}"
hash="${hash%/*}"
let COUNTER=COUNTER+1
echo "Enlace "$COUNTER" id="$hash
URL_TO_DOWNLOAD=$(wget --spider --load-cookies /tmp/cookies.txt "https://docs.google.com/uc?export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id='$hash -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id="$hash 2>&1 | grep *******Localización***********: | head -c-13 | cut -c16-)
rm -rf /tmp/cookies.txt
echo -e "$URL_TO_DOWNLOAD\r" >> /home/**********user***********/URLS_DECODED.txt
echo "Enlace "$COUNTER" URL="$URL_TO_DOWNLOAD
done < /home/**********user***********/URLS.txt