1+ /** #37 - JavaScript -> Jesus Antonio Escamilla */
2+
3+ /**
4+ * FORTNITE RUBIUS CUP.
5+ * Utilizando la Terminal para pedir datos y agregarlos.
6+ */
7+
8+ const participants = [
9+ 'Abby' , 'Ache' , 'Adri Contreras' , 'Agustin' , 'Alexby' , 'Ampeter' , 'Ander' , 'Ari Gameplays' ,
10+ 'Arigely' , 'Auronplay' , 'Axozer' , 'Beniju' , 'By Calitos' , 'Byviruzz' , 'Carrera' , 'Celopan' ,
11+ 'Cheto' , 'CrystalMolly' , 'Dario Eme Hache' , 'Dheyo' , 'DjMariio' , 'Doble' , 'Elvisa' , 'Elyas360' ,
12+ 'Folagor' , 'Grefg' , 'Guanyar' , 'Hika' , 'Hiper' , 'Ibai' , 'Ibelky' , 'Illojuan' , 'Imantado' ,
13+ 'Irina Isasia' , 'JessKiu' , 'Jopa' , 'JordiWild' , 'Kenai Souza' , 'Keroro' , 'Kidd Keo' , 'Kiko Rivera' ,
14+ 'Knekro' , 'Koko' , 'KronnoZomber' , 'Leviathan' , 'Lit Killah' , 'Lola Lolita' , 'Lolito' , 'Luh' ,
15+ 'Luzu' , 'Mangel' , 'Mayichi' , 'Melo' , 'MissaSinfonia' , 'Mixwell' , 'Mr. Jagger' , 'Nate Gentile' ,
16+ 'Nexxuz' , 'Nia' , 'Nil Ojeda' , 'NissaXter' , 'Ollie' , 'Orslok' , 'Outconsumer' , 'Papi Gavi' ,
17+ 'Paracetamor' , 'Patica' , 'Paula Gonu' , 'Pausenpaii' , 'Perxitaa' , 'Plex' , 'Polispol' , 'Quackity' ,
18+ 'RecueroDP' , 'Reven' , 'Rivers' , 'RobertPG' , 'Roier' , 'Rojuu' , 'Rubius' , 'Shadoune' , 'Silithur' ,
19+ 'SpokSponha' , 'Spreen' , 'Spursito' , 'Staxx' , 'SuzyRoxx' , 'Vicens' , 'Vituber' , 'Werlyb' , 'Xavi' ,
20+ 'Xcry' , 'Xokas' , 'Zarcort' , 'Zeling' , 'Zorman'
21+ ] ;
22+
23+ const twitch_clientId = 'y6di6t9mk7ihz9hyqt32wh67rguhzi' ;
24+ const twitch_clientSecret = 'o887i5ph5u94u6aapqvocrnpedl3oo' ;
25+
26+ // Para obtener un token
27+ async function tokenAccessTwitch ( clientId , clientSecret ) {
28+ const response = await fetch ( `https://id.twitch.tv/oauth2/token` , {
29+ method : 'POST' ,
30+ headers : {
31+ 'Content-Type' : 'application/x-www-form-urlencoded'
32+ } ,
33+ body : `client_id=${ clientId } &client_secret=${ clientSecret } &grant_type=client_credentials`
34+ } ) ;
35+
36+ if ( ! response . ok ) {
37+ console . error ( `HTTP error! status: ${ response . status } ` ) ;
38+ }
39+
40+ const result = await response . json ( ) ;
41+ return result . access_token ;
42+ }
43+
44+ // Para obtener el acceso a Twitch
45+ async function fetchTwitchAPI ( endpoint ) {
46+ const tokenAccess = await tokenAccessTwitch ( twitch_clientId , twitch_clientSecret )
47+ const response = await fetch ( `https://api.twitch.tv/helix${ endpoint } ` , {
48+ headers :{
49+ 'Client-ID' : twitch_clientId ,
50+ 'Authorization' : `Bearer ${ tokenAccess } `
51+ }
52+ } ) ;
53+
54+ if ( ! response . ok ) {
55+ console . error ( `Error en la API de Twitch: ${ response . status } ` ) ;
56+ }
57+ return response . json ( ) ;
58+ }
59+
60+ // Para obtener los datos los participantes
61+ async function getUserInfo ( username ) {
62+ username = username . replace ( / [ ^ a - z A - Z 0 - 9 _ ] / g, '' ) ;
63+ const userData = await fetchTwitchAPI ( `/users?login=${ username } ` )
64+ if ( userData . data . length > 0 ) {
65+ const user = userData . data [ 0 ] ;
66+ const followersData = await fetchTwitchAPI ( `/channels/followers?broadcaster_id=${ user . id } ` )
67+ return {
68+ username : user . login ,
69+ displayName : user . display_name ,
70+ followers : followersData . total ,
71+ createdAt : user . created_at
72+ } ;
73+ }
74+ return null ;
75+ }
76+
77+ async function rankingGammer ( ) {
78+ const participantInfo = [ ] ;
79+
80+ for ( const participant of participants ) {
81+ const info = await getUserInfo ( participant )
82+ if ( info ) {
83+ participantInfo . push ( info ) ;
84+ } else {
85+ console . log ( `${ participant } no tiene cuenta de Twitch o no se pudo obtener la información.` ) ;
86+ }
87+ }
88+
89+ // Ranking por número de seguidores
90+ const followerRanking = [ ...participantInfo ] . sort ( ( a , b ) => b . followers - a . followers ) ;
91+
92+ console . log ( "Ranking por número de seguidores:" ) ;
93+ followerRanking . forEach ( ( p , index ) => {
94+ console . log ( `${ index + 1 } . ${ p . displayName } : ${ p . followers } seguidores` ) ;
95+ } ) ;
96+
97+ // Ranking por antigüedad
98+ const ageRanking = [ ...participantInfo ] . sort ( ( a , b ) => new Date ( a . createdAt ) - new Date ( b . createdAt ) ) ;
99+
100+ console . log ( "\nRanking por antigüedad de cuenta:" ) ;
101+ ageRanking . forEach ( ( p , index ) => {
102+ console . log ( `${ index + 1 } . ${ p . displayName } : Cuenta creada el ${ new Date ( p . createdAt ) . toLocaleDateString ( ) } ` ) ;
103+ } ) ;
104+ }
105+
106+ // Ejecutar el programa
107+ rankingGammer ( )
0 commit comments