Created
September 14, 2020 09:06
-
-
Save lokesh1729/5c27da84151b285c25d1c0d9f418e55c to your computer and use it in GitHub Desktop.
Search Github Repos
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 requests | |
import sys | |
import webbrowser | |
GITHUB_URL = "https://api.github.com/search/repositories?q={query_string}&sort=stars" | |
class CustomException(Exception): | |
pass | |
def get_repo_link(repo_name, language): | |
query_string = "+".join(repo_name.split()) | |
if language: | |
query_string = "+".join([repo_name, "language:%s" % language]) | |
try: | |
response = requests.get(GITHUB_URL.format(query_string=query_string)).json() | |
return response["items"][0]['html_url'] | |
except requests.exceptions.BaseHTTPError: | |
raise CustomException("Error in reaching github some error occurred") | |
def main(repo_name, language): | |
try: | |
repo_link = get_repo_link(repo_name, language) | |
webbrowser.open_new_tab(repo_link) | |
except webbrowser.Error: | |
raise CustomException("Error in opening browser") | |
if __name__ == "__main__": | |
repo_name = sys.argv[1] | |
language = None | |
if len(sys.argv) == 3: | |
language = sys.argv[2] | |
main(repo_name, language) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment