Вадим Королёв 1 éve
commit
ac86e4e800

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+example.json

+ 42 - 0
YTMPV.geany

@@ -0,0 +1,42 @@
+[editor]
+line_wrapping=false
+line_break_column=80
+auto_continue_multiline=true
+
+[file_prefs]
+final_new_line=false
+ensure_convert_new_lines=true
+strip_trailing_spaces=false
+replace_tabs=false
+
+[indentation]
+indent_width=4
+indent_type=0
+indent_hard_tab_width=8
+detect_indent=true
+detect_indent_width=true
+indent_mode=2
+
+[project]
+name=YTMPV
+base_path=/home/vad/Code/YTMPV/
+
+[long line marker]
+long_line_behaviour=1
+long_line_column=80
+
+[files]
+current_page=3
+FILE_NAME_0=1967;C++;0;EUTF-8;0;1;0;%2Fhome%2Fvad%2FCode%2FYTMPV%2Flib%2FYoutubeApi%2Fyoutubeapi.cpp;0;4
+FILE_NAME_1=491;C++;0;EUTF-8;0;1;0;%2Fhome%2Fvad%2FCode%2FYTMPV%2Flib%2FYoutubeApi%2Fyoutubeapi.hpp;0;4
+FILE_NAME_2=376;JSON;0;EUTF-8;0;1;0;%2Fhome%2Fvad%2FCode%2FYTMPV%2Fexample.json;0;2
+FILE_NAME_3=166;C++;0;EUTF-8;0;1;0;%2Fhome%2Fvad%2FCode%2FYTMPV%2Fsrc%2FYTMPV%2Fytmpv.cpp;0;4
+
+[prjorg]
+source_patterns=*.c;*.C;*.cpp;*.cxx;*.c++;*.cc;*.m;
+header_patterns=*.h;*.H;*.hpp;*.hxx;*.h++;*.hh;
+ignored_dirs_patterns=.*;CVS;
+ignored_file_patterns=*.o;*.obj;*.a;*.lib;*.so;*.dll;*.lo;*.la;*.class;*.jar;*.pyc;*.mo;*.gmo;
+generate_tag_prefs=0
+show_empty_dirs=true
+external_dirs=

+ 5 - 0
lib/YoutubeApi/meson.build

@@ -0,0 +1,5 @@
+ytapi_lib = library(
+    'ytapi',
+    ['youtubeapi.cpp'],
+    dependencies: [glibmm_dep, curlpp_dep, json_dep]
+)

+ 66 - 0
lib/YoutubeApi/youtubeapi.cpp

@@ -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;
+    }
+}

+ 39 - 0
lib/YoutubeApi/youtubeapi.hpp

@@ -0,0 +1,39 @@
+#pragma once
+#include <glibmm/ustring.h>
+#include <string>
+#include <curl/curl.h>
+#include <vector>
+#include <sstream>
+#include <nlohmann/json.hpp>
+#include <iostream>
+
+// Канал
+struct channel {
+    Glib::ustring id;
+    Glib::ustring name;
+};
+
+// Видео
+struct video {
+    Glib::ustring id;
+    Glib::ustring title;
+    Glib::ustring description;
+    channel author;
+};
+
+// Функция записи данных в std::ostringstream
+// если что, добавить static.
+size_t write_data(char *ptr, size_t size, size_t nmemb, void *userdata);
+
+// Кодирует URL-строку
+// https://stackoverflow.com/a/154627
+std::string urlencode(const std::string& decoded);
+
+// Раскодирует URL-строку
+std::string urldecode(const std::string& encoded);
+
+// Получает список видео по запросу
+std::vector<video> get_videos_by_request(std::string api_key, std::string query);
+
+// Возвращает список видео из JSON
+std::vector<video> get_videos_from_json(std::string input);

+ 2 - 0
lib/meson.build

@@ -0,0 +1,2 @@
+lib_includes = include_directories('.')
+subdir('YoutubeApi')

+ 14 - 0
meson.build

@@ -0,0 +1,14 @@
+project('YTMPV', 'cpp',
+  version : '0.1',
+  default_options : [
+    'warning_level=3',
+    'cpp_std=c++17',
+    'c_std=c11',
+    'default_library=static'
+  ]
+)
+glibmm_dep = dependency('glibmm-2.68')
+curlpp_dep = dependency('curlpp')
+json_dep = dependency('nlohmann_json')
+subdir('lib')
+subdir('src/YTMPV')

+ 7 - 0
src/YTMPV/meson.build

@@ -0,0 +1,7 @@
+ytmpv = executable(
+  'ytmpv',
+  'ytmpv.cpp',
+  link_with: [ytapi_lib],
+  include_directories: [lib_includes],
+  dependencies: [glibmm_dep, curlpp_dep, json_dep]
+)

+ 14 - 0
src/YTMPV/ytmpv.cpp

@@ -0,0 +1,14 @@
+#include <YoutubeApi/youtubeapi.hpp>
+#include <fstream>
+
+int main(int argc, char **argv) {
+    //curl_global_init(CURL_GLOBAL_ALL);
+    //get_videos_by_request("token", argv[1]);
+
+    std::ifstream ifs("example.json");
+    std::string content(
+        (std::istreambuf_iterator<char>(ifs)),
+        (std::istreambuf_iterator<char>())
+    );
+    get_videos_from_json(content);
+}