Created
January 6, 2023 03:26
-
-
Save mov-ebx/6a8f95a4aa0d68e96e09d16990119cb1 to your computer and use it in GitHub Desktop.
A basic Python script which returns all GitHub repos and their forks based on a search query. Not my finest code, I wrote it in about 1m.
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
# written by mov-ebx on github | |
import requests | |
def scan(query): | |
def search_repositories(query): | |
url = f'https://api.github.com/search/repositories?q={query.replace(" ", "+")}' | |
response = requests.get(url) | |
if response.status_code != 200: | |
raise Exception(f'Failed to search repositories: {response.status_code}') | |
return response.json()['items'] | |
def get_forks(full_name): | |
owner, repo = full_name.split('/')[0], full_name.split('/')[1] | |
url = f'https://api.github.com/repos/{owner}/{repo}/forks' | |
response = requests.get(url) | |
if response.status_code != 200: | |
raise Exception(f'Failed to get forks: {response.status_code}') | |
return response.json() | |
def append_with_forks(repo): | |
repos.append(repo['full_name']) | |
if repo['forks_count'] > 1: | |
for fork in get_forks(repo['full_name']): | |
append_with_forks(fork) | |
to_scan, repos = search_repositories(query), [] | |
for repo in to_scan: | |
append_with_forks(repo) | |
return [*set(repos)] | |
if __name__ == '__main__': | |
print(scan(input('repo you want to search: '))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment