Created
October 6, 2022 06:27
-
-
Save hrz6976/afaf4f6af4608f42b3721cef5e26260a to your computer and use it in GitHub Desktop.
Download a repository as a zip archive from anonymous.4open.science
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 os | |
import zipfile | |
import requests | |
repo_name = input("Enter 4open.science repository name: ") | |
api_url = f"https://anonymous.4open.science/api/repo/{repo_name}/files/" | |
r = requests.get(api_url) | |
r.raise_for_status() | |
r_j = r.json() | |
file_list = [] | |
def traverse_json(json: dict, path: str): | |
for k, v in json.items(): | |
if "size" in v: | |
file_list.append(path + "/" + k) | |
else: | |
traverse_json(v, path + "/" + k) | |
traverse_json(r_j, "") | |
print(f"{file_list} files in the repository {repo_name}, first 5: {file_list[:5]}") | |
zip_path = os.path.expanduser(input("Enter zip file path: ")) | |
with zipfile.ZipFile(zip_path, "w") as zip_file: | |
for i, file in enumerate(file_list): | |
r = requests.get(f"https://anonymous.4open.science/api/repo/{repo_name}/file{file}") | |
r.raise_for_status() | |
print(f"({i+1}/{len(file_list)}) Downloading {file[1:]}") | |
zip_file.writestr(file[1:], r.content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This worked for me, thanks so much!