Created
November 9, 2023 10:29
-
-
Save encima/336e077be757348c5c9c7a0535fc62f2 to your computer and use it in GitHub Desktop.
clone-repo-from-tags
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 subprocess | |
import os | |
# Personal access token from GitHub | |
# Replace 'your_token_here' with your actual GitHub personal access token. | |
GITHUB_TOKEN = os.getenv('GH_TOKEN') | |
# Directory where you want to clone the repositories | |
CLONE_DIR = os.getenv('CLONE_DIR', './') | |
# GitHub API URL for searching repositories | |
SEARCH_API_URL = 'https://api.github.com/search/repositories' | |
# The topic you're interested in | |
TOPIC = 'hnl2023' | |
def search_repositories(topic): | |
"""Search for GitHub repositories by topic.""" | |
headers = { | |
'Authorization': f'token {GITHUB_TOKEN}', | |
'Accept': 'application/vnd.github.v3+json', | |
} | |
params = { | |
'q': f'topic:{topic}' | |
} | |
response = requests.get(SEARCH_API_URL, headers=headers, params=params) | |
if response.status_code == 200: | |
return response.json()['items'] | |
else: | |
print(f'Failed to search repositories: {response.content}') | |
return [] | |
def clone_repository(repo_ssh_url): | |
"""Clone a GitHub repository using its SSH URL.""" | |
try: | |
subprocess.run(['git', 'clone', repo_ssh_url], check=True, cwd=CLONE_DIR) | |
except subprocess.CalledProcessError as e: | |
print(f'Failed to clone repository: {e}') | |
def main(): | |
"""Main function to search and clone repositories.""" | |
if not os.path.exists(CLONE_DIR): | |
os.makedirs(CLONE_DIR) | |
repositories = search_repositories(TOPIC) | |
for repo in repositories: | |
repo_ssh_url = repo['ssh_url'] | |
print(f'Cloning {repo["full_name"]}') | |
clone_repository(repo_ssh_url) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment