|
| 1 | +# _____________________________________ |
| 2 | +# https://github.com/kenysdev |
| 3 | +# 2024 - Python |
| 4 | +# _____________________________________ |
| 5 | +# 45 GITHUB OCTOVERSE |
| 6 | +# ------------------------------------ |
| 7 | + |
| 8 | +""" |
| 9 | + * EJERCICIO: |
| 10 | + * GitHub ha publicado el Octoverse 2024, el informe |
| 11 | + * anual del estado de la plataforma: |
| 12 | + * https://octoverse.github.com |
| 13 | + * |
| 14 | + * Utilizando el API de GitHub, crea un informe asociado |
| 15 | + * a un usuario concreto. |
| 16 | + * |
| 17 | + * - Se debe poder definir el nombre del usuario |
| 18 | + * sobre el que se va a generar el informe. |
| 19 | + * |
| 20 | + * - Crea un informe de usuario basándote en las 5 métricas |
| 21 | + * que tú quieras, utilizando la informración que te |
| 22 | + * proporciona GitHub. Por ejemplo: |
| 23 | + * - Lenguaje más utilizado |
| 24 | + * - Cantidad de repositorios |
| 25 | + * - Seguidores/Seguidos |
| 26 | + * - Stars/forks |
| 27 | + * - Contribuciones |
| 28 | + * (lo que se te ocurra) |
| 29 | +""" |
| 30 | + |
| 31 | +import requests |
| 32 | +from collections import Counter |
| 33 | +import textwrap |
| 34 | + |
| 35 | +class GitHubApi: |
| 36 | + def __init__(self, user_name: str) -> None: |
| 37 | + # El límite es de 60 solicitudes por hora |
| 38 | + url: str = f"https://api.github.com/users/{user_name}" |
| 39 | + self.__user_data: dict = self.__get_json(url) |
| 40 | + |
| 41 | + def __get_json(self, url: str) -> dict: |
| 42 | + try: |
| 43 | + user_data: dict = requests.get(url).json() |
| 44 | + return user_data |
| 45 | + except requests.exceptions.RequestException as err: |
| 46 | + print(f"Error: {err}") |
| 47 | + return {} |
| 48 | + |
| 49 | + def __verify_status(self) -> bool: |
| 50 | + if self.__user_data.get("status") == "404": |
| 51 | + print(f"Usuario '{user_name}' no encontrado.") |
| 52 | + return False |
| 53 | + return True |
| 54 | + |
| 55 | + def __get_repo_info(self, dt: dict) -> str: |
| 56 | + return textwrap.dedent(f""" |
| 57 | + Lang: {dt.get("full_name", "Desconocido")}") |
| 58 | + Repo: {dt.get("language")}") |
| 59 | + Stars: {dt.get("stargazers_count", 0)}") |
| 60 | + Forks: {dt.get("forks_count", 0)}")""" |
| 61 | + ) |
| 62 | + |
| 63 | + def print_basic_info(self) -> None: |
| 64 | + if not self.__verify_status: |
| 65 | + return |
| 66 | + |
| 67 | + dt: dict = self.__user_data |
| 68 | + print(textwrap.dedent(f""" |
| 69 | + ------------------------------------------- |
| 70 | + Nombre: {dt.get("name", "Desconocido")} |
| 71 | + Creación: {dt.get("created_at", "Desconocido")} |
| 72 | + Repos: {dt.get("public_repos", 0)} |
| 73 | + Gists: {dt.get("public_gists", 0)} |
| 74 | + Seguidores: {dt.get("followers", 0)} |
| 75 | + Seguidos: {dt.get("following", 0)} |
| 76 | + -------------------------------------------""" |
| 77 | + )) |
| 78 | + |
| 79 | + def print_repos_info(self): |
| 80 | + if not self.__verify_status: |
| 81 | + return |
| 82 | + |
| 83 | + url: str = self.__user_data.get("repos_url", "None") |
| 84 | + repos_data = self.__get_json(url) |
| 85 | + languages = Counter() |
| 86 | + |
| 87 | + print("Repositorios publicos:") |
| 88 | + for repo in repos_data: |
| 89 | + language = repo.get("language") |
| 90 | + print(self.__get_repo_info(repo)) |
| 91 | + if language: |
| 92 | + languages[language] += 1 |
| 93 | + |
| 94 | + most_c, count = languages.most_common(1)[0] |
| 95 | + print(f"________\nTotal de repositorios: '{len(repos_data)}'") |
| 96 | + print(f"El lenguaje más utilizado: '{most_c}'({count})") |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + print("Informe sobre los datos del usuario en GitHub") |
| 100 | + user_name: str = input("Usuario: ") |
| 101 | + github = GitHubApi(user_name) |
| 102 | + github.print_repos_info() |
| 103 | + github.print_basic_info() |
0 commit comments