Created
September 6, 2024 01:12
-
-
Save alexishida/dd52cc785eb5634b3f730d7681a793c6 to your computer and use it in GitHub Desktop.
Python script for backing up gists
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 requests | |
import sys | |
import re | |
from subprocess import call | |
user = 'alexishida' | |
num_pages = 10 | |
# Função para sanitizar o nome da pasta, removendo caracteres inválidos | |
def sanitize_folder_name(name): | |
# Remove caracteres não permitidos em nomes de pastas e substitui por '_' | |
name = re.sub(r'[<>:"/\\|?*]', '_', name) | |
# Remove ponto final no final do nome da pasta | |
name = re.sub(r'\.$', '', name) | |
# Remove espaços extras no início e no final | |
return name.strip() | |
# Iterar sobre o número de páginas | |
for page in range(1, num_pages + 1): | |
r = requests.get('https://api.github.com/users/{0}/gists?page={1}'.format(user, page)) | |
if r.status_code != 200: | |
print(f'Erro ao buscar gists na página {page}: {r.status_code}') | |
continue | |
# Iterar sobre os gists de cada página | |
for i in r.json(): | |
folder_name = i['description'][0:255] if i['description'] else i['id'] | |
sanitized_folder = sanitize_folder_name(folder_name) | |
call(['git', 'clone', i['git_pull_url'], sanitized_folder]) | |
description_file = './{0}/description.txt'.format(sanitized_folder) | |
with open(description_file, 'w') as f: | |
f.write('{0}\n'.format(i['description'])) | |
print(f'Processo concluído para {num_pages} páginas.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment