diff options
author | dec05eba <dec05eba@protonmail.com> | 2019-08-02 20:08:27 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2019-08-02 20:08:27 +0200 |
commit | 65cf7681a04f2511db8c7829e9828b53a6676c88 (patch) | |
tree | 6e7db509cf2bce4a10fa6d8bc89a9eae88d893fa /src/Manganelo.cpp | |
parent | 109c797d7c89223c9eed07dc322448b5c5fe3373 (diff) |
Convert to sfml, starting on manganelo and youtube
Diffstat (limited to 'src/Manganelo.cpp')
-rw-r--r-- | src/Manganelo.cpp | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/Manganelo.cpp b/src/Manganelo.cpp new file mode 100644 index 0000000..56a6d50 --- /dev/null +++ b/src/Manganelo.cpp @@ -0,0 +1,51 @@ +#include "../plugins/Manganelo.hpp" +#include <quickmedia/HtmlSearch.h> + +namespace QuickMedia { + SearchResult Manganelo::search(const std::string &text, std::vector<std::unique_ptr<BodyItem>> &result_items) { + std::string url = "https://manganelo.com/search/"; + // TODO: Escape @text. Need to replace space with underscore for example. + url += text; + + std::string website_data; + if(download_to_string(url, website_data) != DownloadResult::OK) + return SearchResult::NET_ERR; + + struct ItemData { + std::vector<std::unique_ptr<BodyItem>> &result_items; + size_t item_index; + }; + + ItemData item_data = { result_items, 0 }; + + QuickMediaHtmlSearch html_search; + int result = quickmedia_html_search_init(&html_search, website_data.c_str()); + if(result != 0) + goto cleanup; + + result = quickmedia_html_find_nodes_xpath(&html_search, "//h3[class=\"story_name\"]/a", + [](QuickMediaHtmlNode *node, void *userdata) { + ItemData *item_data = (ItemData*)userdata; + const char *href = quickmedia_html_node_get_attribute_value(node, "href"); + const char *text = quickmedia_html_node_get_text(node); + auto item = std::make_unique<BodyItem>(text); + item_data->result_items.push_back(std::move(item)); + }, &item_data); + if (result != 0) + goto cleanup; + + result = quickmedia_html_find_nodes_xpath(&html_search, "//div[class=\"story_item\"]//img", + [](QuickMediaHtmlNode *node, void *userdata) { + ItemData *item_data = (ItemData*)userdata; + const char *src = quickmedia_html_node_get_attribute_value(node, "src"); + if(item_data->item_index < item_data->result_items.size()) { + item_data->result_items[item_data->item_index]->cover_url = src; + ++item_data->item_index; + } + }, &item_data); + + cleanup: + quickmedia_html_search_deinit(&html_search); + return result == 0 ? SearchResult::OK : SearchResult::ERR; + } +}
\ No newline at end of file |