Add different types
Some issues with playlist currently
This commit is contained in:
parent
6bcb245024
commit
7a12249845
1 changed files with 45 additions and 11 deletions
56
main.cpp
56
main.cpp
|
@ -28,12 +28,23 @@ int main(int argc, char **argv)
|
||||||
return EXIT_FAILURE;
|
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;
|
int opt;
|
||||||
|
|
||||||
std::string const video_url = { "https://www.youtube.com/watch?v=" };
|
while ((opt = getopt(argc, argv, "hvcpd")) != -1)
|
||||||
std::string search_url = { "https://www.youtube.com/results?search_query=" };
|
|
||||||
|
|
||||||
while ((opt = getopt(argc, argv, "hv")) != -1)
|
|
||||||
{
|
{
|
||||||
switch (opt)
|
switch (opt)
|
||||||
{
|
{
|
||||||
|
@ -41,19 +52,35 @@ int main(int argc, char **argv)
|
||||||
std::cout << "Usage: ytsearch [options...] search-term" << std::endl <<
|
std::cout << "Usage: ytsearch [options...] search-term" << std::endl <<
|
||||||
" -h: show this help and quit" << std::endl <<
|
" -h: show this help and quit" << std::endl <<
|
||||||
" -v: show version number 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 <<
|
std::endl <<
|
||||||
"Retrieve the url of a youtube video matching the searched terms" << std::endl;
|
"Retrieve the url of a youtube video matching the searched terms" << std::endl;
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
case 'v':
|
case 'v':
|
||||||
std::cout << VERSION << std::endl;
|
std::cout << VERSION << std::endl;
|
||||||
return EXIT_SUCCESS;
|
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:
|
default:
|
||||||
std::cout << "ytsearch: try 'ytsearch -h' for more information" << std::endl;
|
std::cout << "ytsearch: try 'ytsearch -h' for more information" << std::endl;
|
||||||
return EXIT_FAILURE;
|
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;
|
CURL *curl;
|
||||||
std::string readBuffer;
|
std::string readBuffer;
|
||||||
|
@ -66,11 +93,18 @@ int main(int argc, char **argv)
|
||||||
return EXIT_FAILURE;
|
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_URL, search_url.c_str());
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
|
||||||
CURLcode res = { curl_easy_perform(curl) };
|
CURLcode res = { curl_easy_perform(curl) };
|
||||||
curl_easy_cleanup(curl);
|
curl_easy_cleanup(curl);
|
||||||
|
curl_free(search_url_encoded);
|
||||||
|
|
||||||
if (res != 0)
|
if (res != 0)
|
||||||
{
|
{
|
||||||
|
@ -94,15 +128,15 @@ int main(int argc, char **argv)
|
||||||
return EXIT_FAILURE;
|
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;
|
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)
|
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) };
|
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;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue