end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

requests for python で、巨大なfileをhttp getする場合、stream=True で chunk化

以下の通り

# https://stackoverflow.com/questions/38511444/python-download-files-from-google-drive-using-url
import requests
BASE_URL = "https://docs.google.com/uc?export=download"

def download_file_from_google_drive(id, destination):
    session = requests.Session()

    # 「stream=True」により、巨大fileを分割読み込み
    # https://qiita.com/ousaan/items/c83e068c4e46035f49bd
    response = session.get(BASE_URL,
                           params = {'id':id},
                           stream = True)

    # 以下のtoken関連はなくても、動作するみたい
    token = get_confirm_token(response)
    if token:
        params = 
        response = session.get(BASE_URL,
                               params = {'id':id, 'confirm':token},
                               stream = True )

    save_response_content(response, destination)

def get_confirm_token(response):
    for key, value in response.cookies.items():
        if key.startswith('download_warning'):
            return value
    return None

def save_response_content(response, destination):
    CHUNK_SIZE = 32768

    with open(destination, "wb") as f:
        for chunk in response.iter_content(CHUNK_SIZE):
            if chunk:
                f.write(chunk)