|
| 1 | +const https = require('node:https') |
| 2 | + |
| 3 | +const CLIENT_ID = "Your Client ID " |
| 4 | +const CLIENT_SECRET = "Your client secret" |
| 5 | + |
| 6 | + |
| 7 | +async function getToken() { |
| 8 | + return new Promise(async function (resolve, reject) { |
| 9 | + |
| 10 | + const options = { |
| 11 | + hostname: 'id.twitch.tv', |
| 12 | + path: "/oauth2/token", |
| 13 | + method: "POST", |
| 14 | + headers: { |
| 15 | + 'Content-type': "application/x-www-form-urlencoded" |
| 16 | + |
| 17 | + } |
| 18 | + }; |
| 19 | + let body = { |
| 20 | + 'grant_type': 'client_credentials', |
| 21 | + 'client_secret': CLIENT_SECRET, |
| 22 | + 'client_id': CLIENT_ID |
| 23 | + } |
| 24 | + |
| 25 | + // Encode body |
| 26 | + let form_body = [] |
| 27 | + for (let element in body) { |
| 28 | + |
| 29 | + let encoded_prop = encodeURIComponent(element); |
| 30 | + let encoded_value = encodeURIComponent(body[element]) |
| 31 | + form_body.push(encoded_prop + "=" + encoded_value); |
| 32 | + |
| 33 | + } |
| 34 | + form_body = form_body.join("&") |
| 35 | + |
| 36 | + let response_body = ""; |
| 37 | + const req = https.request(options,res => { |
| 38 | + |
| 39 | + res.on('data', d => { |
| 40 | + response_body += d; |
| 41 | + }); |
| 42 | + res.on('end', ()=>{ |
| 43 | + let token = JSON.parse(response_body).access_token; |
| 44 | + resolve(token) |
| 45 | + }); |
| 46 | + res.on('error',(e)=>{ |
| 47 | + reject(e); |
| 48 | + }) |
| 49 | + }); |
| 50 | + |
| 51 | + req.on('error',(e) => { |
| 52 | + reject(e); |
| 53 | + }); |
| 54 | + //Execute post request |
| 55 | + req.write(form_body); |
| 56 | + //End request |
| 57 | + req.end() |
| 58 | + }) |
| 59 | +} |
| 60 | +async function getFollowers(token,id){ |
| 61 | + |
| 62 | + return new Promise(function(resolve,reject){ |
| 63 | + |
| 64 | + let query =`?broadcaster_id=${id}`; |
| 65 | + const header = { |
| 66 | + 'Authorization': "Bearer "+ token, |
| 67 | + 'Client-Id': CLIENT_ID |
| 68 | + }; |
| 69 | + const options = { |
| 70 | + 'hostname': 'api.twitch.tv', |
| 71 | + 'path': `/helix/channels/followers${query}`, |
| 72 | + "method": "GET", |
| 73 | + "headers": header |
| 74 | + }; |
| 75 | + let incoming_message = ""; |
| 76 | + const req = https.get(options,(res)=>{ |
| 77 | + if(res.statusCode == 200){ |
| 78 | + res.on('data',(data)=>{ |
| 79 | + incoming_message += data |
| 80 | + }) |
| 81 | + res.on("error",(error)=>{ |
| 82 | + reject(error.message) |
| 83 | + }); |
| 84 | + res.on("end",()=>{ |
| 85 | + let user = JSON.parse(incoming_message) |
| 86 | + resolve(user.total) |
| 87 | + }) |
| 88 | + } |
| 89 | + else{ |
| 90 | + reject("Error obteniendo seguidores") |
| 91 | + } |
| 92 | + }) |
| 93 | + |
| 94 | + |
| 95 | + }) |
| 96 | +} |
| 97 | +async function basicInfo(token,user){ |
| 98 | + |
| 99 | + return new Promise(function(resolve,reject){ |
| 100 | + |
| 101 | + let user_name = user.replace(" ",""); |
| 102 | + let query = `?login=${user_name}` |
| 103 | + const header = { |
| 104 | + 'Authorization': "Bearer "+ token, |
| 105 | + 'Client-Id': CLIENT_ID |
| 106 | + }; |
| 107 | + const options = { |
| 108 | + 'hostname': 'api.twitch.tv', |
| 109 | + 'path': `/helix/users${query}`, |
| 110 | + "method": "GET", |
| 111 | + "headers": header |
| 112 | + }; |
| 113 | + let incoming_message = ""; |
| 114 | + const req = https.get(options,(res)=>{ |
| 115 | + |
| 116 | + if(res.statusCode == 200){ |
| 117 | + |
| 118 | + res.on("data",(data)=>{ |
| 119 | + incoming_message+= data |
| 120 | + }) |
| 121 | + res.on("error",(error)=>{ |
| 122 | + reject(error.message) |
| 123 | + }) |
| 124 | + res.on("end",()=>{ |
| 125 | + let user = JSON.parse(incoming_message) |
| 126 | + if(user.data[0]){ |
| 127 | + let create_at = user.data[0].created_at; |
| 128 | + let id = user.data[0].id |
| 129 | + resolve([id,create_at]) |
| 130 | + |
| 131 | + } |
| 132 | + else{ |
| 133 | + reject() |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + }) |
| 138 | + } |
| 139 | + else{ |
| 140 | + reject(`Error obteniendo usuario para ${user}`) |
| 141 | + } |
| 142 | + |
| 143 | + }) |
| 144 | + |
| 145 | + }) |
| 146 | + |
| 147 | + |
| 148 | +} |
| 149 | +async function getInfo(users){ |
| 150 | + try{ |
| 151 | + token = await getToken(); |
| 152 | + |
| 153 | + let user_list = []; |
| 154 | + |
| 155 | + for(let user in users){ |
| 156 | + try{ |
| 157 | + |
| 158 | + let list = await basicInfo(token,users[user]) |
| 159 | + let followers = await getFollowers(token,list[0]) |
| 160 | + |
| 161 | + user_list.push( { |
| 162 | + |
| 163 | + "username": users[user], |
| 164 | + "created_at": list[1], |
| 165 | + "followers": followers |
| 166 | + }); |
| 167 | + } |
| 168 | + catch{ |
| 169 | + console.log(`Error obteniendo datos para ${users[user]}`) |
| 170 | + } |
| 171 | + |
| 172 | + |
| 173 | + } |
| 174 | + return user_list |
| 175 | + } |
| 176 | + catch(Error){ |
| 177 | + |
| 178 | + console.log("Error obteniendo token") |
| 179 | + |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +function orderByFollowers(list){ |
| 184 | + |
| 185 | + let new_list = list.sort((a,b)=>a.followers > b.followers?-1:1); |
| 186 | + |
| 187 | + return new_list; |
| 188 | +} |
| 189 | + |
| 190 | +function orderByCreationDate(list){ |
| 191 | + let new_list = list.sort((a,b)=>a.created_at < b.created_at?-1:1); |
| 192 | + |
| 193 | + return new_list; |
| 194 | + |
| 195 | +} |
| 196 | + |
| 197 | +async function main(){ |
| 198 | + let users = [ |
| 199 | + 'Abby', 'Ache', 'Adri Contreras', 'Agustin', 'Alexby', 'Ampeter', 'Ander', 'Ari Gameplays', |
| 200 | + 'Arigely', 'Auronplay', 'Axozer', 'Beniju', 'By Calitos', 'Byviruzz', 'Carrera', 'Celopan', |
| 201 | + 'Cheto', 'CrystalMolly', 'Dario Eme Hache', 'Dheyo', 'DjMariio', 'Doble', 'Elvisa', 'Elyas360', |
| 202 | + 'Folagor', 'Grefg', 'Guanyar', 'Hika', 'Hiper', 'Ibai', 'Ibelky', 'Illojuan', 'Imantado', |
| 203 | + 'Irina Isasia', 'JessKiu', 'Jopa', 'JordiWild', 'Kenai Souza', 'Keroro', 'Kidd Keo', 'Kiko Rivera', |
| 204 | + 'Knekro', 'Koko', 'KronnoZomber', 'Leviathan', 'Lit Killah', 'Lola Lolita', 'Lolito', 'Luh', |
| 205 | + 'Luzu', 'Mangel', 'Mayichi', 'Melo', 'MissaSinfonia', 'Mixwell', 'Mr. Jagger', 'Nate Gentile', |
| 206 | + 'Nexxuz', 'Nia', 'Nil Ojeda', 'NissaXter', 'Ollie', 'Orslok', 'Outconsumer', 'Papi Gavi', |
| 207 | + 'Paracetamor', 'Patica', 'Paula Gonu', 'Pausenpaii', 'Perxitaa', 'Plex', 'Polispol', 'Quackity', |
| 208 | + 'RecueroDP', 'Reven', 'Rivers', 'RobertPG', 'Roier', 'Rojuu', 'Rubius', 'Shadoune', 'Silithur', |
| 209 | + 'SpokSponha', 'Spreen', 'Spursito', 'Staxx', 'SuzyRoxx', 'Vicens', 'Vituber', 'Werlyb', 'Xavi', |
| 210 | + 'Xcry', 'Xokas', 'Zarcort', 'Zeling', 'Zorman' |
| 211 | + ] |
| 212 | + let user_list = await getInfo(users) |
| 213 | + let odered_list_followers = orderByFollowers(user_list); |
| 214 | + |
| 215 | + for(let i=0; i< odered_list_followers.length; i++){ |
| 216 | + |
| 217 | + console.log(`User: ${odered_list_followers[i].username}, followers: ${odered_list_followers[i].followers},`) |
| 218 | + } |
| 219 | + let odered_list_date = orderByCreationDate(user_list); |
| 220 | + |
| 221 | + for(let i=0; i< odered_list_date.length; i++){ |
| 222 | + |
| 223 | + console.log(`User: ${odered_list_date[i].username}, fecha de creacion: ${odered_list_date[i].created_at},`) |
| 224 | + } |
| 225 | + |
| 226 | + |
| 227 | +} |
| 228 | + |
| 229 | +main() |
| 230 | + |
| 231 | + |
| 232 | + |
0 commit comments