aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/Saucenao.cpp
blob: 1278bedf4f425c641a0300074fba79676260c5b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "../../plugins/Saucenao.hpp"
#include "../../include/StringUtils.hpp"
#include <quickmedia/HtmlSearch.h>

namespace QuickMedia {
    PluginResult SaucenaoPage::lazy_fetch(BodyItems &result_items) {
        std::vector<CommandArg> additional_args;
        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 });
        }

        std::string website_data;
        DownloadResult download_result = download_to_string("https://saucenao.com/search.php", website_data, std::move(additional_args), 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());
        if(result != 0)
            goto cleanup;

        result = quickmedia_html_find_nodes_xpath(&html_search, "//td[class='resulttablecontent']",
            [](QuickMediaHtmlNode *node, void *userdata) {
                BodyItems *item_data = (BodyItems*)userdata;
                const char *text = quickmedia_html_node_get_text(node);
                if(text) {
                    std::string title = text;
                    size_t p_index = title.find("%");
                    if(p_index != std::string::npos)
                        title = title.erase(0, p_index + 1);
                    auto item = BodyItem::create(strip(title));
                    item_data->push_back(std::move(item));
                }
            }, &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, "//td[class='resulttableimage']//img",
            [](QuickMediaHtmlNode *node, void *userdata) {
                BodyItemContext *item_data = (BodyItemContext*)userdata;
                const char *src = quickmedia_html_node_get_attribute_value(node, "src");
                const char *data_src = quickmedia_html_node_get_attribute_value(node, "data-src");
                const char *image_url = data_src ? data_src : src;
                if(image_url && item_data->index < item_data->body_items->size()) {
                    (*item_data->body_items)[item_data->index]->thumbnail_url = strip(image_url);
                    (*item_data->body_items)[item_data->index]->thumbnail_size = sf::Vector2i(150, 147);
                    item_data->index++;
                }
            }, &body_item_context);

        cleanup:
        quickmedia_html_search_deinit(&html_search);
        if(result != 0) {
            result_items.clear();
            return PluginResult::ERR;
        }

        return PluginResult::OK;
    }
}