I am using YouTube API with libcurl to check my live stream status. The code snippet is reproduced below. Most of time the response returned by the function is_YouTubeLive
is correct. I have not checked if CURLOPT_PROGRESSFUNCTION
parameter can be of some used here. How can I improve the function?
CODE
static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp){ ((std::string*)userp)->append((char*)contents, size * nmemb); return size * nmemb;}bool is_YouTubeLive(){ line1: CURL* curl = curl_easy_init(); if (curl) { std::string readBuffer; curl_easy_setopt(curl, CURLOPT_URL, "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=[API KEY]"); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); CURLcode response = curl_easy_perform(curl); if (response != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(response)); return false; } curl_easy_cleanup(curl); auto quota_error = readBuffer.find("quotaExceeded"); if (quota_error != std::string::npos) { this_thread::sleep_for(chrono::hours(1)); // sleep for 1 hour goto line1; } auto found = readBuffer.find("\"liveBroadcastContent\": \"live\""); if (found != std::string::npos) return true; } return false;}