aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/Manganelo.cpp
blob: b260deab59ecff0a2fda94704a5e9dc8dc678864 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "../../plugins/Manganelo.hpp"
#include "../../include/Notification.hpp"
#include "../../include/StringUtils.hpp"
#include "../../include/NetUtils.hpp"
#include <quickmedia/HtmlSearch.h>

namespace QuickMedia {
    // Returns true if modified
    static bool remove_html_span(std::string &str) {
        size_t open_tag_start = str.find("<span");
        if(open_tag_start == std::string::npos)
            return false;

        size_t open_tag_end = str.find('>', open_tag_start + 5);
        if(open_tag_end == std::string::npos)
            return false;

        str.erase(open_tag_start, open_tag_end - open_tag_start + 1);

        size_t close_tag = str.find("</span>");
        if(close_tag == std::string::npos)
            return true;

        str.erase(close_tag, 7);
        return true;
    }

    SearchResult ManganeloSearchPage::search(const std::string &str, BodyItems &result_items) {
        std::string url = "https://manganelo.com/getstorysearchjson";
        std::string search_term = "searchword=";
        search_term += url_param_encode(str);
        CommandArg data_arg = { "--data", std::move(search_term) };

        Json::Value json_root;
        DownloadResult result = download_json(json_root, url, {data_arg}, true);
        if(result != DownloadResult::OK) return download_result_to_search_result(result);

        if(json_root.isNull())
            return SearchResult::OK;

        if(!json_root.isArray())
            return SearchResult::ERR;

        for(const Json::Value &child : json_root) {
            if(!child.isObject())
                continue;

            Json::Value name = child.get("name", "");
            Json::Value nameunsigned = child.get("nameunsigned", "");
            if(name.isString() && name.asCString()[0] != '\0' && nameunsigned.isString() && nameunsigned.asCString()[0] != '\0') {
                std::string name_str = name.asString();
                while(remove_html_span(name_str)) {}
                auto item = BodyItem::create(strip(name_str));
                item->url = "https://manganelo.com/manga/" + url_param_encode(nameunsigned.asString());
                Json::Value image = child.get("image", "");
                if(image.isString() && image.asCString()[0] != '\0')
                    item->thumbnail_url = image.asString();
                result_items.push_back(std::move(item));
            }
        }

        return SearchResult::OK;
    }

    PluginResult ManganeloSearchPage::submit(const std::string &title, const std::string &url, std::vector<Tab> &result_tabs) {
        BodyItems chapters_items;
        std::vector<Creator> creators;

        std::string website_data;
        if(download_to_string(url, website_data, {}, is_tor_enabled(), true) != DownloadResult::OK)
            return PluginResult::NET_ERR;

        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, "//ul[class='row-content-chapter']//a",
            [](QuickMediaHtmlNode *node, void *userdata) {
                auto *item_data = (BodyItems*)userdata;
                const char *href = quickmedia_html_node_get_attribute_value(node, "href");
                const char *text = quickmedia_html_node_get_text(node);
                if(href && text) {
                    auto item = BodyItem::create(strip(text));
                    item->url = href;
                    item_data->push_back(std::move(item));
                }
            }, &chapters_items);
        
        quickmedia_html_find_nodes_xpath(&html_search, "//a[class='a-h']",
            [](QuickMediaHtmlNode *node, void *userdata) {
                std::vector<Creator> *creators = (std::vector<Creator>*)userdata;
                const char *href = quickmedia_html_node_get_attribute_value(node, "href");
                const char *text = quickmedia_html_node_get_text(node);
                if(href && text && strstr(href, "/author/story/")) {
                    Creator creator;
                    creator.name = strip(text);
                    creator.url = href;
                    creators->push_back(std::move(creator));
                }
            }, &creators);

        cleanup:
        quickmedia_html_search_deinit(&html_search);
        if(result != 0)
            return PluginResult::ERR;

        auto chapters_body = create_body();
        chapters_body->items = std::move(chapters_items);
        result_tabs.push_back(Tab{std::move(chapters_body), std::make_unique<ManganeloChaptersPage>(program, title, url), create_search_bar("Search...", SEARCH_DELAY_FILTER)});

