aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/Mangadex.cpp
blob: 5f6a0a4077480919528f0055dfbd43120f909775 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include "../../plugins/Mangadex.hpp"
#include "../../include/Storage.hpp"
#include "../../include/Notification.hpp"
#include <quickmedia/HtmlSearch.h>
#include <json/reader.h>

static const std::string mangadex_url = "https://mangadex.org";
static const std::string useragent_str = "user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36";

namespace QuickMedia {
    static std::string title_url_extract_manga_id(const std::string &url) {
        size_t find_index = url.find("/title/");
        if(find_index == std::string::npos)
            return "";
        
        size_t id_start_index = find_index + 7;
        size_t end_index = url.find("/", id_start_index);
        if(end_index == std::string::npos)
            return url.substr(id_start_index);
        
        return url.substr(id_start_index, end_index - id_start_index);
    }

    static std::string chapter_url_extract_manga_id(const std::string &url) {
        size_t find_index = url.find("/chapter/");
        if(find_index == std::string::npos)
            return "";
        return url.substr(find_index + 9);
    }

    struct BodyItemChapterContext {
        BodyItems *body_items;
        int prev_chapter_number;
        bool *is_last_page;
    };

    SearchResult Mangadex::search(const std::string &url, BodyItems &result_items) {
        CommandArg user_agent_arg = { "-H", useragent_str };

        std::string manga_id = title_url_extract_manga_id(url);
        std::string request_url = "https://mangadex.org/api/?id=" + manga_id + "&type=manga";
        std::string server_response;
        if(download_to_string(request_url, server_response, {std::move(user_agent_arg)}, use_tor) != DownloadResult::OK)
            return SearchResult::NET_ERR;

        if(server_response.empty())
            return SearchResult::OK;
        
        Json::Value json_root;
        Json::CharReaderBuilder json_builder;
        std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader());
        std::string json_errors;
        if(!json_reader->parse(&server_response[0], &server_response[server_response.size()], &json_root, &json_errors)) {
            fprintf(stderr, "Mangadex search json error: %s\n", json_errors.c_str());
            return SearchResult::ERR;
        }

        Json::Value &status_json = json_root["status"];
        if(!status_json.isString() || status_json.asString() != "OK")
            return SearchResult::ERR;

        Json::Value &chapter_json = json_root["chapter"];
        if(!chapter_json.isObject())
            return SearchResult::ERR;

        std::vector<std::pair<std::string, Json::Value>> chapters(chapter_json.size());
        for(auto &member_name : chapter_json.getMemberNames()) {
            Json::Value chapter = chapter_json[member_name];
            if(chapter.isObject())
                chapters.push_back(std::make_pair(member_name, std::move(chapter)));
        }

        std::sort(chapters.begin(), chapters.end(), [](std::pair<std::string, Json::Value> &a, std::pair<std::string, Json::Value> &b) {
            Json::Value &a_timestamp_json = a.second["timestamp"];
            Json::Value &b_timestamp_json = b.second["timestamp"];
            int64_t a_timestamp = 0;
            int64_t b_timestamp = 0;
            if(a_timestamp_json.isInt64())
                a_timestamp = a_timestamp_json.asInt64();
            if(b_timestamp_json.isInt64())
                b_timestamp = b_timestamp_json.asInt64();
            return a_timestamp > b_timestamp;
        });

