100 lines
3.2 KiB
C++
100 lines
3.2 KiB
C++
#pragma once
|
|
#include <stdexcept>
|
|
#include <mutex>
|
|
#include <vector>
|
|
#include <optional>
|
|
#include "httplib.h"
|
|
#include "app-window.h"
|
|
#include "rei-json/json.h"
|
|
#include "slint_string.h"
|
|
#include "slint.h"
|
|
#include "AppWindow.h"
|
|
#include <cstdlib>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <memory>
|
|
#include <ostream>
|
|
#include <string>
|
|
#include <print>
|
|
class Anilist {
|
|
|
|
public:
|
|
|
|
|
|
static std::optional<rei::json::JsonObject> fetchUserLists(const std::string& userName) {
|
|
httplib::Client cli("https://graphql.anilist.co");
|
|
cli.set_follow_location(true);
|
|
|
|
rei::json::JsonObject body{};
|
|
rei::json::JsonObject variables{};
|
|
const std::string graphqlQuery = "query Query($userName: String $type: MediaType) {\\n MediaListCollection(userName: $userName, type: $type) {\\n lists {\\n name\\n entries {\\n media {\\n id\\n title {\\n romaji\\n }\\n description\\n episodes\\n genres\\n status\\n startDate {\\n year\\n month\\n day\\n }\\n endDate {\\n year\\n month\\n day\\n }\\n coverImage {\\n extraLarge\\n }\\n idMal\\n isAdult\\n siteUrl\\n bannerImage\\n }\\n }\\n }\\n }\\n}";
|
|
|
|
variables.set("type", "ANIME").set("userName", userName);
|
|
body.set("query", graphqlQuery).addObject("variables", variables);
|
|
auto res = cli.Post("/", rei::json::toString(body), "application/json");
|
|
|
|
if (res.error() != httplib::Error::Success) {
|
|
std::cerr << res.error() << std::endl;
|
|
return std::nullopt;
|
|
}
|
|
|
|
if (!res->has_header("Content-Type")) {
|
|
std::cerr << "Missing 'Content-Type'" << std::endl;
|
|
return std::nullopt;
|
|
}
|
|
|
|
const std::string contentType = res->get_header_value("Content-Type");
|
|
if (!contentType.starts_with("application/json")) {
|
|
std::cerr << "Wrong content type" << " (" << contentType << ")" << std::endl;
|
|
return std::nullopt;
|
|
}
|
|
|
|
if (res.error() != httplib::Error::Success) {
|
|
std::cerr << res.error() << std::endl;
|
|
return std::nullopt;
|
|
}
|
|
|
|
if (res->status != 200) {
|
|
std::println("Status: {}\nBody: {}", res->status, res->body);
|
|
return std::nullopt;
|
|
}
|
|
return std::get<rei::json::JsonObject>(rei::json::parse(res->body));
|
|
}
|
|
|
|
static std::optional<std::string> fetchImage(const std::string& path) {
|
|
httplib::Client cli("https://s4.anilist.co");
|
|
cli.set_follow_location(true);
|
|
|
|
auto res = cli.Get(path);
|
|
|
|
if (res.error() != httplib::Error::Success) {
|
|
std::cerr << res.error() << std::endl;
|
|
return std::nullopt;
|
|
}
|
|
|
|
if (!res->has_header("Content-Type")) {
|
|
std::cerr << "Missing 'Content-Type'" << std::endl;
|
|
return std::nullopt;
|
|
}
|
|
|
|
const std::string contentType = res->get_header_value("Content-Type");
|
|
if (!contentType.starts_with("image/jpeg") && !contentType.starts_with("image/png")) {
|
|
std::cerr << "Wrong content type" << " (" << contentType << ")" << std::endl;
|
|
return std::nullopt;
|
|
}
|
|
|
|
if (res.error() != httplib::Error::Success) {
|
|
std::cerr << res.error() << std::endl;
|
|
return std::nullopt;
|
|
}
|
|
|
|
std::cout << res->status << std::endl;
|
|
|
|
return res->body;
|
|
}
|
|
|
|
private:
|
|
|
|
|
|
};
|