diff options
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/Youtube.cpp | 106 |
1 files changed, 34 insertions, 72 deletions
diff --git a/src/plugins/Youtube.cpp b/src/plugins/Youtube.cpp index 9e84903..a9a2811 100644 --- a/src/plugins/Youtube.cpp +++ b/src/plugins/Youtube.cpp @@ -4,15 +4,8 @@ #include <unordered_set> namespace QuickMedia { - static std::shared_ptr<BodyItem> parse_content_video_renderer(const Json::Value &content_item_json, std::unordered_set<std::string> &added_videos) { - if(!content_item_json.isObject()) - return nullptr; - - const Json::Value &video_renderer_json = content_item_json["videoRenderer"]; - if(!video_renderer_json.isObject()) - return nullptr; - - const Json::Value &video_id_json = video_renderer_json["videoId"]; + static std::shared_ptr<BodyItem> parse_common_video_item(const Json::Value &video_item_json, std::unordered_set<std::string> &added_videos) { + const Json::Value &video_id_json = video_item_json["videoId"]; if(!video_id_json.isString()) return nullptr; @@ -23,7 +16,7 @@ namespace QuickMedia { std::string thumbnail_url = "https://img.youtube.com/vi/" + video_id_str + "/hqdefault.jpg"; const char *date = nullptr; - const Json::Value &published_time_text_json = video_renderer_json["publishedTimeText"]; + const Json::Value &published_time_text_json = video_item_json["publishedTimeText"]; if(published_time_text_json.isObject()) { const Json::Value &text_json = published_time_text_json["simpleText"]; if(text_json.isString()) @@ -31,7 +24,7 @@ namespace QuickMedia { } const char *length = nullptr; - const Json::Value &length_text_json = video_renderer_json["lengthText"]; + const Json::Value &length_text_json = video_item_json["lengthText"]; if(length_text_json.isObject()) { const Json::Value &text_json = length_text_json["simpleText"]; if(text_json.isString()) @@ -39,15 +32,25 @@ namespace QuickMedia { } const char *title = nullptr; - const Json::Value &title_json = video_renderer_json["title"]; + const Json::Value &title_json = video_item_json["title"]; if(title_json.isObject()) { - const Json::Value &runs_json = title_json["runs"]; - if(runs_json.isArray() && !runs_json.empty()) { - const Json::Value &first_runs_json = runs_json[0]; - if(first_runs_json.isObject()) { - const Json::Value &text_json = first_runs_json["text"]; - if(text_json.isString()) - title = text_json.asCString(); + const Json::Value &simple_text_json = title_json["simpleText"]; + if(simple_text_json.isString()) { + title = simple_text_json.asCString(); + } + } + + if(!title) { + const Json::Value &title_json = video_item_json["title"]; + if(title_json.isObject()) { + const Json::Value &runs_json = title_json["runs"]; + if(runs_json.isArray() && !runs_json.empty()) { + const Json::Value &first_runs_json = runs_json[0]; + if(first_runs_json.isObject()) { + const Json::Value &text_json = first_runs_json["text"]; + if(text_json.isString()) + title = text_json.asCString(); + } } } } @@ -72,6 +75,17 @@ namespace QuickMedia { return body_item; } + static std::shared_ptr<BodyItem> parse_content_video_renderer(const Json::Value &content_item_json, std::unordered_set<std::string> &added_videos) { + if(!content_item_json.isObject()) + return nullptr; + + const Json::Value &video_renderer_json = content_item_json["videoRenderer"]; + if(!video_renderer_json.isObject()) + return nullptr; + + return parse_common_video_item(video_renderer_json, added_videos); + } + // Returns empty string if continuation token can't be found static std::string item_section_renderer_get_continuation_token(const Json::Value &item_section_renderer_json) { const Json::Value &continuations_json = item_section_renderer_json["continuations"]; @@ -150,60 +164,8 @@ namespace QuickMedia { const Json::Value &compact_video_renderer_json = item_json["compactVideoRenderer"]; if(!compact_video_renderer_json.isObject()) return nullptr; - - const Json::Value &video_id_json = compact_video_renderer_json["videoId"]; - if(!video_id_json.isString()) - return nullptr; - - std::string video_id_str = video_id_json.asString(); - if(added_videos.find(video_id_str) != added_videos.end()) - return nullptr; - - std::string thumbnail_url = "https://img.youtube.com/vi/" + video_id_str + "/hqdefault.jpg"; - - const char *date = nullptr; - const Json::Value &published_time_text_json = compact_video_renderer_json["publishedTimeText"]; - if(published_time_text_json.isObject()) { - const Json::Value &text_json = published_time_text_json["simpleText"]; - if(text_json.isString()) - date = text_json.asCString(); - } - - const char *length = nullptr; - const Json::Value &length_text_json = compact_video_renderer_json["lengthText"]; - if(length_text_json.isObject()) { - const Json::Value &text_json = length_text_json["simpleText"]; - if(text_json.isString()) - length = text_json.asCString(); - } - - const char *title = nullptr; - const Json::Value &title_json = compact_video_renderer_json["title"]; - if(title_json.isObject()) { - const Json::Value &simple_text_json = title_json["simpleText"]; - if(simple_text_json.isString()) { - title = simple_text_json.asCString(); - } - } - if(!title) - return nullptr; - - auto body_item = BodyItem::create(title); - /* TODO: Make date a different color */ - std::string date_str; - if(date) - date_str += date; - if(length) { - if(!date_str.empty()) - date_str += '\n'; - date_str += length; - } - body_item->set_description(std::move(date_str)); - body_item->url = "https://www.youtube.com/watch?v=" + video_id_str; - body_item->thumbnail_url = std::move(thumbnail_url); - added_videos.insert(video_id_str); - return body_item; + return parse_common_video_item(compact_video_renderer_json, added_videos); } SearchResult YoutubeSearchPage::search(const std::string &str, BodyItems &result_items) { |