pick a language i guess?

This commit is contained in:
Tristan Smith 2024-03-20 21:25:02 -04:00
parent ca73e2dc54
commit db8fcdae42

52
main.py
View file

@ -1,21 +1,49 @@
from re import search
import requests import requests
import webbrowser import webbrowser
import random # Import the random module import random # Import the random module
def get_random_repo(): def get_random_repo(language=None):
url = "https://api.github.com/repositories" # base url for github search api
response = requests.get(url) url = "https://api.github.com/search/repositories"
repos = response.json()
# Pick a random repo from the list of repos
random_repo = random.choice(repos)
return random_repo
# 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(): def main():
repo = get_random_repo() # prompt user to enter a programming language (optional)
print(repo['html_url']) # Print the URL of the random repo language = input("Enter a programming language: ").strip()
# Ensure the returned link opens in the default browser repo = get_random_repo(language=language if language else None)
webbrowser.open(repo['html_url']) 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__": if __name__ == "__main__":
main() main()