diff --git a/main.py b/main.py index 4f05df9..39c58cd 100644 --- a/main.py +++ b/main.py @@ -1,21 +1,49 @@ +from re import search import requests import webbrowser import random # Import the random module -def get_random_repo(): - url = "https://api.github.com/repositories" - response = requests.get(url) - repos = response.json() - # Pick a random repo from the list of repos - random_repo = random.choice(repos) - return random_repo +def get_random_repo(language=None): + # base url for github search api + url = "https://api.github.com/search/repositories" + # define the search parameters + query = "language:{}".format(language) if language else 'stars:>1' + sort = "stars" + order = "desc" + + # parameters for the search query + params = { + 'q': query, + 'sort': sort, + 'order': order + } + + response = requests.get(url, params=params) + + # check if the request was successful + if response.status_code == 200: + search_results = response.json()['items'] + if search_results: + random_repo = random.choice(search_results) + return random_repo + else: + return None + else: + print(f"Failed to fetch repos. Status code: {response.status_code}") + return None + def main(): - repo = get_random_repo() - print(repo['html_url']) # Print the URL of the random repo + # prompt user to enter a programming language (optional) + language = input("Enter a programming language: ").strip() - # Ensure the returned link opens in the default browser - webbrowser.open(repo['html_url']) + repo = get_random_repo(language=language if language else None) + if repo: + print(repo['html_url']) # print the url of the random repo + # open the url in the default web browser + webbrowser.open(repo['html_url']) + else: + print("No repos found") if __name__ == "__main__": - main() + main() \ No newline at end of file