Created
June 6, 2024 04:44
-
-
Save t510599/4c1dca865a7fe1921f1d9cc2c10b1589 to your computer and use it in GitHub Desktop.
Dump video in Google Drive folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from urllib.parse import parse_qs | |
import requests as r | |
from bs4 import BeautifulSoup as Soup | |
import concurrent.futures | |
HEADERS = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0"} | |
def get_video_info(title, docid, filetype, target_fmt="22"): | |
res = r.get("https://drive.google.com/get_video_info", params={"docid": docid}, headers=HEADERS) | |
cookie_drive_stream = res.cookies["DRIVE_STREAM"] | |
for stream in parse_qs(res.text)["url_encoded_fmt_stream_map"][0].split(","): | |
stream = parse_qs(stream) | |
fmt, url = stream["itag"][0], stream["url"][0] | |
if fmt == target_fmt: | |
return (url, cookie_drive_stream, f"{title}.{filetype.split('/')[1]}") | |
TITLE = 2 | |
DOCID = 0 | |
FILETYPE = 3 | |
folder = "" | |
res = r.get(f"https://drive.google.com/drive/folders/{folder}", headers=HEADERS) | |
soup = Soup(res.text, features="html.parser") | |
for script in soup.select("script"): | |
if script.get_text().startswith("window['_DRIVE_ivd']"): | |
break | |
data = json.loads(eval(script.get_text()[len("window['_DRIVE_ivd'] = "):-len(";if (window['_DRIVE_ivdc']) {window['_DRIVE_ivdc']();}")])) | |
with concurrent.futures.ThreadPoolExecutor() as executor: | |
futures = [] | |
for file in data[0]: | |
title, docid, filetype = file[TITLE], file[DOCID], file[FILETYPE] | |
if not filetype.startswith("video/"): | |
continue | |
futures.append(executor.submit(get_video_info, title, docid, filetype)) | |
for result in concurrent.futures.as_completed(futures): | |
url, cookie_drive_stream, filename = result.result() | |
# generate script for aria2c | |
print("\n ".join((url, f'header=Cookie:DRIVE_STREAM={cookie_drive_stream}', f"out=out/{filename}"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment