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