Last active
May 26, 2023 13:17
-
-
Save svandragt/45a57e2677d69af6ac85ee12b932f5a4 to your computer and use it in GitHub Desktop.
Clones all gists for a user. NOTE: change the user to you
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
#!/usr/bin/env python3 | |
import json | |
import os | |
import re | |
import requests | |
import subprocess | |
import unicodedata | |
# UPDATE THESE | |
username = "svandragt" | |
token = os.environ['GITHUB_TOKEN'] | |
# ------------------------------------- | |
def run(command): | |
# Execute the command and capture the output | |
result = subprocess.run(command, shell=True, capture_output=True, text=True) | |
# Check the command's exit status | |
if result.returncode == 0: | |
# Print the output | |
return result.stdout | |
else: | |
raise ValueError(result.stderr) | |
def slugify(value, allow_unicode=False): | |
""" | |
Taken from https://github.com/django/django/blob/master/django/utils/text.py | |
Convert to ASCII if 'allow_unicode' is False. Convert spaces or repeated | |
dashes to single dashes. Remove characters that aren't alphanumerics, | |
underscores, or hyphens. Convert to lowercase. Also strip leading and | |
trailing whitespace, dashes, and underscores. | |
""" | |
value = str(value) | |
if allow_unicode: | |
value = unicodedata.normalize('NFKC', value) | |
else: | |
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii') | |
value = re.sub(r'[^\w\s-]', '-', value.lower()) | |
return re.sub(r'[-\s]+', '-', value).strip('-_') | |
def json_gists(username, token): | |
url = f"https://api.github.com/users/{username}/gists" | |
headers = { | |
"Authorization": f"Token {token}", | |
"Accept": "application/vnd.github.v3+json" | |
} | |
response = requests.get(url, headers=headers) | |
if not response.status_code == 200: | |
raise ValueError(response.status_code) | |
gists = response.json() | |
return gists | |
gists = json_gists(username,token) | |
for gist in gists: | |
first_file = next(iter(gist['files'])) | |
gist_id = gist['id'] | |
dir_name = slugify(f"{first_file}-{gist_id}") | |
with open(f"{dir_name}.json", 'w') as f: | |
json.dump(gist, f) | |
if os.path.isdir(dir_name): | |
print(f"UPDATE: {dir_name}") | |
cwd = os.getcwd() | |
os.chdir(dir_name) | |
run('git pull') | |
os.chdir(cwd) | |
continue | |
git_url = gist['git_pull_url'] | |
git_cmd = f"git clone {git_url} {dir_name}" | |
print(git_cmd) | |
run(git_cmd) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For some reason I can't get output from the git clone command,
print( run( "ls" ) )
outputs correctly.