From d0f54de4520990c8465bf2b78a1ce8c0161d46d7 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 17 Jun 2021 10:24:55 +0200 Subject: Properly support youtube timestamp in url --- src/plugins/Youtube.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'src/plugins/Youtube.cpp') diff --git a/src/plugins/Youtube.cpp b/src/plugins/Youtube.cpp index d9d9239..924bf14 100644 --- a/src/plugins/Youtube.cpp +++ b/src/plugins/Youtube.cpp @@ -1783,6 +1783,43 @@ R"END( return ""; } + static int youtube_url_timestamp_to_seconds(const std::string ×tamp) { + int hours = 0; + int minutes = 0; + int seconds = 0; + if(sscanf(timestamp.c_str(), "%dh%dm%d", &hours, &minutes, &seconds) == 3) + return (hours * 60 * 60) + (minutes * 60) + seconds; + if(sscanf(timestamp.c_str(), "%dm%d", &minutes, &seconds) == 2) + return (minutes * 60) + seconds; + if(sscanf(timestamp.c_str(), "%d", &seconds) == 1) + return seconds; + return 0; + } + + static void youtube_url_remove_timestamp(std::string &url, std::string ×tamp) { + size_t timestamp_start = url.find("&t="); + if(timestamp_start == std::string::npos) + return; + + size_t timestamp_end = url.find("&", timestamp_start + 3); + if(timestamp_end == std::string::npos) + timestamp_end = url.size(); + + int timestamp_seconds = youtube_url_timestamp_to_seconds(url.substr(timestamp_start + 3, timestamp_end - (timestamp_start + 3))); + timestamp = std::to_string(timestamp_seconds); + url.erase(timestamp_start, timestamp_end - timestamp_start); + return; + } + + YoutubeVideoPage::YoutubeVideoPage(Program *program, std::string url) : VideoPage(program, "") { + set_url(std::move(url)); + } + + void YoutubeVideoPage::set_url(std::string new_url) { + youtube_url_remove_timestamp(new_url, timestamp); + VideoPage::set_url(std::move(new_url)); + } + BodyItems YoutubeVideoPage::get_related_media(const std::string &url) { BodyItems result_items; -- cgit v1.2.3