From 1de2ff02bb746607727900180b6f32ded0cd7856 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 23 Nov 2022 18:54:18 +0100 Subject: Allow opening 4chan post directly --- src/plugins/Fourchan.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++----- src/plugins/Info.cpp | 10 ++++++++++ 2 files changed, 54 insertions(+), 5 deletions(-) (limited to 'src/plugins') diff --git a/src/plugins/Fourchan.cpp b/src/plugins/Fourchan.cpp index cf229d4..1ce97c4 100644 --- a/src/plugins/Fourchan.cpp +++ b/src/plugins/Fourchan.cpp @@ -19,6 +19,43 @@ static const std::string fourchan_image_url = "https://i.4cdn.org/"; static const char *SERVICE_NAME = "4chan"; namespace QuickMedia { + bool fourchan_extract_url(const std::string &url, std::string &board_id, std::string &thread_id, std::string &post_id) { + size_t len = 10; + size_t index = url.find("4chan.org/"); + if(index == std::string::npos) { + len = 13; + index = url.find("4channel.org/"); + } + + if(index == std::string::npos) + return false; + + index += len; + size_t board_end = url.find('/', index); + if(board_end == std::string::npos) + return false; + + board_id = url.substr(index, board_end - index); + index = board_end + 1; + + const std::string_view remaining(url.data() + index, url.size() - index); + if(remaining.size() <= 7 || remaining.substr(0, 7) != "thread/") + return false; + + index += 7; + size_t thread_id_end = url.find("#p", index); + if(thread_id_end == std::string::npos) + thread_id_end = url.size(); + + thread_id = url.substr(index, thread_id_end - index); + if(thread_id.empty()) + return false; + + index = thread_id_end + 2; + post_id = url.substr(index); + return true; + } + // Returns empty string on failure to read cookie static std::string get_pass_id_from_cookies_file(const Path &cookies_filepath) { std::string file_content; @@ -271,9 +308,9 @@ namespace QuickMedia { // TODO: Link this quote to a 4chan archive that still has the quoted comment (if available) comment_text += Text::formatted_text(std::move(cp.text) + " (DEAD)", get_theme().attention_alert_text_color, FORMATTED_TEXT_FLAG_COLOR); } else { - result_items[body_item_index]->replies_to.push_back(it->second); - result_items[it->second]->replies.push_back(body_item_index); - result_items[it->second]->add_reaction(">>" + result_items[body_item_index]->post_number, nullptr, get_theme().replies_text_color); + static_cast(result_items[body_item_index]->extra.get())->replies_to.push_back(it->second); + static_cast(result_items[it->second]->extra.get())->replies.push_back(body_item_index); + result_items[it->second]->add_reaction(">>" + std::to_string(static_cast(result_items[body_item_index]->extra.get())->post_id), nullptr, get_theme().replies_text_color); if(it->second == 0) comment_text += Text::formatted_text(std::move(cp.text) + " (OP)", get_theme().attention_alert_text_color, FORMATTED_TEXT_FLAG_COLOR); else @@ -391,7 +428,7 @@ namespace QuickMedia { } PluginResult FourchanThreadListPage::submit(const SubmitArgs &args, std::vector &result_tabs) { - result_tabs.push_back(Tab{create_body(), std::make_unique(program, board_id, args.url, pass_id), nullptr}); + result_tabs.push_back(Tab{create_body(), std::make_unique(program, board_id, args.url, "", pass_id), nullptr}); return PluginResult::OK; } @@ -500,8 +537,10 @@ namespace QuickMedia { int64_t post_num_int = post_num.asInt64(); comment_by_postno[post_num_int] = result_items.size(); + auto image_board_post_data = std::make_shared(); + image_board_post_data->post_id = post_num_int; result_items.push_back(BodyItem::create("")); - result_items.back()->post_number = std::to_string(post_num_int); + result_items.back()->extra = std::move(image_board_post_data); } size_t body_item_index = 0; diff --git a/src/plugins/Info.cpp b/src/plugins/Info.cpp index 1ad7320..d4ee5f1 100644 --- a/src/plugins/Info.cpp +++ b/src/plugins/Info.cpp @@ -1,6 +1,7 @@ #include "../../plugins/Info.hpp" #include "../../plugins/Saucenao.hpp" #include "../../plugins/Youtube.hpp" +#include "../../plugins/Fourchan.hpp" #include "../../include/StringUtils.hpp" #include "../../include/Program.hpp" #include "../../include/Notification.hpp" @@ -40,6 +41,7 @@ namespace QuickMedia { } PluginResult InfoPage::submit(const SubmitArgs &args, std::vector &result_tabs) { + std::string board_id, thread_id, post_id; if(string_starts_with(args.url, REVERSE_IMAGE_SEARCH_URL)) { std::string image_url = args.url.substr(strlen(REVERSE_IMAGE_SEARCH_URL)); result_tabs.push_back(Tab{create_body(), std::make_unique(program, image_url, false), nullptr}); @@ -54,6 +56,9 @@ namespace QuickMedia { } else if(is_youtube_url(args.url)) { result_tabs.push_back(Tab{nullptr, std::make_unique(program, args.url, false), nullptr}); return PluginResult::OK; + } else if(fourchan_extract_url(args.url, board_id, thread_id, post_id)) { + result_tabs.push_back(Tab{create_body(), std::make_unique(program, std::move(board_id), std::move(thread_id), std::move(post_id), ""), nullptr}); + return PluginResult::OK; } else { return open_with_browser(args.url); } @@ -75,13 +80,18 @@ namespace QuickMedia { // static std::shared_ptr InfoPage::add_url(const std::string &url) { + std::string board_id, thread_id, comment_id; std::string title; + if(is_youtube_channel_url(url)) title = "Open youtube channel " + url; else if(is_youtube_url(url)) title = "Play " + url; + else if(fourchan_extract_url(url, board_id, thread_id, comment_id)) + title = "Open " + url; else title = "Open " + url + " in a browser"; + auto body_item = BodyItem::create(std::move(title)); body_item->url = url; return body_item; -- cgit v1.2.3