Mercurial > minori
annotate src/services/anilist.cc @ 316:180714442770
settings: clean up code
| author | Paper <paper@paper.us.eu.org> |
|---|---|
| date | Tue, 11 Jun 2024 15:11:09 -0400 |
| parents | 34347fd2a2de |
| children | b1f4d1867ab1 |
| 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 | |
|
315
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
77 static bool SendJSONRequest(const nlohmann::json& data, nlohmann::json& out) { |
| 175 | 78 std::string request = SendRequest(data.dump()); |
| 79 if (request.empty()) { | |
|
315
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
80 session.SetStatusBar("AniList: JSON request returned an empty result!"); |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
81 return false; |
| 175 | 82 } |
| 83 | |
|
315
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
84 out = nlohmann::json::parse(request, nullptr, false); |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
85 if (out.is_discarded()) { |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
86 session.SetStatusBar("AniList: Failed to parse request JSON!"); |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
87 return false; |
| 175 | 88 } |
| 89 | |
|
315
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
90 if (out.contains("/errors"_json_pointer) && out.at("/errors"_json_pointer).is_array()) { |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
91 for (const auto& error : out.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 |
|
315
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
95 session.SetStatusBar("AniList: Received an error in response!"); |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
96 return false; |
| 175 | 97 } |
| 98 | |
|
315
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
99 return true; |
| 175 | 100 } |
| 101 | |
| 291 | 102 static void ParseListStatus(std::string status, Anime::Anime& anime) { |
|
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
185
diff
changeset
|
103 static const std::unordered_map<std::string, Anime::ListStatus> map = { |
| 279 | 104 {"CURRENT", Anime::ListStatus::Current }, |
| 105 {"PLANNING", Anime::ListStatus::Planning }, | |
| 106 {"COMPLETED", Anime::ListStatus::Completed}, | |
| 107 {"DROPPED", Anime::ListStatus::Dropped }, | |
| 108 {"PAUSED", Anime::ListStatus::Paused } | |
| 258 | 109 }; |
| 9 | 110 |
| 15 | 111 if (status == "REPEATING") { |
| 112 anime.SetUserIsRewatching(true); | |
| 279 | 113 anime.SetUserStatus(Anime::ListStatus::Current); |
| 15 | 114 return; |
| 115 } | |
| 9 | 116 |
|
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
117 if (map.find(status) == map.end()) { |
| 279 | 118 anime.SetUserStatus(Anime::ListStatus::NotInList); |
| 15 | 119 return; |
| 120 } | |
| 9 | 121 |
|
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
185
diff
changeset
|
122 anime.SetUserStatus(map.at(status)); |
| 15 | 123 } |
| 9 | 124 |
| 291 | 125 static std::string ListStatusToString(const Anime::Anime& anime) { |
| 279 | 126 if (anime.GetUserIsRewatching() && anime.GetUserStatus() == Anime::ListStatus::Current) |
| 15 | 127 return "REWATCHING"; |
| 128 | |
|
70
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
129 switch (anime.GetUserStatus()) { |
| 279 | 130 case Anime::ListStatus::Planning: return "PLANNING"; |
| 131 case Anime::ListStatus::Completed: return "COMPLETED"; | |
| 132 case Anime::ListStatus::Dropped: return "DROPPED"; | |
| 133 case Anime::ListStatus::Paused: return "PAUSED"; | |
| 76 | 134 default: break; |
|
70
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
135 } |
|
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
136 return "CURRENT"; |
| 15 | 137 } |
| 9 | 138 |
| 291 | 139 static void ParseTitle(const nlohmann::json& json, Anime::Anime& anime) { |
|
305
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
304
diff
changeset
|
140 static const std::unordered_map<Anime::TitleLanguage, nlohmann::json::json_pointer> map = { |
|
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
304
diff
changeset
|
141 {Anime::TitleLanguage::Native, "/native"_json_pointer}, |
|
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
304
diff
changeset
|
142 {Anime::TitleLanguage::English, "/english"_json_pointer}, |
|
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
304
diff
changeset
|
143 {Anime::TitleLanguage::Romaji, "/romaji"_json_pointer}, |
|
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
304
diff
changeset
|
144 }; |
|
284
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
145 |
|
305
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
304
diff
changeset
|
146 for (const auto& [language, ptr] : map) |
|
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
304
diff
changeset
|
147 if (json.contains(ptr) && json[ptr].is_string()) |
|
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
304
diff
changeset
|
148 anime.SetTitle(language, json[ptr]); |
| 9 | 149 } |
| 150 | |
| 291 | 151 static int ParseMediaJson(const nlohmann::json& json) { |
| 175 | 152 int id = JSON::GetNumber(json, "/id"_json_pointer); |
| 9 | 153 if (!id) |
| 154 return 0; | |
| 175 | 155 |
| 9 | 156 Anime::Anime& anime = Anime::db.items[id]; |
| 157 anime.SetId(id); | |
|
286
53e3c015a973
anime: initial cross-service support
Paper <paper@paper.us.eu.org>
parents:
284
diff
changeset
|
158 anime.SetServiceId(Anime::Service::AniList, Strings::ToUtf8String(id)); |
|
53e3c015a973
anime: initial cross-service support
Paper <paper@paper.us.eu.org>
parents:
284
diff
changeset
|
159 anime.SetServiceId(Anime::Service::MyAnimeList, Strings::ToUtf8String(JSON::GetNumber(json, "/id_mal"_json_pointer))); |
| 9 | 160 |
| 11 | 161 ParseTitle(json.at("/title"_json_pointer), anime); |
| 9 | 162 |
| 175 | 163 anime.SetEpisodes(JSON::GetNumber(json, "/episodes"_json_pointer, 0)); |
| 164 anime.SetFormat(Translate::AniList::ToSeriesFormat(JSON::GetString<std::string>(json, "/format"_json_pointer, ""))); | |
| 9 | 165 |
| 258 | 166 anime.SetAiringStatus( |
| 167 Translate::AniList::ToSeriesStatus(JSON::GetString<std::string>(json, "/status"_json_pointer, ""))); | |
| 9 | 168 |
| 175 | 169 anime.SetAirDate(Date(json["/startDate"_json_pointer])); |
| 9 | 170 |
| 175 | 171 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
|
172 |
| 175 | 173 anime.SetAudienceScore(JSON::GetNumber(json, "/averageScore"_json_pointer, 0)); |
| 279 | 174 // anime.SetSeason(Translate::AniList::ToSeriesSeason(JSON::GetString<std::string>(json, "/season"_json_pointer, ""))); |
| 175 | 175 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
|
176 |
|
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
177 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
|
178 Strings::TextifySynopsis(synopsis); |
|
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
179 anime.SetSynopsis(synopsis); |
| 9 | 180 |
| 175 | 181 anime.SetGenres(JSON::GetArray<std::vector<std::string>>(json, "/genres"_json_pointer, {})); |
| 182 anime.SetTitleSynonyms(JSON::GetArray<std::vector<std::string>>(json, "/synonyms"_json_pointer, {})); | |
| 183 | |
| 10 | 184 return id; |
| 9 | 185 } |
| 186 | |
| 291 | 187 static int ParseListItem(const nlohmann::json& json) { |
| 10 | 188 int id = ParseMediaJson(json["media"]); |
| 189 | |
| 190 Anime::Anime& anime = Anime::db.items[id]; | |
| 191 | |
| 192 anime.AddToUserList(); | |
| 9 | 193 |
| 175 | 194 anime.SetUserScore(JSON::GetNumber(json, "/score"_json_pointer, 0)); |
| 195 anime.SetUserProgress(JSON::GetNumber(json, "/progress"_json_pointer, 0)); | |
| 196 ParseListStatus(JSON::GetString<std::string>(json, "/status"_json_pointer, ""), anime); | |
| 197 anime.SetUserNotes(JSON::GetString<std::string>(json, "/notes"_json_pointer, "")); | |
| 9 | 198 |
| 175 | 199 anime.SetUserDateStarted(Date(json["/startedAt"_json_pointer])); |
| 200 anime.SetUserDateCompleted(Date(json["/completedAt"_json_pointer])); | |
| 9 | 201 |
| 175 | 202 anime.SetUserTimeUpdated(JSON::GetNumber(json, "/updatedAt"_json_pointer, 0)); |
| 10 | 203 |
| 204 return id; | |
| 9 | 205 } |
| 206 | |
| 291 | 207 static int ParseList(const nlohmann::json& json) { |
| 9 | 208 for (const auto& entry : json["entries"].items()) { |
| 209 ParseListItem(entry.value()); | |
| 210 } | |
| 10 | 211 return 1; |
| 9 | 212 } |
| 213 | |
| 10 | 214 int GetAnimeList() { |
| 175 | 215 if (!account.IsValid()) { |
|
315
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
216 session.SetStatusBar("AniList: Account isn't valid!"); |
| 175 | 217 return 0; |
| 218 } | |
| 219 | |
|
315
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
220 session.SetStatusBar("AniList: Retrieving anime list..."); |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
221 |
|
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
222 /* 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
|
223 constexpr std::string_view query = "query ($id: Int) {\n" |
| 258 | 224 " MediaListCollection (userId: $id, type: ANIME) {\n" |
| 225 " lists {\n" | |
| 226 " name\n" | |
| 227 " entries {\n" | |
| 228 " score\n" | |
| 229 " notes\n" | |
| 230 " status\n" | |
| 231 " progress\n" | |
| 232 " startedAt {\n" | |
| 233 " year\n" | |
| 234 " month\n" | |
| 235 " day\n" | |
| 236 " }\n" | |
| 237 " completedAt {\n" | |
| 238 " year\n" | |
| 239 " month\n" | |
| 240 " day\n" | |
| 241 " }\n" | |
| 242 " updatedAt\n" | |
| 243 " media {\n" | |
|
304
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
244 MEDIA_FIELDS |
| 258 | 245 " }\n" |
| 246 " }\n" | |
| 247 " }\n" | |
| 248 " }\n" | |
| 249 "}\n"; | |
| 9 | 250 // clang-format off |
| 251 nlohmann::json json = { | |
| 252 {"query", query}, | |
| 253 {"variables", { | |
| 10 | 254 {"id", account.UserId()} |
| 9 | 255 }} |
| 256 }; | |
| 257 // clang-format on | |
| 175 | 258 |
|
315
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
259 session.SetStatusBar("AniList: Parsing anime list..."); |
| 175 | 260 |
|
315
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
261 nlohmann::json result; |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
262 const bool res = SendJSONRequest(json, result); |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
263 if (!res) |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
264 return 0; |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
265 |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
266 for (const auto& list : result["data"]["MediaListCollection"]["lists"].items()) |
| 10 | 267 ParseList(list.value()); |
|
304
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
268 |
|
315
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
269 session.SetStatusBar("AniList: Retrieved anime list successfully!"); |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
270 |
| 9 | 271 return 1; |
| 272 } | |
| 273 | |
| 250 | 274 /* return is a vector of anime ids */ |
| 275 std::vector<int> Search(const std::string& search) { | |
| 258 | 276 constexpr std::string_view query = "query ($search: String) {\n" |
| 277 " Page (page: 1, perPage: 50) {\n" | |
| 278 " media (search: $search, type: ANIME) {\n" | |
|
304
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
279 MEDIA_FIELDS |
| 258 | 280 " }\n" |
| 281 " }\n" | |
| 282 "}\n"; | |
| 250 | 283 |
| 284 // clang-format off | |
| 285 nlohmann::json json = { | |
| 286 {"query", query}, | |
| 287 {"variables", { | |
| 288 {"search", search} | |
| 289 }} | |
| 290 }; | |
| 291 // clang-format on | |
| 292 | |
|
315
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
293 nlohmann::json result; |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
294 const bool res = SendJSONRequest(json, result); |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
295 if (!res) |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
296 return {}; |
| 250 | 297 |
|
304
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
298 /* FIXME: error handling here */ |
| 250 | 299 std::vector<int> ret; |
|
315
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
300 ret.reserve(result["/data/Page/media"_json_pointer].size()); |
| 250 | 301 |
|
315
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
302 for (const auto& media : result["/data/Page/media"_json_pointer].items()) |
| 250 | 303 ret.push_back(ParseMediaJson(media.value())); |
| 304 | |
| 305 return ret; | |
| 306 } | |
| 307 | |
|
304
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
308 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
|
309 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
|
310 " Page(page: $page) {\n" |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
311 " 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
|
312 MEDIA_FIELDS |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
313 " }\n" |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
314 " pageInfo {\n" |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
315 " total\n" |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
316 " perPage\n" |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
317 " currentPage\n" |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
318 " lastPage\n" |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
319 " hasNextPage\n" |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
320 " }\n" |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
321 " }\n" |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
322 "}\n"; |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
323 std::vector<int> ret; |
|
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 int page = 0; |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
326 bool has_next_page = true; |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
327 while (has_next_page) { |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
328 nlohmann::json json = { |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
329 {"query", query}, |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
330 {"variables", { |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
331 {"season", Translate::AniList::ToString(season)}, |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
332 {"season_year", Strings::ToUtf8String(year)}, |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
333 {"page", page} |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
334 }} |
|
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 |
|
315
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
337 nlohmann::json result; |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
338 const bool res = SendJSONRequest(json, result); |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
339 if (!res) |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
340 return {}; |
|
304
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
341 |
|
315
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
342 ret.reserve(ret.capacity() + result["data"]["Page"]["media"].size()); |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
343 |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
344 for (const auto& media : result["data"]["Page"]["media"].items()) |
|
304
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
345 ret.push_back(ParseMediaJson(media.value())); |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
346 |
|
315
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
347 has_next_page = JSON::GetBoolean(result, "/data/Page/pageInfo/hasNextPage"_json_pointer, false); |
|
304
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
348 if (has_next_page) |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
349 page++; |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
350 } |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
351 |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
352 return ret; |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
353 } |
|
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
354 |
|
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
355 int UpdateAnimeEntry(int id) { |
| 9 | 356 /** |
| 357 * possible values: | |
| 15 | 358 * |
| 9 | 359 * int mediaId, |
| 360 * MediaListStatus status, | |
| 361 * float score, | |
| 362 * int scoreRaw, | |
| 363 * int progress, | |
|
184
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
364 * int progressVolumes, // manga-specific. |
|
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
365 * int repeat, // rewatch |
| 258 | 366 * int priority, |
| 9 | 367 * bool private, |
| 368 * string notes, | |
| 369 * bool hiddenFromStatusLists, | |
| 370 * string[] customLists, | |
| 371 * float[] advancedScores, | |
| 372 * Date startedAt, | |
| 373 * Date completedAt | |
| 258 | 374 **/ |
|
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
375 Anime::Anime& anime = Anime::db.items[id]; |
|
184
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
376 if (!anime.IsInUserList()) |
|
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
377 return 0; |
|
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
378 |
| 291 | 379 std::optional<std::string> service_id = anime.GetServiceId(Anime::Service::AniList); |
| 380 if (!service_id) | |
| 381 return 0; | |
| 382 | |
|
315
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
383 session.SetStatusBar("AniList: Updating anime entry..."); |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
384 |
| 250 | 385 constexpr std::string_view query = |
| 258 | 386 "mutation ($media_id: Int, $progress: Int, $status: MediaListStatus, $score: Int, $notes: String, $start: " |
| 387 "FuzzyDateInput, $comp: FuzzyDateInput, $repeat: Int) {\n" | |
| 388 " SaveMediaListEntry (mediaId: $media_id, progress: $progress, status: $status, scoreRaw: $score, notes: " | |
| 389 "$notes, startedAt: $start, completedAt: $comp, repeat: $repeat) {\n" | |
| 390 " id\n" | |
| 391 " }\n" | |
| 392 "}\n"; | |
| 9 | 393 // clang-format off |
| 394 nlohmann::json json = { | |
| 395 {"query", query}, | |
| 396 {"variables", { | |
| 291 | 397 {"media_id", Strings::ToInt<int64_t>(service_id.value())}, |
| 10 | 398 {"progress", anime.GetUserProgress()}, |
| 15 | 399 {"status", ListStatusToString(anime)}, |
| 10 | 400 {"score", anime.GetUserScore()}, |
| 77 | 401 {"notes", anime.GetUserNotes()}, |
| 402 {"start", anime.GetUserDateStarted().GetAsAniListJson()}, | |
|
184
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
403 {"comp", anime.GetUserDateCompleted().GetAsAniListJson()}, |
|
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
404 {"repeat", anime.GetUserRewatchedTimes()} |
| 9 | 405 }} |
| 406 }; | |
| 407 // clang-format on | |
| 175 | 408 |
|
315
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
409 nlohmann::json result; |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
410 const bool ret = SendJSONRequest(json, result); |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
411 if (!ret) |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
412 return 0; |
| 175 | 413 |
|
315
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
414 session.SetStatusBar("AniList: Anime entry updated successfully!"); |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
415 |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
416 return JSON::GetNumber(result, "/data/SaveMediaListEntry/id"_json_pointer, 0); |
| 9 | 417 } |
| 418 | |
| 291 | 419 static int ParseUser(const nlohmann::json& json) { |
| 175 | 420 account.SetUserId(JSON::GetNumber(json, "/id"_json_pointer, 0)); |
| 10 | 421 return account.UserId(); |
| 9 | 422 } |
| 423 | |
|
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
424 bool AuthorizeUser() { |
| 9 | 425 /* Prompt for PIN */ |
| 258 | 426 QDesktopServices::openUrl(QUrl(Strings::ToQString("https://anilist.co/api/v2/oauth/authorize?client_id=" + |
| 291 | 427 std::string(CLIENT_ID) + "&response_type=token"))); |
| 175 | 428 |
| 9 | 429 bool ok; |
| 430 QString token = QInputDialog::getText( | |
| 258 | 431 0, "Credentials needed!", "Please enter the code given to you after logging in to AniList:", QLineEdit::Normal, |
| 432 "", &ok); | |
| 175 | 433 |
| 434 if (!ok || token.isEmpty()) | |
|
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
435 return false; |
| 175 | 436 |
| 437 account.SetAuthToken(Strings::ToUtf8String(token)); | |
| 438 | |
|
315
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
439 session.SetStatusBar("AniList: Requesting user ID..."); |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
440 |
|
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
441 constexpr std::string_view query = "query {\n" |
| 258 | 442 " Viewer {\n" |
| 443 " id\n" | |
| 444 " name\n" | |
| 445 " mediaListOptions {\n" | |
| 446 " scoreFormat\n" // this will be used... eventually | |
| 447 " }\n" | |
| 448 " }\n" | |
| 449 "}\n"; | |
| 9 | 450 nlohmann::json json = { |
| 258 | 451 {"query", query} |
| 452 }; | |
| 175 | 453 |
|
315
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
454 /* SendJSONRequest handles status errors */ |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
455 nlohmann::json result; |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
456 const bool ret = SendJSONRequest(json, result); |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
457 if (!ret) |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
458 return 0; |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
459 |
|
34347fd2a2de
session: allow printing status messages
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
460 session.SetStatusBar("AniList: Successfully retrieved user data!"); |
| 175 | 461 |
| 74 | 462 ParseUser(ret["data"]["Viewer"]); |
|
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
463 return true; |
| 9 | 464 } |
| 465 | |
| 63 | 466 } // namespace AniList |
| 467 } // namespace Services |
