|
@@ -1,6 +1,26 @@
|
|
|
#include "youtubeapi.hpp"
|
|
|
|
|
|
namespace ytapi {
|
|
|
+
|
|
|
+ void youtubeInit(std::string key) {
|
|
|
+ api_key = key;
|
|
|
+ }
|
|
|
+
|
|
|
+ bool performCurlRequest(std::string url, std::stringstream* response) {
|
|
|
+ // Добавляем ключ к каждому запросу
|
|
|
+ url += "&key=";
|
|
|
+ url += api_key;
|
|
|
+
|
|
|
+ CURL* handle = curl_easy_init();
|
|
|
+ if (!handle) return false;
|
|
|
+ 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 true;
|
|
|
+ }
|
|
|
+
|
|
|
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);
|
|
@@ -27,74 +47,59 @@ namespace ytapi {
|
|
|
return size * nmemb;
|
|
|
}
|
|
|
|
|
|
- std::vector<db::video> getVideosByQuery(std::string api_key, std::string query) {
|
|
|
+ std::vector<std::string> getVideoIDsByQuery(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;
|
|
|
+
|
|
|
+ performCurlRequest(url, &response);
|
|
|
+ return getVideoIDsFromJSON(response.str());
|
|
|
}
|
|
|
|
|
|
- std::vector<db::video> getVideosFromJSON(std::string input) {
|
|
|
+ std::vector<std::string> getVideoIDsFromJSON(std::string input) {
|
|
|
auto json = nlohmann::json::parse(input);
|
|
|
auto items = json["items"];
|
|
|
- std::vector<db::video> output;
|
|
|
+ std::vector<std::string> 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
|
|
|
+ output.push_back(yt_id);
|
|
|
+ }
|
|
|
|
|
|
- // 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;
|
|
|
+ return output;
|
|
|
+ }
|
|
|
|
|
|
- // 3. Добавляем видео в БД
|
|
|
- db::addVideo(¤t_video);
|
|
|
+ video_result getVideoByYTID(std::string yt_id) {
|
|
|
+ std::stringstream response;
|
|
|
+ std::string url;
|
|
|
+ video_result output;
|
|
|
|
|
|
- // 4. И в выходной массив
|
|
|
- output.push_back(current_video);
|
|
|
- }
|
|
|
+ // Формирование URL
|
|
|
+ url += "https://youtube.googleapis.com/youtube/v3/videos?part=snippet,contentDetails,statistics&id=";
|
|
|
+ url += yt_id;
|
|
|
+
|
|
|
+ // Выполнение запроса
|
|
|
+ performCurlRequest(url, &response);
|
|
|
+
|
|
|
+ // Парсинг
|
|
|
+ // TODO:stats
|
|
|
+ auto json = nlohmann::json::parse(response);
|
|
|
+ auto item = json["items"][0];
|
|
|
+ auto snippet = item["snippet"];
|
|
|
+ auto content_details = item["contentDetails"];
|
|
|
+ // auto stats = item["statistics"];
|
|
|
+
|
|
|
+ output.yt_id = yt_id;
|
|
|
+ output.title = Glib::ustring(snippet["title"]);
|
|
|
+ output.description = Glib::ustring(snippet["description"]);
|
|
|
+ output.duration = std::string(content_details["duration"]);
|
|
|
+ output.author_yt_id = std::string(snippet["channelId"]);
|
|
|
+ output.author_name = Glib::ustring(snippet["channelTitle"]);
|
|
|
+ output.published_at = std::string(snippet["publishedAt"]);
|
|
|
|
|
|
return output;
|
|
|
}
|