        for(Creator &creator : creators) {
            result_tabs.push_back(Tab{create_body(), std::make_unique<ManganeloCreatorPage>(program, std::move(creator)), create_search_bar("Search...", SEARCH_DELAY_FILTER)});
        }

        std::string manga_id;
        if(extract_id_from_url(url, manga_id)) {
            if(load_manga_content_storage("manganelo", title, manga_id))
                return PluginResult::OK;
        }
        return PluginResult::ERR;
    }
    
    bool ManganeloSearchPage::extract_id_from_url(const std::string &url, std::string &manga_id) const {
        bool manganelo_website = false;
        if(url.find("mangakakalot") != std::string::npos || url.find("manganelo") != std::string::npos)
            manganelo_website = true;

        if(manganelo_website) {
            size_t index = url.find("manga/");
            if(index == std::string::npos) {
                std::string err_msg = "Url ";
                err_msg += url;
                err_msg += " doesn't contain manga id";
                show_notification("QuickMedia", err_msg, Urgency::CRITICAL);
                return false;
            }

            manga_id = url.substr(index + 6);
            if(manga_id.size() <= 2) {
                std::string err_msg = "Url ";
                err_msg += url;
                err_msg += " doesn't contain manga id";
                show_notification("QuickMedia", err_msg, Urgency::CRITICAL);
                return false;
            }
            return true;
        } else {
            std::string err_msg = "Unexpected url ";
            err_msg += url;
            err_msg += " is not manganelo or mangakakalot";
            show_notification("QuickMedia", err_msg, Urgency::CRITICAL);
            return false;
        }
    }

    PluginResult ManganeloChaptersPage::submit(const std::string &title, const std::string &url, std::vector<Tab> &result_tabs) {
        result_tabs.push_back(Tab{create_body(), std::make_unique<ManganeloImagesPage>(program, content_title, title, url), nullptr});
        return PluginResult::OK;
    }

    PluginResult ManganeloCreatorPage::submit(const std::string &title, const std::string &url, std::vector<Tab> &result_tabs) {
        (void)title;
        (void)url;
        (void)result_tabs;
        // TODO: Implement
        return PluginResult::ERR;
    }

    ImageResult ManganeloImagesPage::get_number_of_images(int &num_images) {
        num_images = 0;
        ImageResult image_result = get_image_urls_for_chapter(url);
        if(image_result != ImageResult::OK)
            return image_result;

        num_images = chapter_image_urls.size();
        return ImageResult::OK;
    }

    ImageResult ManganeloImagesPage::for_each_page_in_chapter(PageCallback callback) {
        std::vector<std::string> image_urls;
        ImageResult image_result = get_image_urls_for_chapter(url);
        if(image_result != ImageResult::OK)
            return image_result;

        image_urls = chapter_image_urls;

        for(const std::string &url : image_urls) {
            if(!callback(url))
                break;
        }

        return ImageResult::OK;
    }

    ImageResult ManganeloImagesPage::get_image_urls_for_chapter(const std::string &url) {
        if(!chapter_image_urls.empty())
            return ImageResult::OK;

        std::string website_data;
        if(download_to_string(url, website_data, {}, is_tor_enabled(), true) != DownloadResult::OK)
            return ImageResult::NET_ERR;

        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, "//div[class='container-chapter-reader']/img",
            [](QuickMediaHtmlNode *node, void *userdata) {
                auto *urls = (std::vector<std::string>*)userdata;
                const char *src = quickmedia_html_node_get_attribute_value(node, "src");
                if(src) {
                    std::string image_url = src;
                    urls->push_back(std::move(image_url));
                }
            }, &chapter_image_urls);

        cleanup:
        quickmedia_html_search_deinit(&html_search);
        if(result != 0) {
            chapter_image_urls.clear();
            return ImageResult::ERR;
        }
        if(chapter_image_urls.empty())
            return ImageResult::ERR;
        return ImageResult::OK;
    }
}