aboutsummaryrefslogtreecommitdiff
path: root/plugins/MangaGeneric.hpp
blob: a03756d1c51b77374997f9498d6861254cfb03af (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
#pragma once

#include "Manga.hpp"
#include <functional>

namespace QuickMedia {
    struct SearchQuery {
        const char *search_template = nullptr;
        int page_start = 0;
    };

    // If |url_contains| is null, then any matching query is added. If |title_field| is "text", then the inner text is used.
    // If |url_field| is null, then the current page is used instead.
    struct TextQuery {
        const char *html_query = nullptr;
        const char *title_field = nullptr;
        const char *url_field = nullptr;
        const char *url_contains = nullptr;
    };

    struct DescriptionQuery {
        const char *html_query = nullptr;
        const char *field_name = nullptr;
    };

    struct ThumbnailQuery {
        const char *html_query = nullptr;
        const char *field_name = nullptr;
        const char *field_contains = nullptr;
    };

    struct AuthorsQuery {
        const char *html_query = nullptr;
        const char *title_field = nullptr;
        const char *url_field = nullptr;
        const char *url_contains = nullptr;
    };

    struct ListChaptersQuery {
        const char *html_query = nullptr;
        const char *title_field = nullptr;
        const char *url_field = nullptr;
        const char *url_contains = nullptr;

        const char *uploaded_time_html_query = nullptr;
        const char *uploaded_time_field_name = nullptr;
        const char *uploaded_time_field_contains = nullptr;
    };

    using ListPageImagesQueryPost = std::function<void(std::vector<std::string> &image_urls)>;
    struct ListPageImagesQuery {
        const char *html_query = nullptr;
        const char *field_name = nullptr;
        const char *field_contains = nullptr;
        ListPageImagesQueryPost post_handler = nullptr;
    };

    struct ListPagePaginationQuery {
        const char *pages_html_query = nullptr;
        const char *pages_field_name = nullptr;

        const char *image_html_query = nullptr;
        const char *image_field_name = nullptr;
        const char *image_field_contains = nullptr;

        const char *next_page_html_query = nullptr;
        const char *next_page_field_name = nullptr;
        const char *next_page_field_contains = nullptr;
    };

    // Return the image sources
    using ListPageCustomHandler = std::function<std::vector<std::string>(const std::string &html_source)>;
    struct ListPageCustomQuery {
        ListPageCustomHandler handler;
    };

    enum class ListPageQueryType {
        IMAGES,
        PAGINATION,
        CUSTOM
    };

    struct ListPageQuery {
        ListPageQueryType type = ListPageQueryType::IMAGES;
        ListPageImagesQuery images_query;
        ListPagePaginationQuery pagination_query;
        ListPageCustomQuery custom_query;
    };

    struct MangaIdExtractor {
        const char *prefix = nullptr;
        const char *end = nullptr;
    };

    class MangaGenericSearchPage : public Page {
    public:
        MangaGenericSearchPage(Program *program, const char *service_name, const char *website_url, bool fail_on_http_error = true);
        const char* get_title() const override { return "All"; }
        bool search_is_filter() override { return false; }
        SearchResult search(const std::string &str, BodyItems &result_items) override;
        PluginResult get_page(const std::string &url, BodyItems &result_items);
        PluginResult get_page(const std::string &str, int page, BodyItems &result_items) override;
        PluginResult submit(const std::string &title, const std::string &url, std::vector<Tab> &result_tabs) override;
        sf::Vector2i get_thumbnail_max_size() override { return sf::Vector2i(101, 141); };

        // Add a %s where the search query should be inserted into |search_template| and add a %p where the page number should be inserted, for example:
        // example.com/search?q=%s&page=%p
        // This is required.
        MangaGenericSearchPage& search_handler(const char *search_template, int page_start);
        // This is required.
        MangaGenericSearchPage& text_handler(std::vector<TextQuery> queries);
        // This is optional.
        MangaGenericSearchPage& description_handler(std::vector<DescriptionQuery> queries);
        // This is optional.
        MangaGenericSearchPage& thumbnail_handler(std::vector<ThumbnailQuery> queries);
        // If |url_contains| is null, then any matching query is added. If |title_field| is "text", then the inner text is used.
        // This is optional.
        MangaGenericSearchPage& authors_handler(const char *html_query, const char *title_field, const char *url_field, const char *url_contains);

        // If |url_contains| is null, then any matching query is added. If |title_field| is "text", then the inner text is used.
        // This is required.
        MangaGenericSearchPage& list_chapters_handler(const char *html_query, const char *title_field, const char *url_field, const char *url_contains);
        // If |field_contains| is null, then any matching query is added. If |field_name| is "text", then the inner text is used.
        // This is optional.
        MangaGenericSearchPage& list_chapters_uploaded_time_handler(const char *html_query, const char *field_name, const char *field_contains);

        // If |field_contains| is null, then any matching query is added. If |field_name| is "text", then the inner text is used.
        // This or |list_page_images_pagination_handler| or |list_page_images_custom_handler| is required.
        MangaGenericSearchPage& list_page_images_handler(const char *html_query, const char *field_name, const char *field_contains, ListPageImagesQueryPost post_handler = nullptr);

        // If |image_field_contains| is null, then any matching query is added. If |pages_field_name| or |image_field_name| is "text", then the inner text is used.
        // The last matching pages html query item is chosen as the number of pages.
        // This or |list_page_images_handler| or |list_page_images_custom_handler| is required.
        MangaGenericSearchPage& list_page_images_pagination_handler(
            const char *pages_html_query, const char *pages_field_name,
            const char *image_html_query, const char *image_field_name, const char *image_field_contains,
            const char *next_page_html_query, const char *next_page_field_name, const char *next_page_field_contains);

        // This or |list_page_images_handler| or |list_page_images_pagination_handler| is required.
        MangaGenericSearchPage& list_page_images_custom_handler(ListPageCustomHandler handler);

        // For example: mangasite.com/manga/204353&f=23
        // /manga/ here would be the |prefix| and & would be |end|. |end| is optional.
        // The goal is to extract 204353 from the manga chapter page url.
        MangaGenericSearchPage& manga_id_handler(const char *prefix, const char *end);
    private:
        const char *service_name;
        std::string website_url;
        bool fail_on_http_error;
        SearchQuery search_query;
        std::vector<TextQuery> text_queries;
        std::vector<DescriptionQuery> description_queries;
        std::vector<ThumbnailQuery> thumbnail_queries;
        AuthorsQuery authors_query;
        ListChaptersQuery list_chapters_query;
        ListPageQuery list_page_query;
        MangaIdExtractor manga_id_extractor;
    };

    class MangaGenericChaptersPage : public MangaChaptersPage {
    public:
        MangaGenericChaptersPage(Program *program, std::string manga_name, std::string manga_url, const MangaIdExtractor &manga_id_extractor, const char *service_name, const std::string &website_url, const ListPageQuery *list_page_query, bool fail_on_http_error) :
            MangaChaptersPage(program, std::move(manga_name), std::move(manga_url)), manga_id_extractor(manga_id_extractor), service_name(service_name), website_url(website_url), list_page_query(list_page_query), fail_on_http_error(fail_on_http_error) {}
        PluginResult submit(const std::string &title, const std::string &url, std::vector<Tab> &result_tabs) override;
    protected:
        bool extract_id_from_url(const std::string &url, std::string &manga_id) const override;
        const char* get_service_name() const override { return service_name; }
    private:
        MangaIdExtractor manga_id_extractor;
        const char *service_name;
        std::string website_url;
        const ListPageQuery *list_page_query;
        bool fail_on_http_error;
    };

    class MangaGenericCreatorPage : public LazyFetchPage {
    public:
        MangaGenericCreatorPage(Program *program, MangaGenericSearchPage *search_page, Creator creator) : LazyFetchPage(program), search_page(search_page), creator(std::move(creator)) {}
        const char* get_title() const override { return creator.name.c_str(); }
        PluginResult submit(const std::string &title, const std::string &url, std::vector<Tab> &result_tabs) override;
        PluginResult lazy_fetch(BodyItems &result_items) override;
        sf::Vector2i get_thumbnail_max_size() override { return sf::Vector2i(101, 141); };
    private:
        MangaGenericSearchPage *search_page;
        Creator creator;
    };

    class MangaGenericImagesPage : public MangaImagesPage {
    public:
        MangaGenericImagesPage(Program *program, std::string manga_name, std::string chapter_name, std::string url, const char *service_name, const std::string &website_url, const ListPageQuery *list_page_query, bool fail_on_http_error) :
            MangaImagesPage(program, std::move(manga_name), std::move(chapter_name), std::move(url)), service_name(service_name), website_url(website_url), list_page_query(list_page_query), fail_on_http_error(fail_on_http_error) {}
        ImageResult get_number_of_images(int &num_images) override;
        ImageResult for_each_page_in_chapter(PageCallback callback) override;
        const char* get_service_name() const override { return service_name; }
    private:
        ImageResult get_page_image_urls();
    private:
        const char *service_name;
        std::string website_url;
        const ListPageQuery *list_page_query;
        bool fail_on_http_error;
        std::string current_image_url;
        std::string next_page_url;
    };
}