gitdotrandom/main.py

50 lines
1.4 KiB
Python
Raw Normal View History

2024-03-21 01:25:02 +00:00
from re import search
2024-03-21 01:00:51 +00:00
import requests
import webbrowser
import random # Import the random module
2024-03-21 01:25:02 +00:00
def get_random_repo(language=None):
# base url for github search api
url = "https://api.github.com/search/repositories"
2024-03-21 01:00:51 +00:00
2024-03-21 01:25:02 +00:00
# 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
2024-03-21 01:00:51 +00:00
def main():
2024-03-21 01:25:02 +00:00
# prompt user to enter a programming language (optional)
language = input("Enter a programming language (leave blank for random): ").strip()
2024-03-21 01:00:51 +00:00
2024-03-21 01:25:02 +00:00
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")
2024-03-21 01:00:51 +00:00
if __name__ == "__main__":
main()