Created
August 1, 2024 18:07
-
-
Save hxhcreate/f11eb3e7a7c9d5b25a1d08764982c458 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 os, re, sys, time | |
| import requests | |
| import gdown | |
| # Solve the limit of 50 on the maximum number of files downloaded in single folder on Google Drive. You can use this script to recursively download a folder, including all the files and subfolders on Google Drive. | |
| # based on https://gist.github.com/DaniDipp/744b52adb341e41fdf871346a59e442c. thanks to @DaniDipp | |
| def recursive_gdown(folder_id, current_path=''): | |
| url = f"https://drive.google.com/embeddedfolderview?id={folder_id}" | |
| response = requests.get(url) | |
| if response.status_code != 200: | |
| print(f"Error {response.status_code}: {response.content}") | |
| return | |
| data = str(response.content) | |
| folder_title = re.search(r"<title>(.*)</title>", data).group(1) | |
| # Pattern to find files and folders | |
| file_pattern = r"https://drive\.google\.com/file/d/([-\w]{25,})/view\?usp=drive_web" | |
| folder_pattern = r"https://drive\.google\.com/drive/folders/([-\w]{25,})" | |
| files = re.findall(file_pattern, data) | |
| folders = re.findall(folder_pattern, data) | |
| print(f"Found {len(files)} files and {len(folders)} folders") | |
| # Create directory for current folder if it doesn't exist | |
| path = os.path.join(current_path, folder_title) | |
| os.makedirs(path, exist_ok=True) | |
| print(f"Processing directory: {path}") | |
| # Download files | |
| for i, file_id in enumerate(files): | |
| file_directory = f"{path}/" | |
| print(f"Downloading File {i + 1}/{len(files)}: {file_id}") | |
| while True: | |
| try: | |
| gdown.download(f"https://drive.google.com/uc?id={file_id}", file_directory, quiet=False, resume=True) | |
| break | |
| except gdown.exceptions.FileURLRetrievalError: | |
| print("Failed to download file. Retrying in 10 mins...") | |
| time.sleep(10 * 60) | |
| # Recursively process each sub-folder | |
| for folder_id in folders: | |
| recursive_gdown(folder_id, path) | |
| if __name__ == "__main__": | |
| if(len(sys.argv) < 2): | |
| print(f"Usage: ./{sys.argv[0]} <gdrive-folder-id>") | |
| exit(1) | |
| folder_id = sys.argv[1] | |
| if(not bool(re.match(r"^[-\w]{25,}$", folder_id))): | |
| print(f"Invalid id: {folder_id}") | |
| exit(1) | |
| recursive_gdown(folder_id, "./gdown") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
../support/python-3.8.10-embed-win32/python ../gdown_folder.py 1ZZHWbZg1rEeVNzDD8BbxuYABa4aUNQrh
Downloading...
From: https://drive.google.com/uc?id=1fKV-7rTBwA2MtbZilAuy3RRdcziyKBLO
To: C:\Users\Pinon\Desktop\NSMBW_Mod_ISO_Builder\test\gdown\NewerLAA ver.1.20\NewerLAA
Found 0 files and 2 folders
Processing directory: ./gdown\NewerLAA ver.1.20
Found 2 files and 16 folders
Processing directory: ./gdown\NewerLAA ver.1.20\NewerLAA
Downloading File 1/2: 1fKV-7rTBwA2MtbZilAuy3RRdcziyKBLO
100%|▒i▒i▒i▒i▒i▒i▒i▒i▒i▒i| 2.41k/2.41k [00:00<?, ?B/s]
Traceback (most recent call last):
File "../gdown_folder.py", line 54, in
recursive_gdown(folder_id, "./gdown")
File "../gdown_folder.py", line 44, in recursive_gdown
recursive_gdown(folder_id, path)
File "../gdown_folder.py", line 36, in recursive_gdown
gdown.download(f"https://drive.google.com/uc?id={file_id}", file_directory, quiet=False, resume=True)
File "C:\Users\Pinon\Desktop\NSMBW_Mod_ISO_Builder\support\python-3.8.10-embed-win32\lib\site-packages\gdown\download.py", line 381, in download
shutil.move(tmp_file, output)
File "shutil.py", line 789, in move
shutil.Error: Destination path './gdown\NewerLAA ver.1.20\NewerLAA/gk8_edya.part' already exists