Reputation: 33
I'm currently using the session.get() code found at this stackoverflow. When I save the drive files they don't have a filetype suffix, so I have to add one manually to based on file type to open it. Is there a way I can parse the chunk and get filetype variable or maybe even search by hexcode? Better method?
import requests
def download_file_from_google_drive(id, destination):
URL = "https://docs.google.com/uc?export=download"
session = requests.Session()
response = session.get(URL, params = { 'id' : id }, stream = True)
token = get_confirm_token(response)
if token:
params = { 'id' : id, 'confirm' : token }
response = session.get(URL, params = params, 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
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: # filter out keep-alive new chunks
f.write(chunk)
if __name__ == "__main__":
file_id = 'TAKE ID FROM SHAREABLE LINK'
destination = 'DESTINATION FILE ON YOUR DISK'
download_file_from_google_drive(file_id, destination)
Source: Python: download files from google drive using url via @user6770522
Upvotes: 2
Views: 890
Reputation: 33
I used beatifulSoup and findAll to grab the 'title' tag. Then applied it to destination by os path join.
for filename in soup.findAll('title'):
link = filename.string
filename = link.replace(' - Google Drive','')
destination = os.path.join(dir,filename)
file_path = os.path.join(year_name, destination)
drive_pull.download_file_from_google_drive(url_id, file_path)
Upvotes: 1