|
| 1 | +""" |
| 2 | +/* |
| 3 | + * EJERCICIO: |
| 4 | + * ¡Rubius tiene su propia skin en Fortnite! |
| 5 | + * Y va a organizar una competición para celebrarlo. |
| 6 | + * Esta es la lista de participantes: |
| 7 | + * https://x.com/Rubiu5/status/1840161450154692876 |
| 8 | + * |
| 9 | + * Desarrolla un programa que obtenga el número de seguidores en |
| 10 | + * Twitch de cada participante, la fecha de creación de la cuenta |
| 11 | + * y ordene los resultados en dos listados. |
| 12 | + * - Usa el API de Twitch: https://dev.twitch.tv/docs/api/reference |
| 13 | + * (NO subas las credenciales de autenticación) |
| 14 | + * - Crea un ranking por número de seguidores y por antigüedad. |
| 15 | + * - Si algún participante no tiene usuario en Twitch, debe reflejarlo. |
| 16 | + */ |
| 17 | +""" |
| 18 | +import requests |
| 19 | +import os |
| 20 | +from dotenv import load_dotenv |
| 21 | + |
| 22 | +load_dotenv() |
| 23 | + |
| 24 | +CLIENT_ID = os.getenv("TWITCH_CLIENT_ID") |
| 25 | +CLIENT_SECRET = os.getenv("TWITCH_CLIENT_SECRET") |
| 26 | + |
| 27 | +""" |
| 28 | +curl -X POST 'https://id.twitch.tv/oauth2/token' \ |
| 29 | +-H 'Content-Type: application/x-www-form-urlencoded' \ |
| 30 | +-d 'client_id=<your client id goes here>&client_secret=<your client secret goes here>&grant_type=client_credentials' |
| 31 | +""" |
| 32 | +def get_access_token(client_id: str, client_secret: str) -> str: |
| 33 | + url = "https://id.twitch.tv/oauth2/token" |
| 34 | + data = { |
| 35 | + "client_id": client_id, |
| 36 | + "client_secret": client_secret, |
| 37 | + "grant_type": "client_credentials" |
| 38 | + } |
| 39 | + |
| 40 | + response = requests.post(url=url, data=data) |
| 41 | + |
| 42 | + #Verificar si la respuesta es correcta, se puede hacer con response.raise_for_status() también |
| 43 | + if response.status_code == 200: |
| 44 | + token_data = response.json() |
| 45 | + return token_data.get("access_token") |
| 46 | + else: |
| 47 | + print(f"Error: Unable to get access token. Status Code {response.status_code}.") |
| 48 | + return None |
| 49 | +""" |
| 50 | +curl -X GET 'https://api.twitch.tv/helix/users?id=141981764' \ |
| 51 | +-H 'Authorization: Bearer cfabdegwdoklmawdzdo98xt2fo512y' \ |
| 52 | +-H 'Client-Id: uo6dggojyb8d6soh92zknwmi5ej1q2' |
| 53 | +""" |
| 54 | +def get_user_info(token: str, client_id: str, login: str): |
| 55 | + url = "https://api.twitch.tv/helix/users" |
| 56 | + headers = { |
| 57 | + "Authorization": f"Bearer {token}", |
| 58 | + "Client-Id": client_id |
| 59 | + } |
| 60 | + params = {"login": login} |
| 61 | + |
| 62 | + response = requests.get(url=url, headers=headers, params=params) |
| 63 | + response.raise_for_status() |
| 64 | + data = response.json().get("data", []) #Si no hay data, devolver lista vacía, lo hace el ", [])" |
| 65 | + if not data: |
| 66 | + return None |
| 67 | + |
| 68 | + return data[0] |
| 69 | + |
| 70 | +""" |
| 71 | +curl -X GET 'https://api.twitch.tv/helix/channels/followers?broadcaster_id=123456' \ |
| 72 | +-H 'Authorization: Bearer kpvy3cjboyptmiacwr0c19hotn5s' \ |
| 73 | +-H 'Client-Id: hof5gwx0su6owfn0nyan9c87zr6t' |
| 74 | +""" |
| 75 | +def get_user_followers(token: str, client_id: str, user_id: str) -> int: |
| 76 | + url = "https://api.twitch.tv/helix/channels/followers" |
| 77 | + headers = { |
| 78 | + "Authorization": f"Bearer {token}", |
| 79 | + "Client-Id": client_id |
| 80 | + } |
| 81 | + params = {"broadcaster_id": user_id} |
| 82 | + response = requests.get(url=url, headers=headers, params=params) |
| 83 | + response.raise_for_status() |
| 84 | + return response.json().get("total", 0) |
| 85 | + |
| 86 | +users = [ |
| 87 | + "littleragergirl", "ache", "adricontreras4", "agustin51", "alexby11", "ampeterby7", "tvander", |
| 88 | + "arigameplays", "arigeli_", "auronplay", "axozer", "beniju03", "bycalitos", |
| 89 | + "byviruzz", "carreraaa", "celopan", "srcheeto", "crystalmolly", "darioemehache", |
| 90 | + "dheylo", "djmariio", "doble", "elvisayomastercard", "elyas360", "folagorlives", "thegrefg", |
| 91 | + "guanyar", "hika", "hiperop", "ibai", "ibelky_", "illojuan", "imantado", |
| 92 | + "irinaissaia", "jesskiu", "jopa", "jordiwild", "kenaivsouza", "mrkeroro10", |
| 93 | + "thekiddkeo95", "kikorivera", "knekro", "kokoop", "kronnozomberoficial", "leviathan", |
| 94 | + "litkillah", "lolalolita", "lolitofdez", "luh", "luzu", "mangel", "mayichi", |
| 95 | + "melo", "missasinfonia", "mixwell", "jaggerprincesa", "nategentile7", "nexxuz", |
| 96 | + "lakshartnia", "nilojeda", "nissaxter", "olliegamerz", "orslok", "outconsumer", "papigavitv", |
| 97 | + "paracetamor", "patica1999", "paulagonu", "pausenpaii", "perxitaa", "nosoyplex", |
| 98 | + "polispol1", "quackity", "recuerd0p", "reventxz", "rivers_gg", "robertpg", "roier", |
| 99 | + "ceuvebrokenheart", "rubius", "shadoune666", "silithur", "spok_sponha", "elspreen", "spursito", |
| 100 | + "bystaxx", "suzyroxx", "vicens", "vitu", "werlyb", "xavi", "xcry", "elxokas", |
| 101 | + "thezarcort", "zeling", "zormanworld", "mouredev" |
| 102 | +] |
| 103 | +users_data = [] |
| 104 | +not_found_users = [] |
| 105 | + |
| 106 | +TOKEN = get_access_token(CLIENT_ID, CLIENT_SECRET) |
| 107 | + |
| 108 | +for user_name in users: |
| 109 | + user = get_user_info(TOKEN, CLIENT_ID, user_name) |
| 110 | + if user is None: |
| 111 | + not_found_users.append(user_name) |
| 112 | + else: |
| 113 | + |
| 114 | + followers = get_user_followers(TOKEN, CLIENT_ID, user.get("id")) |
| 115 | + users_data.append({ |
| 116 | + "username": user_name, |
| 117 | + "created_at": user.get("created_at"), |
| 118 | + "followers": followers |
| 119 | + }) |
| 120 | + |
| 121 | +#Usuarios no encontrados |
| 122 | +print (not_found_users) |
| 123 | + |
| 124 | +#Ordenar por seguidores |
| 125 | +users_data.sort(key=lambda x: x["followers"], reverse=True) |
| 126 | + |
| 127 | +print("Ranking por seguidores:") |
| 128 | +for id, user, in enumerate(users_data): |
| 129 | + print(f"{id + 1} - {user['username']}: {user['followers']} seguidores") |
| 130 | + |
| 131 | +#Ordenar por antigüedad |
| 132 | +users_data.sort(key=lambda x: x["created_at"]) |
| 133 | + |
| 134 | +print("Ranking por antigüedad:") |
| 135 | +for id, user, in enumerate(users_data): |
| 136 | + print(f"{id + 1} - {user['username']}: {user['created_at']}") |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | + |
0 commit comments