Skip to content

Instantly share code, notes, and snippets.

@lxdlam
Created May 31, 2024 04:06
Show Gist options
  • Save lxdlam/1be92fa999f8c3f0c4383d64bcacf567 to your computer and use it in GitHub Desktop.
Save lxdlam/1be92fa999f8c3f0c4383d64bcacf567 to your computer and use it in GitHub Desktop.
from os import walk
from urllib.parse import urlparse
from playwright.sync_api import sync_playwright
from time import sleep
TEMPLATE = "https://drive.usercontent.google.com/download?id={share_id}&export=download"
if __name__ == "__main__":
with sync_playwright() as p:
browser = p.firefox.launch()
for _, _, files in walk("./"):
for file in files:
if file.endswith(".txt"):
with open(file, "r") as f:
for line in f:
url = line.strip()
if not url:
print(f"invalid url: {url}")
continue
share_id = urlparse(url).path.split("/")[-2]
page = browser.new_page()
try:
with page.expect_download() as download_info:
try:
page.goto(TEMPLATE.format(share_id=share_id))
page.get_by_text("Download anyway").click()
except:
pass
d = download_info.value
d.save_as(
f"./google_drive/{share_id}/{d.suggested_filename}"
)
print(f"saved: {share_id}/{d.suggested_filename}")
except Exception as e:
print(f"error: {e}")
sleep(1)
browser.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment