diff options
author | dec05eba <dec05eba@protonmail.com> | 2021-07-19 19:28:45 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-07-19 19:28:45 +0200 |
commit | cd4d2e98c34ed413ca164dbad61ba19636377f77 (patch) | |
tree | 69a1ab7f19acc4b9abf016025921efed3b80c2a9 /src/plugins | |
parent | e671784144174c4fceaa6df3737ba9b4de4a6c63 (diff) |
Add hotexamples
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/Fourchan.cpp | 23 | ||||
-rw-r--r-- | src/plugins/HotExamples.cpp | 139 | ||||
-rw-r--r-- | src/plugins/MangaGeneric.cpp | 5 | ||||
-rw-r--r-- | src/plugins/Saucenao.cpp | 5 |
4 files changed, 149 insertions, 23 deletions
diff --git a/src/plugins/Fourchan.cpp b/src/plugins/Fourchan.cpp index 4b2ca61..d4fb726 100644 --- a/src/plugins/Fourchan.cpp +++ b/src/plugins/Fourchan.cpp @@ -462,9 +462,9 @@ namespace QuickMedia { } std::vector<CommandArg> additional_args = { - CommandArg{"-F", "id=" + token}, - CommandArg{"-F", "pin=" + pin}, - CommandArg{"-F", "xhr=1"}, + CommandArg{"--form-string", "id=" + token}, + CommandArg{"--form-string", "pin=" + pin}, + CommandArg{"--form-string", "xhr=1"}, CommandArg{"-c", cookies_filepath.data} }; @@ -502,29 +502,22 @@ namespace QuickMedia { PostResult FourchanThreadPage::post_comment(const std::string &captcha_id, const std::string &comment, const std::string &filepath) { std::string url = "https://sys.4chan.org/" + board_id + "/post"; - std::string comment_fixed = comment; - if(comment_fixed[0] == '@') - comment_fixed = "\\" + comment_fixed; std::vector<CommandArg> additional_args = { CommandArg{"-H", "Referer: https://boards.4chan.org/"}, CommandArg{"-H", "Origin: https://boards.4chan.org"}, - CommandArg{"-F", "resto=" + thread_id}, - CommandArg{"-F", "com=" + comment_fixed}, - CommandArg{"-F", "mode=regist"} + CommandArg{"--form-string", "resto=" + thread_id}, + CommandArg{"--form-string", "com=" + comment}, + CommandArg{"--form-string", "mode=regist"} }; if(!filepath.empty()) { - std::string filename = file_get_filename(filepath); - if(filename[0] == '@') - filename = "\\" + filename; - additional_args.push_back({ "-F", "upfile=@" + filepath }); - additional_args.push_back({ "-F", "filename=" + filename }); + additional_args.push_back({ "--form-string", "filename=" + file_get_filename(filepath) }); } if(pass_id.empty()) { - additional_args.push_back(CommandArg{"-F", "g-recaptcha-response=" + captcha_id}); + additional_args.push_back(CommandArg{"--form-string", "g-recaptcha-response=" + captcha_id}); } else { Path cookies_filepath; if(get_cookies_filepath(cookies_filepath, SERVICE_NAME) != 0) { diff --git a/src/plugins/HotExamples.cpp b/src/plugins/HotExamples.cpp new file mode 100644 index 0000000..02f1217 --- /dev/null +++ b/src/plugins/HotExamples.cpp @@ -0,0 +1,139 @@ +#include "../../plugins/HotExamples.hpp" +#include "../../include/Theme.hpp" +#include "../../include/StringUtils.hpp" +#include <quickmedia/HtmlSearch.h> + +namespace QuickMedia { + static std::shared_ptr<BodyItem> create_body_item_with_url(const std::string &title, const std::string &url) { + auto body_item = BodyItem::create(title); + body_item->url = url; + return body_item; + } + + void hot_examples_front_page_fill(BodyItems &body_items) { + body_items.push_back(create_body_item_with_url("C++", "cpp")); + body_items.push_back(create_body_item_with_url("C#", "csharp")); + body_items.push_back(create_body_item_with_url("Go", "go")); + body_items.push_back(create_body_item_with_url("Java", "java")); + body_items.push_back(create_body_item_with_url("JavaScript", "javascript")); + body_items.push_back(create_body_item_with_url("PHP", "php")); + body_items.push_back(create_body_item_with_url("Python", "python")); + body_items.push_back(create_body_item_with_url("TypeScript", "typescript")); + } + + PluginResult HotExamplesLanguageSelectPage::submit(const std::string&, const std::string &url, std::vector<Tab> &result_tabs) { + result_tabs.push_back({ create_body(), std::make_unique<HotExamplesSearchPage>(program, url), create_search_bar("Search...", 500) }); + return PluginResult::OK; + } + + SearchResult HotExamplesSearchPage::search(const std::string &str, BodyItems &result_items) { + std::vector<CommandArg> additional_args = { + { "-H", "content-type: application/x-www-form-urlencoded" }, + { "--data-raw", "SearchForm[lang]=" + language + "&SearchForm[search]=" + url_param_encode(str) } + }; + + std::string website_data; + DownloadResult download_result = download_to_string("https://hotexamples.com/search", website_data, additional_args, true); + if(download_result != DownloadResult::OK) return download_result_to_search_result(download_result); + + QuickMediaHtmlSearch html_search; + int result = quickmedia_html_search_init(&html_search, website_data.c_str(), website_data.size()); + if(result != 0) + return SearchResult::ERR; + + quickmedia_html_find_nodes_xpath(&html_search, "//div[class='search-result row']//div[class='header']//a", + [](QuickMediaMatchNode *node, void *userdata) { + auto *item_data = (BodyItems*)userdata; + QuickMediaStringView href = quickmedia_html_node_get_attribute_value(node, "href"); + if(href.data && memmem(href.data, href.size, "/examples/", 10)) { + QuickMediaStringView text = quickmedia_html_node_get_text(node); + if(text.data) { + std::string title(text.data, text.size); + html_unescape_sequences(title); + + auto item = BodyItem::create(std::move(title)); + item->url.assign(href.data, href.size); + item_data->push_back(std::move(item)); + } + } + return 0; + }, &result_items); + + BodyItemContext body_item_context; + body_item_context.body_items = &result_items; + body_item_context.index = 0; + + quickmedia_html_find_nodes_xpath(&html_search, "//div[class='search-result row']//span[class='count']", + [](QuickMediaMatchNode *node, void *userdata) { + auto *item_data = (BodyItemContext*)userdata; + QuickMediaStringView text = quickmedia_html_node_get_text(node); + if(text.data && item_data->index < item_data->body_items->size()) { + std::string desc(text.data, text.size); + html_unescape_sequences(desc); + + (*item_data->body_items)[item_data->index]->set_description(std::move(desc)); + (*item_data->body_items)[item_data->index]->set_description_color(get_current_theme().faded_text_color); + item_data->index++; + } + return 0; + }, &body_item_context); + + quickmedia_html_search_deinit(&html_search); + return SearchResult::OK; + } + + PluginResult HotExamplesSearchPage::submit(const std::string &title, const std::string &url, std::vector<Tab> &result_tabs) { + BodyItems result_items; + std::string website_data; + DownloadResult download_result = download_to_string(url, website_data, {}, true); + if(download_result != DownloadResult::OK) return download_result_to_plugin_result(download_result); + + QuickMediaHtmlSearch html_search; + int result = quickmedia_html_search_init(&html_search, website_data.c_str(), website_data.size()); + if(result != 0) + return PluginResult::ERR; + + quickmedia_html_find_nodes_xpath(&html_search, "//div[class='example-item']//div[class='example-project-info']", + [](QuickMediaMatchNode *node, void *userdata) { + auto *item_data = (BodyItems*)userdata; + QuickMediaStringView text = quickmedia_html_node_get_text(node); + if(text.data) { + std::string title(text.data, text.size); + html_unescape_sequences(title); + string_replace_all(title, "Project:", " Project:"); + + auto item = BodyItem::create(std::move(title)); + //item->url.assign(href.data, href.size); + item_data->push_back(std::move(item)); + } + return 0; + }, &result_items); + + BodyItemContext body_item_context; + body_item_context.body_items = &result_items; + body_item_context.index = 0; + + quickmedia_html_find_nodes_xpath(&html_search, "//div[class='example-item']//div[class='example']", + [](QuickMediaMatchNode *node, void *userdata) { + auto *item_data = (BodyItemContext*)userdata; + QuickMediaStringView text = quickmedia_html_node_get_text(node); + if(text.data && item_data->index < item_data->body_items->size()) { + std::string desc(text.data, text.size); + html_unescape_sequences(desc); + + (*item_data->body_items)[item_data->index]->set_description(std::move(desc)); + (*item_data->body_items)[item_data->index]->set_description_color(get_current_theme().text_color); + // TODO: Use monospace + item_data->index++; + } + return 0; + }, &body_item_context); + + quickmedia_html_search_deinit(&html_search); + + auto body = create_body(); + body->items = std::move(result_items); + result_tabs.push_back({ std::move(body), std::make_unique<HotExamplesCodeExamplesPage>(program, title + " code examples"), create_search_bar("Search...", SEARCH_DELAY_FILTER) }); + return PluginResult::OK; + } +}
\ No newline at end of file diff --git a/src/plugins/MangaGeneric.cpp b/src/plugins/MangaGeneric.cpp index a2608ab..29480da 100644 --- a/src/plugins/MangaGeneric.cpp +++ b/src/plugins/MangaGeneric.cpp @@ -151,10 +151,7 @@ namespace QuickMedia { args.push_back({ "-X", "POST" }); args.push_back({ "-H", "x-requested-with: XMLHttpRequest" }); for(const MangaFormDataStr &form : form_data) { - std::string form_value = form.value; - if(form_value[0] == '@') - form_value = "\\" + form_value; - args.push_back({ "-F", std::string(form.key) + "=" + form_value }); + args.push_back({ "--form-string", std::string(form.key) + "=" + form.value }); } assert(search_query.json_handler); diff --git a/src/plugins/Saucenao.cpp b/src/plugins/Saucenao.cpp index e8d8357..fd752c5 100644 --- a/src/plugins/Saucenao.cpp +++ b/src/plugins/Saucenao.cpp @@ -7,10 +7,7 @@ namespace QuickMedia { if(is_local) { additional_args.push_back({ "-F", "file=@" + path }); } else { - std::string url = path; - if(url[0] == '@') - url = "\\" + url; - additional_args.push_back({ "-F", "url=" + url }); + additional_args.push_back({ "--form-string", "url=" + path }); } std::string website_data; |