annotate src/services/anilist.cc @ 341:052ec053ee37

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