Skip to content

Instantly share code, notes, and snippets.

@KasRoudra
Last active August 2, 2024 17:57
Show Gist options
  • Save KasRoudra/c91efbefb40a2661b9f3ec9f4760c3a0 to your computer and use it in GitHub Desktop.
Save KasRoudra/c91efbefb40a2661b9f3ec9f4760c3a0 to your computer and use it in GitHub Desktop.
A simple google drive downloader
from requests import get
from bs4 import BeautifulSoup
from sys import argv, stdout
from time import time
from shutil import get_terminal_size
strip = lambda str: str[:min(str.find('?'), str.find('/'))] if '?' in str or '/' in str else str
def download(id, title):
try:
# dl_url = f"https://drive.google.com/uc?export=download&id={id}"
dl_url = f"https://drive.usercontent.google.com/download?id={id}&export=download&confirm=t"
print(f"Downloading {title}")
response = get(dl_url, stream=True)
total_size = int(response.headers.get('content-length', 0))
block_size = 1024 # 1 Kibibyte
downloaded = 0
start_time = time()
columns = get_terminal_size().columns - 25
with open(title, 'wb') as f:
for data in response.iter_content(block_size):
progress = downloaded / total_size
percentage = progress * 100
elapsed_time = time() - start_time
speed = downloaded / elapsed_time if elapsed_time > 0 else 0
for unit in ['Byte', 'KB','MB','GB']:
if speed < 1024.0:
str_speed = f"{speed:.2f} {unit}ps"
break
speed /= 1024.0
filled = ("=" * (int(progress * columns) - 1)) + ">"
space = " " * int((1-progress) * columns)
f.write(data)
downloaded += len(data)
# Print progress bar
stdout.write(f"\r{percentage:.2f}% [{filled}{space}] {str_speed} ")
stdout.flush()
print("\nDownload complete!")
except KeyboardInterrupt:
print("\nDownload cancelled!")
except Exception as e:
print(e)
def file(url):
try:
response = get(url).text
soup = BeautifulSoup(response, "html.parser")
title = soup.find("meta", attrs={"property": "og:title"}).get("content")
view_url = soup.find("meta", attrs={"property": "og:url"}).get("content")
last = view_url.replace("https://drive.google.com/file/d/", "")
id = strip(last)
download(id, title)
except Exception as e:
print(e)
def folder(url):
try:
response = get(url).text
soup = BeautifulSoup(response, "html.parser")
divs = soup.find_all("div", attrs={'data-target' : True})
for div in divs:
id = div.get("data-id")
title_div = div.select_one("div > div > div > div:nth-of-type(2) > div > div")
if title_div.get("data-tooltip").startswith("Google Drive Folder"):
folder_url = f"https://drive.google.com/drive/folders/{id}"
folder(folder_url)
title = title_div.getText().strip()
download(id, title)
except Exception as e:
print(e)
def main():
url = argv[1] if len(argv) > 1 else input("Enter google drive url: ")
if "https://drive.google.com/" not in url:
print("Not a google drive url")
return
if "folder" in url:
folder(url)
if "file" in url:
file(url)
# dl_url = ""
# form = soup.find("form")
# action = form.get("action")
# inputs = form.find_all("input")
# dl_url += f"{action}?"
# for input in inputs:
# name = input.get("name")
# if name:
# value = input.get("value")
# dl_url += f"{name}={value}&"
# dl_url = dl_url.rstrip("&")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment