Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.

Commit d3fca42

Browse files
committed
fixed multi-page problem
1 parent 3557b86 commit d3fca42

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

include/mal/mal.hpp

+31-27
Original file line numberDiff line numberDiff line change
@@ -164,37 +164,41 @@ namespace mal {
164164
*/
165165
template<typename T>
166166
void search(const std::string_view& name, const short& results, std::function<void(const T&)> callback) {
167-
request([&](bio_callback bio) {
168-
for (short page = 1; page <= (results + 24) / 25; ++page) {
167+
short i_results{};
168+
for (short page = 1; page <= (results + 24) / 25; ++page) {
169+
std::unique_ptr<json> j = std::make_unique<json>();
170+
request([&](bio_callback bio) {
169171
BIO_puts(bio.get(),
170-
webpage(std::format(
171-
"/v4/{0}?q=\"{1}\"&limit={2}&page={3}",
172-
std::string(typeid(T).name()).substr(sizeof("class ") - 1, sizeof(typeid(T).name())), replace(name, ' ', "%20"), ((results < 25) ? results : 25), page)).c_str()
172+
webpage(std::format(
173+
"/v4/{0}?q=\"{1}\"&limit={2}&page={3}",
174+
std::string(typeid(T).name()).substr(sizeof("class ") - 1, sizeof(typeid(T).name())), replace(name, ' ', "%20"), 25, page)).c_str()
173175
);
174-
std::unique_ptr<json> j = std::make_unique<json>();
176+
{
177+
std::unique_ptr<std::string> all_data = std::make_unique<std::string>();
175178
{
176-
std::unique_ptr<std::string> all_data = std::make_unique<std::string>();
177-
{
178-
std::unique_ptr<bool> once = std::make_unique<bool>(false);
179-
std::unique_ptr<char[]> data = std::make_unique<char[]>(INT_MAX);
180-
for (int r = BIO_read(bio.get(), data.get(), INT_MAX); r > 0; r = BIO_read(bio.get(), data.get(), INT_MAX)) {
181-
data[r] = static_cast<char>(0);
182-
all_data->append(data.get());
183-
if (not *once and all_data->find("\r\n\r\n") not_eq -1) {
184-
all_data = std::make_unique<std::string>(all_data->substr(all_data->find("\r\n\r\n") + 4));
185-
once.reset(new bool(true));
186-
}
179+
std::unique_ptr<bool> once = std::make_unique<bool>(false);
180+
std::unique_ptr<char[]> data = std::make_unique<char[]>(INT_MAX);
181+
for (int r = BIO_read(bio.get(), data.get(), INT_MAX); r > 0; r = BIO_read(bio.get(), data.get(), INT_MAX)) {
182+
data[r] = static_cast<char>(0);
183+
all_data->append(data.get());
184+
if (not *once and all_data->find("\r\n\r\n") not_eq -1) {
185+
all_data = std::make_unique<std::string>(all_data->substr(all_data->find("\r\n\r\n") + 4));
186+
once.reset(new bool(true));
187187
}
188-
std::erase_if(*all_data, [](char c) { return not std::isprint(c); });
189-
} // cleanup
190-
all_data = std::make_unique<std::string>(all_data->substr(sizeof "\n\r\n", all_data->size() - sizeof "\n\r\n0"));
191-
if (json::accept(*all_data))
192-
*j = json(json::parse(*all_data));
193-
} // cleanup
194-
if (is_null<bool>((*j)["pagination"]["has_next_page"]) == false) break;
195-
for (const json& data : (*j)["data"])
196-
callback(T(data));
188+
}
189+
std::erase_if(*all_data, [](const char& c) { return not std::isprint(c); });
190+
}
191+
all_data = std::make_unique<std::string>(all_data->substr(sizeof "\n\r\n", all_data->size() - sizeof "\n\r\n0"));
192+
if (json::accept(*all_data))
193+
*j = json(json::parse(*all_data));
197194
}
198-
});
195+
});
196+
if ((*j)["data"].is_null()) break;
197+
for (const json& data : (*j)["data"]) {
198+
i_results++;
199+
if (i_results > results) break;
200+
callback(T(data));
201+
}
202+
}
199203
}
200204
}

0 commit comments

Comments
 (0)