123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- #include "youtubeapi.hpp"
- namespace ytapi {
- 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;
- }
- size_t write_http_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<db::video> getVideosByQuery(std::string api_key, std::string query) {
- std::stringstream response;
- std::string url;
- std::vector<db::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_http_data);
- curl_easy_setopt(handle, CURLOPT_WRITEDATA, &response);
- curl_easy_perform(handle);
- curl_easy_cleanup(handle);
- return output;
- }
- std::vector<db::video> getVideosFromJSON(std::string input) {
- auto json = nlohmann::json::parse(input);
- auto items = json["items"];
- std::vector<db::video> output;
-
- for (auto it = items.begin(); it != items.end(); ++it) {
- auto item = it.value();
- auto yt_id = item["id"]["videoId"];
- auto snippet = item["snippet"];
- // Проверить существует ли видео в базе данных
- db::video current_video;
- if (db::getVideoByYTID(yt_id, ¤t_video)) {
- output.push_back(current_video);
- continue;
- }
- // Видео в базе данных не существует. Добавляем!
- // 1. Ищем автора по его yt_id
- db::author current_author;
- if (!db::getAuthorByYTID(snippet["channelId"], ¤t_author)) {
- // 1.5 Автора в базе данных нет, создаём
- current_author.yt_id = snippet["channelId"];
- current_author.name = Glib::ustring(snippet["channelTitle"]);
- db::addAuthor(¤t_author);
- }
- // Перезаписать snippet
- // 2. Заполняем объект видео данными из JSON
- current_video.yt_id = yt_id;
- current_video.title = Glib::ustring(snippet["title"]);
- current_video.description = Glib::ustring(snippet["description"]);
- current_video.published_at = std::string(snippet["publishedAt"]);
- current_video.author_obj = current_author;
- // 3. Добавляем видео в БД
- db::addVideo(¤t_video);
- // 4. И в выходной массив
- output.push_back(current_video);
- }
- return output;
- }
- }
|