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