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