From 7a1224984581a33902874c963de245be583c9d44 Mon Sep 17 00:00:00 2001 From: linarphy Date: Tue, 4 Apr 2023 03:56:16 +0200 Subject: [PATCH] Add different types Some issues with playlist currently --- main.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 11 deletions(-) diff --git a/main.cpp b/main.cpp index 8c4ac65..63715f5 100644 --- a/main.cpp +++ b/main.cpp @@ -28,12 +28,23 @@ int main(int argc, char **argv) return EXIT_FAILURE; } + int const TYPE_VIDEO = { 0 }; + int const TYPE_CHANNEL = { 1 }; + int const TYPE_PLAYLIST = { 2 }; + + std::string const IDS[3] = { + "WEB_PAGE_TYPE_WATCH", + "WEB_PAGE_TYPE_CHANNEL", + "WEB_PAGE_TYPE_PLAYLIST", + }; + + int type = TYPE_VIDEO; + + int indice_search_terms = 1; + int opt; - std::string const video_url = { "https://www.youtube.com/watch?v=" }; - std::string search_url = { "https://www.youtube.com/results?search_query=" }; - - while ((opt = getopt(argc, argv, "hv")) != -1) + while ((opt = getopt(argc, argv, "hvcpd")) != -1) { switch (opt) { @@ -41,19 +52,35 @@ int main(int argc, char **argv) std::cout << "Usage: ytsearch [options...] search-term" << std::endl << " -h: show this help and quit" << std::endl << " -v: show version number and quit" << std::endl << + " -c: get channel url" << std::endl << + " -d: get video url (default)" << std::endl << + " -p: get playlist url" << std::endl << std::endl << "Retrieve the url of a youtube video matching the searched terms" << std::endl; return EXIT_SUCCESS; case 'v': std::cout << VERSION << std::endl; return EXIT_SUCCESS; + case 'c': + type = TYPE_CHANNEL; + indice_search_terms++; + break; + case 'p': + type = TYPE_PLAYLIST; + indice_search_terms++; + break; + case 'd': + type = TYPE_VIDEO; + indice_search_terms++; + break; default: std::cout << "ytsearch: try 'ytsearch -h' for more information" << std::endl; return EXIT_FAILURE; } } - search_url += argv[1]; + std::string const base_search_url = { "https://www.youtube.com/results?search_query=" }; + std::string const youtube_url = { "https://www.youtube.com/" }; CURL *curl; std::string readBuffer; @@ -66,11 +93,18 @@ int main(int argc, char **argv) return EXIT_FAILURE; } + std::string search_terms = argv[indice_search_terms]; + + char* search_url_encoded = curl_easy_escape(curl, search_terms.c_str(), 0); + + std::string search_url = base_search_url + search_url_encoded; + curl_easy_setopt(curl, CURLOPT_URL, search_url.c_str()); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); CURLcode res = { curl_easy_perform(curl) }; curl_easy_cleanup(curl); + curl_free(search_url_encoded); if (res != 0) { @@ -94,15 +128,15 @@ int main(int argc, char **argv) return EXIT_FAILURE; } - size_t first_video = readBuffer.find("WEB_PAGE_TYPE_WATCH", pos_start); + size_t first_item = readBuffer.find(IDS[type].c_str(), pos_start); - if (first_video == std::string::npos or pos_start == 0) + if (first_item == std::string::npos or pos_start == 0) { - std::cout << error_api << std::endl; + std::cout << "ytsearch: error, no item found with the asked type" << std::endl; return EXIT_FAILURE; } - while (first_video > pos_start + 60) // type is defined a little after url, we want a video + while (first_item > pos_start + 60) // type is defined a little after url, we want a video { for (std::string name : paths) { @@ -116,11 +150,11 @@ int main(int argc, char **argv) } } - size_t size = { pos_start + 15 }; // size of " url":"/watch?v= ", last part of paths + size_t size = { pos_start + 7 }; // size of " url":"/ ", last part of paths size_t pos_end = { readBuffer.find('"', size) }; - std::cout << video_url << readBuffer.substr(size, pos_end - size) << std::endl; + std::cout << youtube_url << readBuffer.substr(size, pos_end - size) << std::endl; return EXIT_SUCCESS; }