From 89c41c1488854858e02ff6bd48a6518161fa05a5 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 22 Nov 2022 01:44:39 +0100 Subject: Allow launching directly into 4chan thread --- src/plugins/Fourchan.cpp | 173 ++++++++++++++++++++++++----------------------- 1 file changed, 87 insertions(+), 86 deletions(-) (limited to 'src/plugins/Fourchan.cpp') diff --git a/src/plugins/Fourchan.cpp b/src/plugins/Fourchan.cpp index 4a9d2d7..cf229d4 100644 --- a/src/plugins/Fourchan.cpp +++ b/src/plugins/Fourchan.cpp @@ -390,16 +390,100 @@ namespace QuickMedia { needs_refresh = true; } - // TODO: Merge with lazy fetch 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}); + return PluginResult::OK; + } + + PluginResult FourchanThreadListPage::lazy_fetch(BodyItems &result_items) { Json::Value json_root; - DownloadResult result = download_json(json_root, fourchan_url + board_id + "/thread/" + args.url + ".json", {}, true); + DownloadResult result = download_json(json_root, fourchan_url + board_id + "/catalog.json?s=Index", {}, true); + if(result != DownloadResult::OK) return download_result_to_plugin_result(result); + + if(!json_root.isArray()) + return PluginResult::ERR; + + std::unordered_map comment_by_postno; + for(const Json::Value &page_data : json_root) { + if(!page_data.isObject()) + continue; + + const Json::Value &threads = page_data["threads"]; + if(!threads.isArray()) + continue; + + for(const Json::Value &thread : threads) { + if(!thread.isObject()) + continue; + + const Json::Value &sub = thread["sub"]; + const char *sub_begin = ""; + const char *sub_end = sub_begin; + sub.getString(&sub_begin, &sub_end); + + const Json::Value &com = thread["com"]; + const char *comment_begin = ""; + const char *comment_end = comment_begin; + com.getString(&comment_begin, &comment_end); + + const Json::Value &thread_num = thread["no"]; + if(!thread_num.isNumeric()) + continue; + + std::string title_text = html_to_text(sub_begin, sub_end - sub_begin, comment_by_postno, result_items, 0); + if(!title_text.empty() && title_text.back() == '\n') + title_text.back() = ' '; + + std::string comment_text = html_to_text(comment_begin, comment_end - comment_begin, comment_by_postno, result_items, 0); + + auto body_item = BodyItem::create(std::move(comment_text)); + body_item->set_title_max_lines(6); + body_item->set_author(std::move(title_text)); + body_item->url = std::to_string(thread_num.asInt64()); + + const Json::Value &ext = thread["ext"]; + const Json::Value &tim = thread["tim"]; + if(tim.isNumeric() && ext.isString()) { + std::string ext_str = ext.asString(); + if(ext_str == ".png" || ext_str == ".jpg" || ext_str == ".jpeg" || ext_str == ".webm" || ext_str == ".mp4" || ext_str == ".gif") { + } else { + fprintf(stderr, "TODO: Support file extension: %s\n", ext_str.c_str()); + } + // "s" means small, that's the url 4chan uses for thumbnails. + // thumbnails always has .jpg extension even if they are gifs or webm. + body_item->thumbnail_url = fourchan_image_url + board_id + "/" + std::to_string(tim.asInt64()) + "s.jpg"; + + mgl::vec2i thumbnail_size(64, 64); + const Json::Value &tn_w = thread["tn_w"]; + const Json::Value &tn_h = thread["tn_h"]; + if(tn_w.isNumeric() && tn_h.isNumeric()) + thumbnail_size = mgl::vec2i(tn_w.asInt() / 2, tn_h.asInt() / 2); + body_item->thumbnail_size = std::move(thumbnail_size); + } + + result_items.push_back(std::move(body_item)); + } + } + + return PluginResult::OK; + } + + static std::string file_get_filename(const std::string &filepath) { + size_t index = filepath.rfind('/'); + if(index == std::string::npos) + return filepath.c_str(); + return filepath.c_str() + index + 1; + } + + // TODO: Merge with FourchanThreadListPage lazy fetch + PluginResult FourchanThreadPage::lazy_fetch(BodyItems &result_items) { + Json::Value json_root; + DownloadResult result = download_json(json_root, fourchan_url + board_id + "/thread/" + thread_id + ".json", {}, true); if(result != DownloadResult::OK) return download_result_to_plugin_result(result); if(!json_root.isObject()) return PluginResult::ERR; - BodyItems result_items; std::unordered_map comment_by_postno; const Json::Value &posts = json_root["posts"]; @@ -484,92 +568,9 @@ namespace QuickMedia { ++body_item_index; } - auto body = create_body(false); - body->set_items(std::move(result_items)); - result_tabs.push_back(Tab{std::move(body), std::make_unique(program, board_id, args.url, pass_id), nullptr}); return PluginResult::OK; } - PluginResult FourchanThreadListPage::lazy_fetch(BodyItems &result_items) { - Json::Value json_root; - DownloadResult result = download_json(json_root, fourchan_url + board_id + "/catalog.json?s=Index", {}, true); - if(result != DownloadResult::OK) return download_result_to_plugin_result(result); - - if(!json_root.isArray()) - return PluginResult::ERR; - - std::unordered_map comment_by_postno; - for(const Json::Value &page_data : json_root) { - if(!page_data.isObject()) - continue; - - const Json::Value &threads = page_data["threads"]; - if(!threads.isArray()) - continue; - - for(const Json::Value &thread : threads) { - if(!thread.isObject()) - continue; - - const Json::Value &sub = thread["sub"]; - const char *sub_begin = ""; - const char *sub_end = sub_begin; - sub.getString(&sub_begin, &sub_end); - - const Json::Value &com = thread["com"]; - const char *comment_begin = ""; - const char *comment_end = comment_begin; - com.getString(&comment_begin, &comment_end); - - const Json::Value &thread_num = thread["no"]; - if(!thread_num.isNumeric()) - continue; - - std::string title_text = html_to_text(sub_begin, sub_end - sub_begin, comment_by_postno, result_items, 0); - if(!title_text.empty() && title_text.back() == '\n') - title_text.back() = ' '; - - std::string comment_text = html_to_text(comment_begin, comment_end - comment_begin, comment_by_postno, result_items, 0); - - auto body_item = BodyItem::create(std::move(comment_text)); - body_item->set_title_max_lines(6); - body_item->set_author(std::move(title_text)); - body_item->url = std::to_string(thread_num.asInt64()); - - const Json::Value &ext = thread["ext"]; - const Json::Value &tim = thread["tim"]; - if(tim.isNumeric() && ext.isString()) { - std::string ext_str = ext.asString(); - if(ext_str == ".png" || ext_str == ".jpg" || ext_str == ".jpeg" || ext_str == ".webm" || ext_str == ".mp4" || ext_str == ".gif") { - } else { - fprintf(stderr, "TODO: Support file extension: %s\n", ext_str.c_str()); - } - // "s" means small, that's the url 4chan uses for thumbnails. - // thumbnails always has .jpg extension even if they are gifs or webm. - body_item->thumbnail_url = fourchan_image_url + board_id + "/" + std::to_string(tim.asInt64()) + "s.jpg"; - - mgl::vec2i thumbnail_size(64, 64); - const Json::Value &tn_w = thread["tn_w"]; - const Json::Value &tn_h = thread["tn_h"]; - if(tn_w.isNumeric() && tn_h.isNumeric()) - thumbnail_size = mgl::vec2i(tn_w.asInt() / 2, tn_h.asInt() / 2); - body_item->thumbnail_size = std::move(thumbnail_size); - } - - result_items.push_back(std::move(body_item)); - } - } - - return PluginResult::OK; - } - - static std::string file_get_filename(const std::string &filepath) { - size_t index = filepath.rfind('/'); - if(index == std::string::npos) - return filepath.c_str(); - return filepath.c_str() + index + 1; - } - PostResult FourchanThreadPage::post_comment(const std::string &captcha_id, const std::string &captcha_solution, const std::string &comment, const std::string &filepath) { Path cookies_filepath; if(get_cookies_filepath(cookies_filepath, SERVICE_NAME) != 0) { -- cgit v1.2.3