|
| 1 | +// Autor: Héctor Adán |
| 2 | +// GitHub: https://github.com/hectorio23 |
| 3 | + |
| 4 | +#include <iostream> |
| 5 | +#include <vector> |
| 6 | +#include <string> |
| 7 | +#include <curl/curl.h> |
| 8 | +#include <json/json.h> // Incluye la biblioteca JSONcpp (debe estar instalada) |
| 9 | + |
| 10 | +// Función callback para manejar la respuesta HTTP y almacenarla en una cadena |
| 11 | +size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* s) { |
| 12 | + s->append((char*)contents, size * nmemb); |
| 13 | + return size * nmemb; |
| 14 | +} |
| 15 | + |
| 16 | +// Realiza la solicitud HTTP a la API de Twitch y devuelve la respuesta como cadena |
| 17 | +std::string make_request(const std::string& url, const std::string& auth_token) { |
| 18 | + CURL* curl; |
| 19 | + CURLcode res; |
| 20 | + std::string response_string; |
| 21 | + |
| 22 | + curl = curl_easy_init(); |
| 23 | + if (curl) { |
| 24 | + struct curl_slist* headers = NULL; |
| 25 | + headers = curl_slist_append(headers, ("Authorization: Bearer " + auth_token).c_str()); |
| 26 | + headers = curl_slist_append(headers, "Client-ID: YOUR_CLIENT_ID"); |
| 27 | + curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); |
| 28 | + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); |
| 29 | + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); |
| 30 | + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_string); |
| 31 | + |
| 32 | + res = curl_easy_perform(curl); |
| 33 | + curl_easy_cleanup(curl); |
| 34 | + } |
| 35 | + return response_string; |
| 36 | +} |
| 37 | + |
| 38 | +// Procesa la respuesta JSON para obtener el número de seguidores y la fecha de creación |
| 39 | +void process_data(const std::vector<std::string>& participants, const std::string& auth_token) { |
| 40 | + Json::CharReaderBuilder readerBuilder; |
| 41 | + Json::Value root; |
| 42 | + std::string errs; |
| 43 | + |
| 44 | + for (const auto& user : participants) { |
| 45 | + std::string url = "https://api.twitch.tv/helix/users?login=" + user; |
| 46 | + std::string response = make_request(url, auth_token); |
| 47 | + |
| 48 | + // Parseamos la respuesta JSON |
| 49 | + std::stringstream ss(response); |
| 50 | + if (Json::parseFromStream(readerBuilder, ss, &root, &errs)) { |
| 51 | + const auto& data = root["data"]; |
| 52 | + if (!data.empty()) { |
| 53 | + const std::string userId = data[0]["id"].asString(); |
| 54 | + const std::string creationDate = data[0]["created_at"].asString(); |
| 55 | + |
| 56 | + // Solicitar el número de seguidores para este usuario |
| 57 | + std::string followers_url = "https://api.twitch.tv/helix/users/follows?to_id=" + userId; |
| 58 | + std::string followers_response = make_request(followers_url, auth_token); |
| 59 | + |
| 60 | + Json::Value followers_root; |
| 61 | + std::stringstream ss_followers(followers_response); |
| 62 | + if (Json::parseFromStream(readerBuilder, ss_followers, &followers_root, &errs)) { |
| 63 | + int total_followers = followers_root["total"].asInt(); |
| 64 | + |
| 65 | + // Imprime la información del usuario |
| 66 | + std::cout << "Usuario: " << user << "\n"; |
| 67 | + std::cout << "Seguidores: " << total_followers << "\n"; |
| 68 | + std::cout << "Fecha de creación: " << creationDate << "\n\n"; |
| 69 | + } else { |
| 70 | + std::cerr << "Error al parsear la respuesta de seguidores de " << user << ": " << errs << "\n"; |
| 71 | + } |
| 72 | + } else { |
| 73 | + std::cout << "El usuario " << user << " no existe en Twitch.\n\n"; |
| 74 | + } |
| 75 | + } else { |
| 76 | + std::cerr << "Error al parsear la respuesta de Twitch para " << user << ": " << errs << "\n"; |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +int main() { |
| 82 | + std::vector<std::string> participants = {"user1", "user2", "user3"}; // Lista de participantes |
| 83 | + std::string auth_token = "YOUR_AUTH_TOKEN"; // Debes usar un token válido |
| 84 | + |
| 85 | + process_data(participants, auth_token); |
| 86 | + |
| 87 | + return 0; |
| 88 | +} |
| 89 | + |
| 90 | +// Para cmpilar el programa en un sistema tipo UNIX usa la siguiente instruccion |
| 91 | +// g++ -std=c++11 -lcurl -ljsoncpp -o twitch_ranking twitch_ranking.cpp |
0 commit comments