        std::string prev_chapter_number;
        for(auto it = chapters.begin(); it != chapters.end(); ++it) {
            const std::string &chapter_id = it->first;
            Json::Value &chapter = it->second;

            Json::Value &lang_code = chapter["lang_code"];
            // TODO: Allow selecting other languages than english
            if(!lang_code.isString() || lang_code.asString() != "gb")
                continue;

            Json::Value &chapter_number_json = chapter["chapter"];
            if(!chapter_number_json.isString())
                continue;

            std::string chapter_number_str = chapter_number_json.asString();
            if(chapter_number_str == prev_chapter_number)
                continue;
            prev_chapter_number = chapter_number_str;

            Json::Value &chapter_title_json = chapter["title"];
            std::string chapter_url = mangadex_url + "/chapter/" + chapter_id;
            std::string chapter_name = std::string("Ch. ") + chapter_number_str;
            if(chapter_title_json.isString() && strlen(chapter_title_json.asCString()) > 0)
                chapter_name += std::string(" - ") + chapter_title_json.asCString();

            auto item = std::make_unique<BodyItem>(std::move(chapter_name));
            item->url = std::move(chapter_url);
            result_items.push_back(std::move(item));
        }
        return SearchResult::OK;
    }

    static bool get_cookie_filepath(std::string &cookie_filepath) {
        Path cookie_path = get_storage_dir().join("cookies").join("mangadex.txt");
        if(create_directory_recursive(cookie_path) != 0) {
            show_notification("Storage", "Failed to create directory: " + cookie_path.data, Urgency::CRITICAL);
            return false;
        }
        cookie_filepath = cookie_path.data;
        return true;
    }

    bool Mangadex::get_rememberme_token(std::string &rememberme_token_output) {
        if(rememberme_token) {
            rememberme_token_output = rememberme_token.value();
            return true;
        }

        Path mangadex_credentials_path = get_storage_dir().join("credentials").join("mangadex.json");
        std::string mangadex_credentials;
        if(file_get_content(mangadex_credentials_path, mangadex_credentials) != 0) {
            fprintf(stderr, "Failed to get content of file: %s\n", mangadex_credentials_path.data.c_str());
            return false;
        }

        Json::Value json_root;
        Json::CharReaderBuilder json_builder;
        std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader());
        std::string json_errors;
        if(!json_reader->parse(&mangadex_credentials[0], &mangadex_credentials[mangadex_credentials.size()], &json_root, &json_errors)) {
            fprintf(stderr, "Mangadex credentials json error: %s\n", json_errors.c_str());
            return false;
        }

        if(json_root.isObject()) {
            Json::Value &rememberme_token_json = json_root["rememberme_token"];
            if(rememberme_token_json.isString())
                rememberme_token = rememberme_token_json.asString();
        }

        rememberme_token = rememberme_token_output;
        return true;
    }

    struct BodyItemImageContext {
        BodyItems *body_items;
        size_t index;
    };

    // TODO: Implement pagination (go to next page and get all results as well)
    SuggestionResult Mangadex::update_search_suggestions(const std::string &text, BodyItems &result_items) {
        std::string rememberme_token;
        if(!get_rememberme_token(rememberme_token))
            return SuggestionResult::ERR;

        std::string url = "https://mangadex.org/search?title=";
        url += url_param_encode(text);
        CommandArg cookie_arg = { "-H", "cookie: mangadex_rememberme_token=" + rememberme_token };
        CommandArg user_agent_arg = { "-H", useragent_str };

        std::string website_data;
        if(download_to_string(url, website_data, {std::move(cookie_arg), std::move(user_agent_arg)}, use_tor) != DownloadResult::OK)
            return SuggestionResult::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, "//a",
            [](QuickMediaHtmlNode *node, void *userdata) {
                auto *item_data = (BodyItems*)userdata;
                const char *href = quickmedia_html_node_get_attribute_value(node, "href");
                const char *title = quickmedia_html_node_get_attribute_value(node, "title");
                if(title && href && strncmp(href, "/title/", 7) == 0) {
                    auto item = std::make_unique<BodyItem>(strip(title));
                    item->url = mangadex_url + href;
                    item_data->push_back(std::move(item));
                }
            }, &result_items);

        BodyItemImageContext body_item_image_context;
        body_item_image_context.body_items = &result_items;
        body_item_image_context.index = 0;

        result = quickmedia_html_find_nodes_xpath(&html_search, "//img",
            [](QuickMediaHtmlNode *node, void *userdata) {
                auto *item_data = (BodyItemImageContext*)userdata;
                const char *src = quickmedia_html_node_get_attribute_value(node, "src");
                if(src && strncmp(src, "/images/manga/", 14) == 0) {
                    if(item_data->index < item_data->body_items->size()) {
                        (*item_data->body_items)[item_data->index]->thumbnail_url = mangadex_url + src;
                        item_data->index++;
                    }
                }
            }, &body_item_image_context);

        cleanup:
        quickmedia_html_search_deinit(&html_search);
        return result == 0 ? SuggestionResult::OK : SuggestionResult::ERR;
    }

    ImageResult Mangadex::get_number_of_images(const std::string &url, int &num_images) {
        std::lock_guard<std::mutex> lock(image_urls_mutex);
        num_images = 0;
        ImageResult image_result = get_image_urls_for_chapter(url);
        if(image_result != ImageResult::OK)
            return image_result;

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

    bool Mangadex::save_mangadex_cookies(const std::string &url, const std::string &cookie_filepath) {
        CommandArg user_agent_arg = { "-H", useragent_str };
        CommandArg cookie_arg = { "-c", std::move(cookie_filepath) };
        std::string server_response;
        if(download_to_string(url, server_response, {std::move(user_agent_arg), std::move(cookie_arg)}, use_tor) != DownloadResult::OK)
            return false;

        return true;
    }

    ImageResult Mangadex::get_image_urls_for_chapter(const std::string &url) {
        if(url == last_chapter_url)
            return ImageResult::OK;

        last_chapter_image_urls.clear();

        std::string cookie_filepath;
        if(!get_cookie_filepath(cookie_filepath))
            return ImageResult::ERR;

        if(!save_mangadex_cookies(url, cookie_filepath))
            return ImageResult::ERR;

        CommandArg user_agent_arg = { "-H", useragent_str };
        CommandArg cookie_arg = { "-b", std::move(cookie_filepath) };
        std::string manga_id = chapter_url_extract_manga_id(url);
        std::string request_url = mangadex_url + "/api/?id=" + manga_id + "&server=null&type=chapter";
        std::string server_response;
        if(download_to_string(request_url, server_response, {std::move(user_agent_arg), std::move(cookie_arg)}, use_tor) != DownloadResult::OK)
            return ImageResult::NET_ERR;

        if(server_response.empty())
            return ImageResult::OK;

        Json::Value json_root;
        Json::CharReaderBuilder json_builder;
        std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader());
        std::string json_errors;
        if(!json_reader->parse(&server_response[0], &server_response[server_response.size()], &json_root, &json_errors)) {
            fprintf(stderr, "Mangadex image urls json error: %s\n", json_errors.c_str());
            return ImageResult::ERR;
        }

        Json::Value &status_json = json_root["status"];
        if(!status_json.isString() || status_json.asString() != "OK")
            return ImageResult::ERR;

        Json::Value &chapter_hash = json_root["hash"];
        if(!chapter_hash.isString())
            return ImageResult::ERR;
        const char *chapter_hash_str = chapter_hash.asCString();

        Json::Value &server_json = json_root["server"];
        std::string server;
        if(server_json.isString())
            server = server_json.asString();
        else
            server = mangadex_url + "/data/";

        Json::Value &page_array = json_root["page_array"];
        if(page_array.isArray()) {
            for(const Json::Value &image_name : page_array) {
                if(!image_name.isString())
                    continue;

                std::string image_url = server + chapter_hash_str + "/" + image_name.asCString();
                last_chapter_image_urls.push_back(std::move(image_url));
            }
        }

        last_chapter_url = url;
        if(last_chapter_image_urls.empty()) {
            last_chapter_url.clear();
            return ImageResult::ERR;
        }
        return ImageResult::OK;
    }

    ImageResult Mangadex::for_each_page_in_chapter(const std::string &chapter_url, PageCallback callback) {
        std::vector<std::string> image_urls;
        {
            std::lock_guard<std::mutex> lock(image_urls_mutex);
            ImageResult image_result = get_image_urls_for_chapter(chapter_url);
            if(image_result != ImageResult::OK)
                return image_result;

            image_urls = last_chapter_image_urls;
        }

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

    bool Mangadex::extract_id_from_url(const std::string &url, std::string &manga_id) {
        manga_id = title_url_extract_manga_id(url);
        return true;
    }
}