-
-
Save datavudeja/727e8c8b2e59f7d35c58ff9f8df6a345 to your computer and use it in GitHub Desktop.
Github repo downloader
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
| import os | |
| import subprocess | |
| import requests | |
| from scrapy import Selector | |
| from rich import print as rprint | |
| from rich.prompt import Prompt | |
| import click | |
| from urllib.parse import urljoin | |
| @click.command() | |
| @click.option('--all', is_flag=True, help='Download all repositories without asking.') | |
| def main(all): | |
| rprint("[bold cyan]GitHub Repository Downloader[/bold cyan]") | |
| rprint("=" * 30) | |
| home_url = "https://github.com/" | |
| # Prompt user for repository owner's name | |
| repo_owner = Prompt.ask("[bright_red]Enter the repository owner's name for the repositories you want to download[/bright_red]") | |
| # Construct URL for the owner's repositories | |
| all_repos_url = f"https://github.com/{repo_owner}?tab=repositories" | |
| # Make a request to fetch the repository data | |
| data = requests.get(url=all_repos_url) | |
| response = Selector(text=data.text) | |
| repos_list = response.css("a[itemprop='name codeRepository'] ::attr(href)").getall() | |
| while response.css("a.next_page"): | |
| data = requests.get(url=urljoin(home_url, response.css("a.next_page ::attr(href)").get())) | |
| response = Selector(text=data.text) | |
| repos_list.extend(response.css("a[itemprop='name codeRepository'] ::attr(href)").getall()) | |
| rprint(f"There are {len(repos_list)} repositories in {repo_owner} account.") | |
| # Extract repository URLs | |
| repo_names = [repo.split("/")[-1] for repo in repos_list] | |
| repos_urls = [f"https://github.com/{repo_owner}/{repo_name}.git" for repo_name in repo_names] | |
| # Process each repository URL | |
| for repo_url in repos_urls: | |
| repo_name = repo_url.split("/")[-1][:-4] # Remove '.git' extension | |
| # Check if repository already exists | |
| if os.path.exists(repo_name): | |
| rprint(f"Repository '[cyan]{repo_name}[/cyan]' already exists. [dim]Skipping...[/dim]") | |
| elif all or Prompt.ask(f"[bright_red]Do you want to download repository '{repo_name}'? (y/n)[/bright_red]").lower() == 'y': | |
| # Clone the repository using git | |
| subprocess.run(["git", "clone", repo_url]) | |
| rprint(f"Repository '[cyan]{repo_name}[/cyan]' downloaded [bold green]successfully[/bold green].") | |
| else: | |
| rprint(f"Repository '[cyan]{repo_name}[/cyan]' [dim]skipped[/dim].") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment