Skip to content

Commit 2654844

Browse files
committed
mouredev#40 - python
1 parent 117ae7e commit 2654844

File tree

1 file changed

+238
-0
lines changed
  • Roadmap/40 - FORTNITE RUBIUS CUP/python

1 file changed

+238
-0
lines changed
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
# 40 FORTNITE RUBIUS CUP
2+
3+
import requests
4+
import base64
5+
from datetime import datetime
6+
import os
7+
from dotenv import load_dotenv
8+
9+
# Cargar las variables de entorno desde .env
10+
load_dotenv()
11+
12+
# Obtener CLIENT_ID y CLIENT_SECRET desde las variables de entorno
13+
CLIENT_ID = os.getenv("CLIENT_ID")
14+
CLIENT_SECRET = os.getenv("CLIENT_SECRET")
15+
16+
17+
def obtener_token() -> str:
18+
url = "https://id.twitch.tv/oauth2/token"
19+
data = {
20+
"client_id": CLIENT_ID,
21+
"client_secret": CLIENT_SECRET,
22+
"grant_type": "client_credentials"
23+
}
24+
25+
headers = {
26+
"Content-Type": "application/x-www-form-urlencoded"
27+
}
28+
29+
response = requests.post(url, headers=headers, data=data)
30+
if response.status_code != 200:
31+
raise Exception(f"Error obteniendo el token de Twitch: {
32+
response.json()}.")
33+
34+
return response.json()["access_token"]
35+
36+
37+
def buscar_participante(token: str, login: str):
38+
url = f"https://api.twitch.tv/helix/users?login={login}"
39+
headers = {
40+
"Authorization": f"Bearer {token}",
41+
"Client-Id": CLIENT_ID
42+
}
43+
44+
response = requests.get(url, headers=headers)
45+
if response.status_code != 200:
46+
return {
47+
"login": login,
48+
"estado del usuario de twitch": "Inactivo"
49+
}
50+
resultados = response.json().get("data", [])
51+
if resultados:
52+
return {
53+
"id": resultados[0]["id"],
54+
"login": resultados[0]["login"],
55+
"usuario twitch": resultados[0]["display_name"],
56+
"creación de cuenta": (datetime.strptime(resultados[0]["created_at"], "%Y-%m-%dT%H:%M:%SZ")),
57+
"descripción": resultados[0]["description"],
58+
"estado del usuario de twitch": "activo"
59+
}
60+
return {}
61+
62+
63+
def buscar_seguidores_participante(token: str, id: str):
64+
url = f"https://api.twitch.tv/helix/channels/followers?broadcaster_id={id}"
65+
headers = {
66+
"Authorization": f"Bearer {token}",
67+
"Client-Id": CLIENT_ID
68+
}
69+
response = requests.get(url, headers=headers)
70+
if response.status_code != 200:
71+
return {
72+
"total de seguidores": 0,
73+
"estado del usuario de twitch": "Inactivo",
74+
"seguido desde": "Sin informacion disponible"
75+
}
76+
77+
resultados = response.json()
78+
total_seguidores = resultados.get("total", 0)
79+
80+
if resultados.get("data"):
81+
resultados_canal = resultados["data"][0]
82+
return {
83+
"total de seguidores": total_seguidores,
84+
"broadcaster id": resultados_canal['user_id'],
85+
"login del usuario": resultados_canal['user_login'],
86+
"seguido desde": resultados_canal['followed_at']
87+
}
88+
return {
89+
"total de seguidores": total_seguidores,
90+
"estado del usuario de twitch": "Activo",
91+
"seguido desde": "Sin informacion disponible"
92+
}
93+
94+
95+
def buscar_info_canal(token: str, broadcaster_id: str):
96+
url = f"https://api.twitch.tv/helix/channels?broadcaster_id={
97+
broadcaster_id}"
98+
headers = {
99+
"Authorization": f"Bearer {token}",
100+
"Client-Id": CLIENT_ID
101+
}
102+
response = requests.get(url, headers=headers)
103+
if response.status_code != 200:
104+
return {
105+
"juegos": "sin información",
106+
"tópicos del canal": "sin información",
107+
}
108+
resultados_canal = response.json().get("data", [])
109+
if resultados_canal:
110+
return {
111+
"juegos": resultados_canal[0]['game_name'],
112+
"tópicos del canal": resultados_canal[0]['content_classification_labels']
113+
}
114+
return {
115+
"juegos": "sin información",
116+
"tópicos del canal": "sin información",
117+
}
118+
119+
120+
token = obtener_token()
121+
122+
participantes_lista = [
123+
'littleragergirl', 'DJMARIIO', 'KikoRivera', 'NISSAXTER',
124+
'SHADOUNE666', 'ACHE', 'DOBLE', 'KNEKRO',
125+
'OllieGamerz', 'SILITHUR', 'AdriContreras4', 'ElvisaYomastercard',
126+
'KOKO', 'ORSLOK', 'spok_sponha', 'agustin51',
127+
'elyas360', 'KronnoZomberOficial', 'Outconsumer', 'SPREEN',
128+
'ElSpreen', 'FolagorLives', 'Leviathan', 'PapiGaviTV',
129+
'Spursito', 'Ampeterby7', 'TheGrefg', 'LITkillah',
130+
'paracetamor', 'bysTaXx', 'tvandeR', 'GUANYAR',
131+
'LOLALOLITA', 'patica1999', 'SUZYROXX', 'ARIGAMEPLAYS',
132+
'HIKA', 'LOLITOFDEZ', 'PAULAGONU', 'VICENS',
133+
'ARIGELI_', 'Hiperop', 'LUH', 'PAUSENPAII',
134+
'VITU', 'AURONPLAY', 'IBAI', 'LUZU',
135+
'PERXITAA', 'WERLYB', 'AXOZER', 'IBELKY_',
136+
'MANGEL', 'NoSoyPlex', 'XAVI', 'BENIJU03',
137+
'ILLOJUAN', 'MAYICHI', 'POLISPOL1', 'XCRY',
138+
'BYCALITOS', 'IMANTADO', 'MELO', 'QUACKITY',
139+
'elxokas', 'BYVIRUZZ', 'IRINA ISASIA', 'MISSASINFONIA',
140+
'Recuerd0p', 'THEZARCORT', 'CARRERAAA', 'JESSKIU',
141+
'MIXWELL', 'REVENXZ', 'ZELING', 'CELOPAN',
142+
'JOPA', 'JaggerPrincesa', 'rivers_gg', 'ZormanWorld',
143+
'srcheeto', 'JORDIWILD', 'NATEGENTILE7', 'ROBERTPG',
144+
'CRYSTALMOLLY', 'kenaivsouza', 'NEXXUZ', 'ROIER',
145+
'DARIOEMEHACHE', 'MrKeroro10', 'LakshartNia', 'ROJUU',
146+
'DHEYLO', 'TheKiddKeo95', 'nilojeda', 'RUBIUS'
147+
]
148+
149+
150+
informacion_participantes_seguidores = []
151+
informacion_participantes_antiguedad = []
152+
153+
154+
for nombre in participantes_lista:
155+
try:
156+
participante = buscar_participante(token, nombre)
157+
info = {"usuario twitch": nombre}
158+
159+
if "id" in participante:
160+
info["id"] = participante["id"]
161+
info["estado del usuario de twitch"] = participante.get(
162+
"estado del usuario de twitch", "Inactivo")
163+
seguidores = buscar_seguidores_participante(
164+
token, participante["id"])
165+
info["total seguidores"] = int(
166+
seguidores.get("total de seguidores", 0))
167+
168+
if "broadcaster id" in seguidores:
169+
canal = buscar_info_canal(token, seguidores["broadcaster id"])
170+
info["juegos"] = canal.get(
171+
"juegos", "Informacion no disponible")
172+
info["tópicos del canal"] = canal.get()
173+
else:
174+
info["estado del usuario de twitch"] = "No tiene cuenta de twitch"
175+
info["total seguidores"] = 0
176+
177+
informacion_participantes_seguidores.append(info)
178+
179+
except Exception as e:
180+
print(f"Error al procesar {nombre}: {e}")
181+
182+
for nombre in participantes_lista:
183+
try:
184+
participante = buscar_participante(token, nombre)
185+
cuenta = {"usuario twitch": nombre}
186+
if "id" in participante:
187+
cuenta["id"] = participante["id"]
188+
cuenta["estado del usuario de twitch"] = participante.get(
189+
"estado del usuario de twitch", "Inactivo")
190+
cuenta["creación de cuenta"] = participante.get(
191+
"creación de cuenta", None)
192+
creacion = buscar_seguidores_participante(
193+
token, participante["id"])
194+
creacion["seguido desde"] = creacion. get("seguido desde", None)
195+
else:
196+
cuenta["estado del usuario de twitch"] = "No tiene cuenta de twitch"
197+
cuenta["creación de cuenta"] = None
198+
199+
informacion_participantes_antiguedad.append(cuenta)
200+
201+
except Exception as e:
202+
print(f"Error al procesar {nombre}: {e}")
203+
204+
205+
informacion_participantes_seguidores = sorted(
206+
informacion_participantes_seguidores,
207+
key=lambda x: x.get("total seguidores", 0),
208+
reverse=True
209+
)
210+
211+
212+
informacion_participantes_antiguedad = sorted(
213+
informacion_participantes_antiguedad,
214+
key=lambda x: x.get("creación de cuenta") if x.get(
215+
"creación de cuenta") is not None else datetime.min,
216+
reverse=True
217+
)
218+
# Participantes del evento Rubius Cup
219+
print("\nLista de los seguidores de los participantes del evento Rubius Cup en Twitch\n")
220+
for participante_evento_fortnite in informacion_participantes_seguidores:
221+
print(f"Participante: {participante_evento_fortnite['usuario twitch']}, Estado: {
222+
participante_evento_fortnite['estado del usuario de twitch']}, Seguidores: {participante_evento_fortnite['total seguidores']}")
223+
224+
print("\nLista de antigueddad de las cuentas de los participantes del evento Rubius Cup en Twitch\n")
225+
226+
# Participantes del evento Rubius Cup
227+
for participante_evento_fortnite in informacion_participantes_antiguedad:
228+
fecha_creacion = participante_evento_fortnite.get("creación de cuenta")
229+
230+
if isinstance(fecha_creacion, datetime):
231+
fecha_visual = fecha_creacion.strftime("%d-%m-%Y %H:%M:%S")
232+
elif isinstance(fecha_creacion, str):
233+
fecha_visual = fecha_creacion
234+
else:
235+
fecha_visual = "Información no disponible"
236+
237+
print(f"Participante: {participante_evento_fortnite['usuario twitch']}, Estado: {
238+
participante_evento_fortnite['estado del usuario de twitch']}, Creación de cuenta: {fecha_visual}")

0 commit comments

Comments
 (0)