aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/Mangadex.cpp
blob: 0b50366883e19c55188dbfb4c6f2dccd62826483 (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
#include "../../plugins/Mangadex.hpp"
#include <json/writer.h>

namespace QuickMedia {
    PluginResult legacy_mangadex_id_to_new_manga_id(Page *page, const std::vector<int> &manga_ids, std::vector<std::pair<int, std::string>> &new_manga_ids) {
        Json::Value request_json(Json::objectValue);
        request_json["type"] = "manga";
        Json::Value manga_ids_json(Json::arrayValue);
        for(int manga_id : manga_ids) {
            manga_ids_json.append(manga_id);
        }
        request_json["ids"] = std::move(manga_ids_json);

        Json::StreamWriterBuilder json_builder;
        json_builder["commentStyle"] = "None";
        json_builder["indentation"] = "";

        std::vector<CommandArg> additional_args = {
            { "-X", "POST" },
            { "-H", "content-type: application/json" },
            { "--data-binary", Json::writeString(json_builder, request_json) }
        };

        Json::Value json_root;
        if(page->download_json(json_root, "https://api.mangadex.org/legacy/mapping", std::move(additional_args), true) != DownloadResult::OK)
            return PluginResult::NET_ERR;

        if(!json_root.isArray())
            return PluginResult::OK;

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

            const Json::Value &result_json = result_item_json["result"];
            if(!result_json.isString() || strcmp(result_json.asCString(), "ok") != 0)
                continue;

            const Json::Value &data_json = result_item_json["data"];
            if(!data_json.isObject())
                continue;

            const Json::Value &attributes_json = data_json["attributes"];
            if(!attributes_json.isObject())
                continue;

            const Json::Value &legacy_id_json = attributes_json["legacyId"];
            if(!legacy_id_json.isInt())
                continue;

            const Json::Value &new_id_json = attributes_json["newId"];
            if(!new_id_json.isString())
                continue;

            new_manga_ids.push_back(std::make_pair<int, std::string>(legacy_id_json.asInt(), new_id_json.asString()));
        }

        return PluginResult::OK;
    }

    class MangadexChapterImagesList : public BodyItemExtra {
    public:
        std::vector<std::string> image_urls;
    };

    SearchResult MangadexSearchPage::search(const std::string &str, int page, BodyItems &result_items) {
        std::string url = "https://api.mangadex.org/manga?title=" + url_param_encode(str) + "&limit=100&offset=" + std::to_string(page * 100);

        Json::Value json_root;
        if(download_json(json_root, url, {}, true) != DownloadResult::OK)
            return SearchResult::NET_ERR;

        if(!json_root.isObject())
            return SearchResult::OK;

        const Json::Value &results_json = json_root["results"];
        if(!results_json.isArray())
            return SearchResult::OK;

        for(const Json::Value &result_item_json : results_json) {
            if(!result_item_json.isObject())
                continue;

            const Json::Value &result_json = result_item_json["result"];
            if(!result_json.isString() || strcmp(result_json.asCString(), "ok") != 0)
                continue;

            const Json::Value &data_json = result_item_json["data"];
            if(!data_json.isObject())
                continue;
            
            const Json::Value &id_json = data_json["id"];
            if(!id_json.isString())
                continue;

            const Json::Value &attributes_json = data_json["attributes"];
            if(!attributes_json.isObject())
                continue;

            const Json::Value &title_json = attributes_json["title"];
            if(!title_json.isObject())
                continue;

            std::string title;
            const Json::Value &title_en_json = title_json["en"];
            if(title_en_json.isString())
                title = title_en_json.asString();
            else
                title = "No title"; // TODO: Verify if this happens. If it happens, get the title in any available language

            auto body_item = BodyItem::create(std::move(title));
            body_item->url = id_json.asString();
            result_items.push_back(std::move(body_item));
        }

        return SearchResult::OK;
    }

    SearchResult MangadexSearchPage::search(const std::string &str, BodyItems &result_items) {
        return search(str, 0, result_items);
    }

    PluginResult MangadexSearchPage::get_page(const std::string &str, int page, BodyItems &result_items) {
        return search_result_to_plugin_result(search(str, page, result_items));
    }

    static PluginResult get_chapters_for_manga(Page *page, const std::string &manga_id, int page_num, BodyItems &result_items) {
        std::string request_url = "https://api.mangadex.org/chapter?manga=" + manga_id + "&limit=100&offset=" + std::to_string(page_num * 100) + "&order[publishAt]=desc";

        Json::Value json_root;
        if(page->download_json(json_root, request_url, {}, true) != DownloadResult::OK)
            return PluginResult::NET_ERR;

        if(!json_root.isObject())
            return PluginResult::OK;

        const Json::Value &results_json = json_root["results"];
        if(!results_json.isArray())
            return PluginResult::OK;

        for(const Json::Value &result_item_json : results_json) {
            if(!result_item_json.isObject())
                continue;

            const Json::Value &result_json = result_item_json["result"];
            if(!result_json.isString() || strcmp(result_json.asCString(), "ok") != 0)
                continue;

            const Json::Value &data_json = result_item_json["data"];
            if(!data_json.isObject())
                continue;

            const Json::Value &id_json = data_json["id"];
            if(!id_json.isString())
                continue;

            const Json::Value &attributes_json = data_json["attributes"];
            if(!attributes_json.isObject())
                continue;

            const Json::Value &translated_language_json = attributes_json["translatedLanguage"];
            if(!translated_language_json.isString() || strcmp(translated_language_json.asCString(), "en") != 0)
                continue;

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

            std::string title = "Ch. " + chapter_json.asString();
            const Json::Value &title_json = attributes_json["title"];
            if(title_json.isString() && title_json.asCString()[0] != '\0')
                title += " - " + title_json.asString();

            const Json::Value &hash_json = attributes_json["hash"];
            if(!hash_json.isString())
                continue;

            const Json::Value &attributes_data_json = attributes_json["data"];
            if(!attributes_data_json.isArray())
                continue;

            auto body_item = BodyItem::create(std::move(title));
            body_item->url = id_json.asString();
            auto images_list = std::make_shared<MangadexChapterImagesList>();
            for(const Json::Value &data_item_json : attributes_data_json) {
                if(!data_item_json.isString())
                    continue;

                std::string url = hash_json.asString() + "/" + data_item_json.asString();
                images_list->image_urls.push_back(std::move(url));
            }
            body_item->extra = std::move(images_list);
            result_items.push_back(std::move(body_item));
        }

        return PluginResult::OK;
    }

    PluginResult MangadexSearchPage::submit(const std::string &title, const std::string &url, std::vector<Tab> &result_tabs) {
        auto body = create_body();
        get_chapters_for_manga(this, url, 0, body->items);
        result_tabs.push_back(Tab{std::move(body), std::make_unique<MangadexChaptersPage>(program, title, url), create_search_bar("Search...", SEARCH_DELAY_FILTER)});
        return PluginResult::OK;
    }

    PluginResult MangadexChaptersPage::submit(const std::string &title, const std::string &url, std::vector<Tab> &result_tabs) {
        Json::Value json_root;
        if(download_json(json_root, "https://api.mangadex.org/at-home/server/" + url, {}, true) != DownloadResult::OK)
            return PluginResult::NET_ERR;

        if(!json_root.isObject())
            return PluginResult::ERR;

        const Json::Value &base_url_json = json_root["baseUrl"];
        if(!base_url_json.isString())
            return PluginResult::ERR;

        std::string base_url = base_url_json.asString();
        if(!base_url.empty() && base_url.back() != '/')
            base_url += '/';

        auto image_urls = static_cast<MangadexChapterImagesList*>(submit_body_item->extra.get())->image_urls;
        for(auto &image_url : image_urls) {
            image_url = base_url + "data/" + image_url;
        }

        result_tabs.push_back(Tab{nullptr, std::make_unique<MangadexImagesPage>(program, content_title, title, content_url, image_urls), nullptr});
        return PluginResult::OK;
    }

    PluginResult MangadexChaptersPage::get_page(const std::string&, int page, BodyItems &result_items) {
        return get_chapters_for_manga(this, content_url, page, result_items);
    }

    bool MangadexChaptersPage::extract_id_from_url(const std::string &url, std::string &manga_id) const {
        size_t start_index = url.find("manga=");
        if(start_index == std::string::npos) {
            manga_id = url;
            return true;
        }

        start_index += 6;
        size_t end_index = url.find('&', start_index);
        if(end_index == std::string::npos) {
            manga_id = url.substr(start_index);
            return true;
        }
        
        manga_id = url.substr(start_index, end_index - start_index);
        return true;
    }

    ImageResult MangadexImagesPage::get_number_of_images(int &num_images) {
        num_images = image_urls.size();
        return ImageResult::OK;
    }

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