Mercurial > minori
annotate src/services/anilist.cc @ 304:2115488eb302
*: add very early season searcher
| author | Paper <paper@paper.us.eu.org> |
|---|---|
| date | Mon, 13 May 2024 17:02:35 -0400 |
| parents | 9a88e1725fd2 |
| children | 91ac90a34003 |
| rev | line source |
|---|---|
| 9 | 1 #include "services/anilist.h" |
| 2 #include "core/anime.h" | |
| 10 | 3 #include "core/anime_db.h" |
|
304
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
4 #include "core/date.h" |
| 9 | 5 #include "core/config.h" |
| 76 | 6 #include "core/http.h" |
| 9 | 7 #include "core/json.h" |
| 8 #include "core/session.h" | |
| 9 #include "core/strings.h" | |
| 15 | 10 #include "gui/translate/anilist.h" |
|
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
185
diff
changeset
|
11 |
| 258 | 12 #include <QByteArray> |
|
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
185
diff
changeset
|
13 #include <QDate> |
| 9 | 14 #include <QDesktopServices> |
| 15 #include <QInputDialog> | |
| 16 #include <QLineEdit> | |
| 17 #include <QMessageBox> | |
| 10 | 18 #include <QUrl> |
|
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
185
diff
changeset
|
19 |
| 9 | 20 #include <chrono> |
| 21 #include <exception> | |
| 291 | 22 #include <string_view> |
| 175 | 23 |
| 24 #include <iostream> | |
| 25 | |
| 64 | 26 using namespace nlohmann::literals::json_literals; |
| 11 | 27 |
| 63 | 28 namespace Services { |
| 29 namespace AniList { | |
| 9 | 30 |
| 291 | 31 static constexpr std::string_view CLIENT_ID = "13706"; |
|
304
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
32 #define MEDIA_FIELDS \ |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
33 "coverImage {\n" \ |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
34 " large\n" \ |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
35 "}\n" \ |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
36 "id\n" \ |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
37 "title {\n" \ |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
38 " romaji\n" \ |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
39 " english\n" \ |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
40 " native\n" \ |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
41 "}\n" \ |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
42 "format\n" \ |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
43 "status\n" \ |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
44 "averageScore\n" \ |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
45 "season\n" \ |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
46 "startDate {\n" \ |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
47 " year\n" \ |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
48 " month\n" \ |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
49 " day\n" \ |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
50 "}\n" \ |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
51 "genres\n" \ |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
52 "episodes\n" \ |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
53 "duration\n" \ |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
54 "synonyms\n" \ |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
55 "description(asHtml: false)\n" |
|
185
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
184
diff
changeset
|
56 |
| 9 | 57 class Account { |
| 258 | 58 public: |
| 59 int UserId() const { return session.config.auth.anilist.user_id; } | |
| 60 void SetUserId(const int id) { session.config.auth.anilist.user_id = id; } | |
| 9 | 61 |
| 258 | 62 std::string AuthToken() const { return session.config.auth.anilist.auth_token; } |
|
286
53e3c015a973
anime: initial cross-service support
Paper <paper@paper.us.eu.org>
parents:
284
diff
changeset
|
63 void SetAuthToken(const std::string& auth_token) { session.config.auth.anilist.auth_token = auth_token; } |
| 9 | 64 |
| 258 | 65 bool Authenticated() const { return !AuthToken().empty(); } |
| 66 bool IsValid() const { return UserId() && Authenticated(); } | |
| 10 | 67 }; |
| 9 | 68 |
| 69 static Account account; | |
| 70 | |
| 291 | 71 static std::string SendRequest(const std::string& data) { |
| 76 | 72 std::vector<std::string> headers = {"Authorization: Bearer " + account.AuthToken(), "Accept: application/json", |
| 258 | 73 "Content-Type: application/json"}; |
| 291 | 74 return Strings::ToUtf8String(HTTP::Request("https://graphql.anilist.co", headers, data, HTTP::Type::Post)); |
| 9 | 75 } |
| 76 | |
| 291 | 77 static nlohmann::json SendJSONRequest(const nlohmann::json& data) { |
| 175 | 78 std::string request = SendRequest(data.dump()); |
| 79 if (request.empty()) { | |
| 80 std::cerr << "[AniList] JSON Request returned an empty result!" << std::endl; | |
| 81 return {}; | |
| 82 } | |
| 83 | |
| 84 auto ret = nlohmann::json::parse(request, nullptr, false); | |
| 85 if (ret.is_discarded()) { | |
| 86 std::cerr << "[AniList] Failed to parse request JSON!" << std::endl; | |
| 87 return {}; | |
| 88 } | |
| 89 | |
| 90 if (ret.contains("/errors"_json_pointer) && ret.at("/errors"_json_pointer).is_array()) { | |
| 91 for (const auto& error : ret.at("/errors"_json_pointer)) | |
| 258 | 92 std::cerr << "[AniList] Received an error in response: " |
| 93 << JSON::GetString<std::string>(error, "/message"_json_pointer, "") << std::endl; | |
| 175 | 94 |
| 95 return {}; | |
| 96 } | |
| 97 | |
| 98 return ret; | |
| 99 } | |
| 100 | |
| 291 | 101 static void ParseListStatus(std::string status, Anime::Anime& anime) { |
|
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
185
diff
changeset
|
102 static const std::unordered_map<std::string, Anime::ListStatus> map = { |
| 279 | 103 {"CURRENT", Anime::ListStatus::Current }, |
| 104 {"PLANNING", Anime::ListStatus::Planning }, | |
| 105 {"COMPLETED", Anime::ListStatus::Completed}, | |
| 106 {"DROPPED", Anime::ListStatus::Dropped }, | |
| 107 {"PAUSED", Anime::ListStatus::Paused } | |
| 258 | 108 }; |
| 9 | 109 |
| 15 | 110 if (status == "REPEATING") { |
| 111 anime.SetUserIsRewatching(true); | |
| 279 | 112 anime.SetUserStatus(Anime::ListStatus::Current); |
| 15 | 113 return; |
| 114 } | |
| 9 | 115 |
|
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
116 if (map.find(status) == map.end()) { |
| 279 | 117 anime.SetUserStatus(Anime::ListStatus::NotInList); |
| 15 | 118 return; |
| 119 } | |
| 9 | 120 |
|
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
185
diff
changeset
|
121 anime.SetUserStatus(map.at(status)); |
| 15 | 122 } |
| 9 | 123 |
| 291 | 124 static std::string ListStatusToString(const Anime::Anime& anime) { |
| 279 | 125 if (anime.GetUserIsRewatching() && anime.GetUserStatus() == Anime::ListStatus::Current) |
| 15 | 126 return "REWATCHING"; |
| 127 | |
|
70
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
128 switch (anime.GetUserStatus()) { |
| 279 | 129 case Anime::ListStatus::Planning: return "PLANNING"; |
| 130 case Anime::ListStatus::Completed: return "COMPLETED"; | |
| 131 case Anime::ListStatus::Dropped: return "DROPPED"; | |
| 132 case Anime::ListStatus::Paused: return "PAUSED"; | |
| 76 | 133 default: break; |
|
70
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
134 } |
|
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
135 return "CURRENT"; |
| 15 | 136 } |
| 9 | 137 |
| 291 | 138 static void ParseTitle(const nlohmann::json& json, Anime::Anime& anime) { |
|
284
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
139 nlohmann::json::json_pointer g = "/native"_json_pointer; |
|
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
140 if (json.contains(g) && json[g].is_string()) |
|
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
141 anime.SetTitle(Anime::TitleLanguage::Native, json[g]); |
|
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
142 |
|
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
143 g = "/english"_json_pointer; |
|
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
144 if (json.contains(g) && json[g].is_string()) |
|
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
145 anime.SetTitle(Anime::TitleLanguage::English, json[g]); |
|
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
146 |
|
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
147 g = "/romaji"_json_pointer; |
|
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
148 if (json.contains(g) && json[g].is_string()) |
|
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
149 anime.SetTitle(Anime::TitleLanguage::Romaji, json[g]); |
| 9 | 150 } |
| 151 | |
| 291 | 152 static int ParseMediaJson(const nlohmann::json& json) { |
| 175 | 153 int id = JSON::GetNumber(json, "/id"_json_pointer); |
| 9 | 154 if (!id) |
| 155 return 0; | |
| 175 | 156 |
| 9 | 157 Anime::Anime& anime = Anime::db.items[id]; |
| 158 anime.SetId(id); | |
|
286
53e3c015a973
anime: initial cross-service support
Paper <paper@paper.us.eu.org>
parents:
284
diff
changeset
|
159 anime.SetServiceId(Anime::Service::AniList, Strings::ToUtf8String(id)); |
|
53e3c015a973
anime: initial cross-service support
Paper <paper@paper.us.eu.org>
parents:
284
diff
changeset
|
160 anime.SetServiceId(Anime::Service::MyAnimeList, Strings::ToUtf8String(JSON::GetNumber(json, "/id_mal"_json_pointer))); |
| 9 | 161 |
| 11 | 162 ParseTitle(json.at("/title"_json_pointer), anime); |
| 9 | 163 |
| 175 | 164 anime.SetEpisodes(JSON::GetNumber(json, "/episodes"_json_pointer, 0)); |
| 165 anime.SetFormat(Translate::AniList::ToSeriesFormat(JSON::GetString<std::string>(json, "/format"_json_pointer, ""))); | |
| 9 | 166 |
| 258 | 167 anime.SetAiringStatus( |
| 168 Translate::AniList::ToSeriesStatus(JSON::GetString<std::string>(json, "/status"_json_pointer, ""))); | |
| 9 | 169 |
| 175 | 170 anime.SetAirDate(Date(json["/startDate"_json_pointer])); |
| 9 | 171 |
| 175 | 172 anime.SetPosterUrl(JSON::GetString<std::string>(json, "/coverImage/large"_json_pointer, "")); |
|
66
6481c5aed3e1
posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
65
diff
changeset
|
173 |
| 175 | 174 anime.SetAudienceScore(JSON::GetNumber(json, "/averageScore"_json_pointer, 0)); |
| 279 | 175 // anime.SetSeason(Translate::AniList::ToSeriesSeason(JSON::GetString<std::string>(json, "/season"_json_pointer, ""))); |
| 175 | 176 anime.SetDuration(JSON::GetNumber(json, "/duration"_json_pointer, 0)); |
|
260
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
177 |
|
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
178 std::string synopsis = JSON::GetString<std::string>(json, "/description"_json_pointer, ""); |
|
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
179 Strings::TextifySynopsis(synopsis); |
|
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
180 anime.SetSynopsis(synopsis); |
| 9 | 181 |
| 175 | 182 anime.SetGenres(JSON::GetArray<std::vector<std::string>>(json, "/genres"_json_pointer, {})); |
| 183 anime.SetTitleSynonyms(JSON::GetArray<std::vector<std::string>>(json, "/synonyms"_json_pointer, {})); | |
| 184 | |
| 10 | 185 return id; |
| 9 | 186 } |
| 187 | |
| 291 | 188 static int ParseListItem(const nlohmann::json& json) { |
| 10 | 189 int id = ParseMediaJson(json["media"]); |
| 190 | |
| 191 Anime::Anime& anime = Anime::db.items[id]; | |
| 192 | |
| 193 anime.AddToUserList(); | |
| 9 | 194 |
| 175 | 195 anime.SetUserScore(JSON::GetNumber(json, "/score"_json_pointer, 0)); |
| 196 anime.SetUserProgress(JSON::GetNumber(json, "/progress"_json_pointer, 0)); | |
| 197 ParseListStatus(JSON::GetString<std::string>(json, "/status"_json_pointer, ""), anime); | |
| 198 anime.SetUserNotes(JSON::GetString<std::string>(json, "/notes"_json_pointer, "")); | |
| 9 | 199 |
| 175 | 200 anime.SetUserDateStarted(Date(json["/startedAt"_json_pointer])); |
| 201 anime.SetUserDateCompleted(Date(json["/completedAt"_json_pointer])); | |
| 9 | 202 |
| 175 | 203 anime.SetUserTimeUpdated(JSON::GetNumber(json, "/updatedAt"_json_pointer, 0)); |
| 10 | 204 |
| 205 return id; | |
| 9 | 206 } |
| 207 | |
| 291 | 208 static int ParseList(const nlohmann::json& json) { |
| 9 | 209 for (const auto& entry : json["entries"].items()) { |
| 210 ParseListItem(entry.value()); | |
| 211 } | |
| 10 | 212 return 1; |
| 9 | 213 } |
| 214 | |
| 10 | 215 int GetAnimeList() { |
| 175 | 216 if (!account.IsValid()) { |
| 217 std::cerr << "AniList: Account isn't valid!" << std::endl; | |
| 218 return 0; | |
| 219 } | |
| 220 | |
|
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
221 /* NOTE: these really ought to be in the qrc file */ |
|
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
222 constexpr std::string_view query = "query ($id: Int) {\n" |
| 258 | 223 " MediaListCollection (userId: $id, type: ANIME) {\n" |
| 224 " lists {\n" | |
| 225 " name\n" | |
| 226 " entries {\n" | |
| 227 " score\n" | |
| 228 " notes\n" | |
| 229 " status\n" | |
| 230 " progress\n" | |
| 231 " startedAt {\n" | |
| 232 " year\n" | |
| 233 " month\n" | |
| 234 " day\n" | |
| 235 " }\n" | |
| 236 " completedAt {\n" | |
| 237 " year\n" | |
| 238 " month\n" | |
| 239 " day\n" | |
| 240 " }\n" | |
| 241 " updatedAt\n" | |
| 242 " media {\n" | |
|
304
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
243 MEDIA_FIELDS |
| 258 | 244 " }\n" |
| 245 " }\n" | |
| 246 " }\n" | |
| 247 " }\n" | |
| 248 "}\n"; | |
| 9 | 249 // clang-format off |
| 250 nlohmann::json json = { | |
| 251 {"query", query}, | |
| 252 {"variables", { | |
| 10 | 253 {"id", account.UserId()} |
| 9 | 254 }} |
| 255 }; | |
| 256 // clang-format on | |
| 175 | 257 |
| 258 auto res = SendJSONRequest(json); | |
| 259 | |
|
304
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
260 for (const auto& list : res["data"]["MediaListCollection"]["lists"].items()) |
| 10 | 261 ParseList(list.value()); |
|
304
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
262 |
| 9 | 263 return 1; |
| 264 } | |
| 265 | |
| 250 | 266 /* return is a vector of anime ids */ |
| 267 std::vector<int> Search(const std::string& search) { | |
| 258 | 268 constexpr std::string_view query = "query ($search: String) {\n" |
| 269 " Page (page: 1, perPage: 50) {\n" | |
| 270 " media (search: $search, type: ANIME) {\n" | |
|
304
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
271 MEDIA_FIELDS |
| 258 | 272 " }\n" |
| 273 " }\n" | |
| 274 "}\n"; | |
| 250 | 275 |
| 276 // clang-format off | |
| 277 nlohmann::json json = { | |
| 278 {"query", query}, | |
| 279 {"variables", { | |
| 280 {"search", search} | |
| 281 }} | |
| 282 }; | |
| 283 // clang-format on | |
| 284 | |
| 285 auto res = SendJSONRequest(json); | |
| 286 | |
|
304
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
287 /* FIXME: error handling here */ |
| 250 | 288 std::vector<int> ret; |
| 289 ret.reserve(res["data"]["Page"]["media"].size()); | |
| 290 | |
| 291 for (const auto& media : res["data"]["Page"]["media"].items()) | |
| 292 ret.push_back(ParseMediaJson(media.value())); | |
| 293 | |
| 294 return ret; | |
| 295 } | |
| 296 | |
|
304
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
297 std::vector<int> GetSeason(Anime::SeriesSeason season, Date::Year year) { |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
298 constexpr std::string_view query = "query ($season: MediaSeason!, $season_year: Int!, $page: Int) {\n" |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
299 " Page(page: $page) {\n" |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
300 " media(season: $season, seasonYear: $season_year, type: ANIME, sort: START_DATE) {\n" |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
301 MEDIA_FIELDS |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
302 " }\n" |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
303 " pageInfo {\n" |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
304 " total\n" |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
305 " perPage\n" |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
306 " currentPage\n" |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
307 " lastPage\n" |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
308 " hasNextPage\n" |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
309 " }\n" |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
310 " }\n" |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
311 "}\n"; |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
312 std::vector<int> ret; |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
313 |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
314 int page = 0; |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
315 bool has_next_page = true; |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
316 while (has_next_page) { |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
317 nlohmann::json json = { |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
318 {"query", query}, |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
319 {"variables", { |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
320 {"season", Translate::AniList::ToString(season)}, |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
321 {"season_year", Strings::ToUtf8String(year)}, |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
322 {"page", page} |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
323 }} |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
324 }; |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
325 |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
326 auto res = SendJSONRequest(json); |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
327 ret.reserve(ret.capacity() + res["data"]["Page"]["media"].size()); |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
328 |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
329 for (const auto& media : res["data"]["Page"]["media"].items()) |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
330 ret.push_back(ParseMediaJson(media.value())); |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
331 |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
332 has_next_page = JSON::GetBoolean(res, "/data/Page/pageInfo/hasNextPage"_json_pointer, false); |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
333 if (has_next_page) |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
334 page++; |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
335 } |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
336 |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
337 return ret; |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
338 } |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
339 |
|
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
340 int UpdateAnimeEntry(int id) { |
| 9 | 341 /** |
| 342 * possible values: | |
| 15 | 343 * |
| 9 | 344 * int mediaId, |
| 345 * MediaListStatus status, | |
| 346 * float score, | |
| 347 * int scoreRaw, | |
| 348 * int progress, | |
|
184
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
349 * int progressVolumes, // manga-specific. |
|
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
350 * int repeat, // rewatch |
| 258 | 351 * int priority, |
| 9 | 352 * bool private, |
| 353 * string notes, | |
| 354 * bool hiddenFromStatusLists, | |
| 355 * string[] customLists, | |
| 356 * float[] advancedScores, | |
| 357 * Date startedAt, | |
| 358 * Date completedAt | |
| 258 | 359 **/ |
|
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
360 Anime::Anime& anime = Anime::db.items[id]; |
|
184
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
361 if (!anime.IsInUserList()) |
|
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
362 return 0; |
|
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
363 |
| 291 | 364 std::optional<std::string> service_id = anime.GetServiceId(Anime::Service::AniList); |
| 365 if (!service_id) | |
| 366 return 0; | |
| 367 | |
| 250 | 368 constexpr std::string_view query = |
| 258 | 369 "mutation ($media_id: Int, $progress: Int, $status: MediaListStatus, $score: Int, $notes: String, $start: " |
| 370 "FuzzyDateInput, $comp: FuzzyDateInput, $repeat: Int) {\n" | |
| 371 " SaveMediaListEntry (mediaId: $media_id, progress: $progress, status: $status, scoreRaw: $score, notes: " | |
| 372 "$notes, startedAt: $start, completedAt: $comp, repeat: $repeat) {\n" | |
| 373 " id\n" | |
| 374 " }\n" | |
| 375 "}\n"; | |
| 9 | 376 // clang-format off |
| 377 nlohmann::json json = { | |
| 378 {"query", query}, | |
| 379 {"variables", { | |
| 291 | 380 {"media_id", Strings::ToInt<int64_t>(service_id.value())}, |
| 10 | 381 {"progress", anime.GetUserProgress()}, |
| 15 | 382 {"status", ListStatusToString(anime)}, |
| 10 | 383 {"score", anime.GetUserScore()}, |
| 77 | 384 {"notes", anime.GetUserNotes()}, |
| 385 {"start", anime.GetUserDateStarted().GetAsAniListJson()}, | |
|
184
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
386 {"comp", anime.GetUserDateCompleted().GetAsAniListJson()}, |
|
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
387 {"repeat", anime.GetUserRewatchedTimes()} |
| 9 | 388 }} |
| 389 }; | |
| 390 // clang-format on | |
| 175 | 391 |
| 392 auto ret = SendJSONRequest(json); | |
| 393 | |
| 394 return JSON::GetNumber(ret, "/data/SaveMediaListEntry/id"_json_pointer, 0); | |
| 9 | 395 } |
| 396 | |
| 291 | 397 static int ParseUser(const nlohmann::json& json) { |
| 175 | 398 account.SetUserId(JSON::GetNumber(json, "/id"_json_pointer, 0)); |
| 10 | 399 return account.UserId(); |
| 9 | 400 } |
| 401 | |
|
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
402 bool AuthorizeUser() { |
| 9 | 403 /* Prompt for PIN */ |
| 258 | 404 QDesktopServices::openUrl(QUrl(Strings::ToQString("https://anilist.co/api/v2/oauth/authorize?client_id=" + |
| 291 | 405 std::string(CLIENT_ID) + "&response_type=token"))); |
| 175 | 406 |
| 9 | 407 bool ok; |
| 408 QString token = QInputDialog::getText( | |
| 258 | 409 0, "Credentials needed!", "Please enter the code given to you after logging in to AniList:", QLineEdit::Normal, |
| 410 "", &ok); | |
| 175 | 411 |
| 412 if (!ok || token.isEmpty()) | |
|
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
413 return false; |
| 175 | 414 |
| 415 account.SetAuthToken(Strings::ToUtf8String(token)); | |
| 416 | |
|
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
417 constexpr std::string_view query = "query {\n" |
| 258 | 418 " Viewer {\n" |
| 419 " id\n" | |
| 420 " name\n" | |
| 421 " mediaListOptions {\n" | |
| 422 " scoreFormat\n" // this will be used... eventually | |
| 423 " }\n" | |
| 424 " }\n" | |
| 425 "}\n"; | |
| 9 | 426 nlohmann::json json = { |
| 258 | 427 {"query", query} |
| 428 }; | |
| 175 | 429 |
| 430 auto ret = SendJSONRequest(json); | |
| 431 | |
| 74 | 432 ParseUser(ret["data"]["Viewer"]); |
|
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
433 return true; |
| 9 | 434 } |
| 435 | |
| 63 | 436 } // namespace AniList |
| 437 } // namespace Services |
