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