|
@@ -0,0 +1,66 @@
|
|
|
+#include "youtubeapi.hpp"
|
|
|
+
|
|
|
+std::string urlencode(const std::string& decoded) {
|
|
|
+ const auto encoded_value = curl_easy_escape(nullptr, decoded.c_str(), static_cast<int>(decoded.length()));
|
|
|
+ std::string result(encoded_value);
|
|
|
+ curl_free(encoded_value);
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+std::string urldecode(const std::string& encoded) {
|
|
|
+ int output_length;
|
|
|
+ const auto decoded_value = curl_easy_unescape(
|
|
|
+ nullptr,
|
|
|
+ encoded.c_str(),
|
|
|
+ static_cast<int>(encoded.length()),
|
|
|
+ &output_length
|
|
|
+ );
|
|
|
+ std::string result(decoded_value, output_length);
|
|
|
+ curl_free(decoded_value);
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+// если что, добавить static.
|
|
|
+size_t write_data(char* ptr, size_t size, size_t nmemb, void* userdata) {
|
|
|
+ std::string data((const char*) ptr, (size_t) size * nmemb);
|
|
|
+ *((std::stringstream*) userdata) << data;
|
|
|
+ return size * nmemb;
|
|
|
+}
|
|
|
+
|
|
|
+std::vector<video> get_videos_by_request(std::string api_key, std::string query) {
|
|
|
+ std::stringstream response;
|
|
|
+ std::string url;
|
|
|
+ std::vector<video> output;
|
|
|
+ CURL* handle;
|
|
|
+
|
|
|
+ handle = curl_easy_init();
|
|
|
+ if (!handle) {
|
|
|
+ return output;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Формирование URL
|
|
|
+ url += "https://youtube.googleapis.com/youtube/v3/search?type=video&part=snippet&maxResult=25&q=";
|
|
|
+ url += urlencode(query);
|
|
|
+ url += "&key=";
|
|
|
+ url += api_key;
|
|
|
+
|
|
|
+ curl_easy_setopt(handle, CURLOPT_URL, url.c_str());
|
|
|
+ curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, &write_data);
|
|
|
+ curl_easy_setopt(handle, CURLOPT_WRITEDATA, &response);
|
|
|
+ curl_easy_perform(handle);
|
|
|
+ curl_easy_cleanup(handle);
|
|
|
+
|
|
|
+ return output;
|
|
|
+}
|
|
|
+
|
|
|
+std::vector<video> get_videos_from_json(std::string input) {
|
|
|
+ auto json = nlohmann::json::parse(input);
|
|
|
+ auto items = json["items"];
|
|
|
+ for (auto it = items.begin(); it != items.end(); ++it) {
|
|
|
+ auto id = it.value()["id"]["videoId"];
|
|
|
+
|
|
|
+ // Проверить существует ли видео в базе данных
|
|
|
+
|
|
|
+ std::cout << id << std::endl;
|
|
|
+ }
|
|
|
+}
|