aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/Saucenao.cpp
blob: d81e233931bbcd7c560d2b86d16b7cd76a536467 (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 {
            additional_args.push_back({ "--form-string", "url=" + path });
        }

        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(), website_data.size());
        if(result != 0)
            goto cleanup;

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

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

        return PluginResult::OK;
    }
}