|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +A small command line Python program that uses the GitHub search API to list |
| 5 | +the top projects by language, based on stars. |
| 6 | +
|
| 7 | +GitHub Search API documentation: https://developer.github.com/v3/search/ |
| 8 | +
|
| 9 | +Requests to this endpoint are rate limited to 10 requests per |
| 10 | +minute per IP address. |
| 11 | +""" |
| 12 | + |
| 13 | +import sys |
| 14 | +import requests |
| 15 | + |
| 16 | + |
| 17 | +GITHUB_API_URL = "https://api.github.com/search/repositories" |
| 18 | + |
| 19 | + |
| 20 | +class GitHubApiException(Exception): |
| 21 | + |
| 22 | + def __init__(self, status_code): |
| 23 | + if status_code == 403: |
| 24 | + message = "Rate limit reached. Please wait a minute and try again." |
| 25 | + else: |
| 26 | + message = f"HTTP Status Code was: {status_code}." |
| 27 | + |
| 28 | + super().__init__("A GitHub API Error Occurred: " + message) |
| 29 | + |
| 30 | + |
| 31 | +class GitHubRepo: |
| 32 | + """ |
| 33 | + A class used to represent a single GitHub Repository. |
| 34 | + """ |
| 35 | + |
| 36 | + def __init__(self, name, language, num_stars): |
| 37 | + self.name = name |
| 38 | + self.language = language |
| 39 | + self.num_stars = num_stars |
| 40 | + |
| 41 | + def __str__(self): |
| 42 | + return f"-> {self.name} is a {self.language} repo with {self.num_stars} stars." |
| 43 | + |
| 44 | + def __repr__(self): |
| 45 | + return f'GitHubRepo({self.name}, {self.language}, {self.num_stars})' |
| 46 | + |
| 47 | + |
| 48 | +def create_query(languages, min_stars): |
| 49 | + """ |
| 50 | + Create the query string for the GitHub search API, |
| 51 | + based on the minimum amount of stars for a project, and |
| 52 | + the provided programming languages. |
| 53 | + """ |
| 54 | + # Notice we are calling .strip() on each language, to clear it of leading |
| 55 | + # and trailing whitespace |
| 56 | + query = " ".join(f"language:{language.strip()}" for language in languages) |
| 57 | + query = query + f" stars:>{min_stars}" |
| 58 | + return query |
| 59 | + |
| 60 | + |
| 61 | +def repos_with_most_stars(languages, min_stars=50000, sort="stars", order="desc"): |
| 62 | + query = create_query(languages, min_stars) |
| 63 | + parameters = {"q": query, "sort": sort, "order": order} |
| 64 | + response = requests.get(GITHUB_API_URL, params=parameters) |
| 65 | + |
| 66 | + if response.status_code != 200: |
| 67 | + raise GitHubApiException(response.status_code) |
| 68 | + |
| 69 | + response_json = response.json() |
| 70 | + items = response_json["items"] |
| 71 | + return [GitHubRepo(item["name"], item["language"], item["stargazers_count"]) for item in items] |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == "__main__": |
| 75 | + # Accept an optional argument for minimum number of stars from the command line |
| 76 | + # $ ./gh_api 100000 # means an input of 100,000 minimum stars. |
| 77 | + script_arguments = sys.argv |
| 78 | + min_stars = 50000 |
| 79 | + |
| 80 | + if len(script_arguments) >= 2: |
| 81 | + try: |
| 82 | + min_stars = int(script_arguments[1]) |
| 83 | + except ValueError: |
| 84 | + sys.exit("Error: Command line argument must be a valid number.") |
| 85 | + |
| 86 | + # Accept the list of languages from the user, or provide a default list. |
| 87 | + languages = input( |
| 88 | + "Enter a comma separated list of programming languages (or press ENTER for defaults): " |
| 89 | + ).strip() |
| 90 | + if not languages: |
| 91 | + languages = ["python", "javascript", "ruby"] |
| 92 | + else: |
| 93 | + languages = languages.split(",") |
| 94 | + |
| 95 | + # Get the results |
| 96 | + result_repos = repos_with_most_stars(languages=languages, min_stars=min_stars) |
| 97 | + if not result_repos: |
| 98 | + print("No Results Found.") |
| 99 | + else: |
| 100 | + for repo in result_repos: |
| 101 | + print(repo) |
0 commit comments