youtubeapi.hpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #pragma once
  2. #include <glibmm/ustring.h>
  3. #include <string>
  4. #include <curl/curl.h>
  5. #include <vector>
  6. #include <sstream>
  7. #include <nlohmann/json.hpp>
  8. #include <iostream>
  9. #include <Database/entities.hpp>
  10. #include <Database/database.hpp>
  11. namespace ytapi {
  12. // Детали видео - необработанные
  13. struct video_result {
  14. std::string yt_id; // ID на YouTube
  15. Glib::ustring title; // Название
  16. Glib::ustring description; // Описание
  17. std::string duration; // Длительность
  18. std::string author_yt_id; // ID автора на YouTube
  19. Glib::ustring author_name; // Имя автора на YouTube
  20. std::string published_at; // Когда было опубликовано
  21. int views_count; // Количество просмотров
  22. int rating; // Моя оценка видео
  23. };
  24. // API ключ
  25. static std::string api_key;
  26. // Записывает ключ API в api_key
  27. void youtubeInit(std::string key);
  28. // Функция записи данных в std::ostringstream
  29. size_t writeHttpToString(char* ptr, size_t size, size_t nmemb, void *userdata);
  30. // Функция записи данных в файл на диске
  31. size_t writeHttpToFile(void* ptr, size_t size, size_t nmemb, FILE* userdata);
  32. // Кодирует URL-строку
  33. // https://stackoverflow.com/a/154627
  34. std::string urlencode(const std::string& decoded);
  35. // Раскодирует URL-строку
  36. std::string urldecode(const std::string& encoded);
  37. // Получает список YT_ID видео по запросу
  38. std::vector<std::string> getVideoIDsByQuery(std::string query);
  39. // Возвращает объект видео, получив данные по API
  40. video_result getVideoByYTID(std::string yt_id);
  41. // Возвращает список видео id из JSON списка видео
  42. std::vector<std::string> getVideoIDsFromJSON(std::string input);
  43. // Подгатавливает, настраивает и выполняет CURL запрос. Возвращает
  44. // успешность вызова
  45. bool performCurlRequest(std::string url, std::stringstream* response);
  46. // Подгатавливает, настраивает и выполняет CURL запрос на скачивание файла
  47. // Файл сохранится в save_path
  48. bool performCurlRequest(std::string url, std::string save_path);
  49. // Скачивает превью видео и сохраняет в путь save_path
  50. void downloadMediumThumnail(std::string yt_id, std::string save_path);
  51. // Скачивает большое превью видео и сохраняет в путь save_path
  52. void downloadLargeThumnail(std::string yt_id, std::string save_path);
  53. }