Created
February 3, 2023 03:33
-
-
Save jlomako/455242bfcc2932cde31202ab4a1f2822 to your computer and use it in GitHub Desktop.
restore all files from github directory
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
# Script that uses github API to restore all files from directory | |
# Get API token from Settings > Developer Setting > Personal Access Token > Fine Grained Token | |
import requests | |
from datetime import datetime | |
headers = { | |
"Authorization": "<TOKEN>" | |
} | |
response = requests.get("https://api.github.com/repos/<USERNAME>/<REPOSITORY>/commits", headers=headers) | |
data = response.json() | |
shas = [commit["sha"] for commit in data] | |
for commit in data: | |
sha = commit["sha"] | |
url = f"https://raw.githubusercontent.com/<USERNAME>/<REPOSITORY>/{sha}/data/<FILENAME>.csv" | |
response = requests.get(url) | |
creation_datetime = datetime.strptime(commit["commit"]["committer"]["date"], '%Y-%m-%dT%H:%M:%SZ').strftime('%Y%m%d_%H%M%S') | |
filename = f"<FILENAME>_{creation_datetime}.csv" | |
with open(filename, "wb") as file: | |
file.write(response.content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment