annotate src/anilist.cpp @ 7:07a9095eaeed

Update Refactored some code, moved some around
author Paper <mrpapersonic@gmail.com>
date Thu, 24 Aug 2023 23:11:38 -0400
parents 1d82f6e04d7d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
1 #include <QMessageBox>
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
2 #include <QDesktopServices>
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
3 #include <QInputDialog>
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
4 #include <QLineEdit>
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
5 #include <curl/curl.h>
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
6 #include <chrono>
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
7 #include <exception>
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
8 #include <format>
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
9 #include "json.h"
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
10 #include "anilist.h"
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
11 #include "anime.h"
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
12 #include "config.h"
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
13 #include "string_utils.h"
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
14 #include "session.h"
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
15 #define CLIENT_ID "13706"
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
16
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
17 CURL* AniList::curl = NULL;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
18 CURLcode AniList::res = (CURLcode)0;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
19
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
20 size_t AniList::CurlWriteCallback(void *contents, size_t size, size_t nmemb, void *userdata) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
21 ((std::string*)userdata)->append((char*)contents, size * nmemb);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
22 return size * nmemb;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
23 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
24
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
25 std::string AniList::SendRequest(std::string data) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
26 struct curl_slist *list = NULL;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
27 std::string userdata;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
28 curl = curl_easy_init();
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
29 if (curl) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
30 list = curl_slist_append(list, "Accept: application/json");
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
31 list = curl_slist_append(list, "Content-Type: application/json");
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
32 std::string bearer = "Authorization: Bearer " + session.config.anilist.auth_token;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
33 list = curl_slist_append(list, bearer.c_str());
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
34 curl_easy_setopt(curl, CURLOPT_URL, "https://graphql.anilist.co");
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
35 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
36 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
37 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &userdata);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
38 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &CurlWriteCallback);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
39 /* FIXME: This sucks. When using HTTPS, we should ALWAYS make sure that our peer
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
40 is actually valid. I assume the best way to go about this would be to bundle a
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
41 certificate file, and if it's not found we should *prompt the user* and ask them
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
42 if it's okay to contact AniList WITHOUT verification. If so, we're golden, and this
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
43 flag will be set. If not, we should abort mission.
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
44
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
45 For this program, it's probably fine to just contact AniList without
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
46 HTTPS verification. However it should still be in the list of things to do... */
4
5af270662505 Set override functions as override
Paper <mrpapersonic@gmail.com>
parents: 3
diff changeset
47 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
48 res = curl_easy_perform(curl);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
49 curl_slist_free_all(list);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
50 if (res != CURLE_OK) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
51 QMessageBox box(QMessageBox::Icon::Critical, "", QString("curl_easy_perform(curl) failed!: ") + QString(curl_easy_strerror(res)));
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
52 box.exec();
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
53 curl_easy_cleanup(curl);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
54 return "";
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
55 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
56 curl_easy_cleanup(curl);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
57 return userdata;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
58 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
59 return "";
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
60 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
61
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
62 int AniList::GetUserId(std::string name) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
63 #define QUERY "query ($name: String) {\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
64 " User (name: $name) {\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
65 " id\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
66 " }\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
67 "}\n"
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
68 nlohmann::json json = {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
69 {"query", QUERY},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
70 {"variables", {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
71 {"name", name}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
72 }}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
73 };
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
74 auto ret = nlohmann::json::parse(SendRequest(json.dump()));
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
75 return JSON::GetInt(ret, "/data/User/id"_json_pointer);
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
76 #undef QUERY
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
77 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
78
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
79 /* Maps to convert string forms to our internal enums */
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
80
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
81 std::map<std::string, enum AnimeWatchingStatus> StringToAnimeWatchingMap = {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
82 {"CURRENT", CURRENT},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
83 {"PLANNING", PLANNING},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
84 {"COMPLETED", COMPLETED},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
85 {"DROPPED", DROPPED},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
86 {"PAUSED", PAUSED},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
87 {"REPEATING", REPEATING}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
88 };
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
89
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
90 std::map<std::string, enum AnimeAiringStatus> StringToAnimeAiringMap = {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
91 {"FINISHED", FINISHED},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
92 {"RELEASING", RELEASING},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
93 {"NOT_YET_RELEASED", NOT_YET_RELEASED},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
94 {"CANCELLED", CANCELLED},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
95 {"HIATUS", HIATUS}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
96 };
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
97
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
98 std::map<std::string, enum AnimeSeason> StringToAnimeSeasonMap = {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
99 {"WINTER", WINTER},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
100 {"SPRING", SPRING},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
101 {"SUMMER", SUMMER},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
102 {"FALL", FALL}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
103 };
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
104
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
105 std::map<std::string, enum AnimeFormat> StringToAnimeFormatMap = {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
106 {"TV", TV},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
107 {"TV_SHORT", TV_SHORT},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
108 {"MOVIE", MOVIE},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
109 {"SPECIAL", SPECIAL},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
110 {"OVA", OVA},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
111 {"ONA", ONA},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
112 {"MUSIC", MUSIC},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
113 {"MANGA", MANGA},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
114 {"NOVEL", NOVEL},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
115 {"ONE_SHOT", ONE_SHOT}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
116 };
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
117
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
118 int AniList::UpdateAnimeList(std::vector<AnimeList>* anime_lists, int id) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
119 /* NOTE: these should be in the qrc file */
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
120 #define QUERY "query ($id: Int) {\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
121 " MediaListCollection (userId: $id, type: ANIME) {\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
122 " lists {\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
123 " name\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
124 " entries {\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
125 " score\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
126 " notes\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
127 " progress\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
128 " startedAt {\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
129 " year\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
130 " month\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
131 " day\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
132 " }\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
133 " completedAt {\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
134 " year\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
135 " month\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
136 " day\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
137 " }\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
138 " updatedAt\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
139 " media {\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
140 " id\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
141 " title {\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
142 " romaji\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
143 " english\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
144 " native\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
145 " }\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
146 " format\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
147 " status\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
148 " averageScore\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
149 " season\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
150 " startDate {\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
151 " year\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
152 " month\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
153 " day\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
154 " }\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
155 " genres\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
156 " episodes\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
157 " duration\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
158 " synonyms\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
159 " description(asHtml: false)\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
160 " }\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
161 " }\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
162 " }\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
163 " }\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
164 "}\n"
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
165 nlohmann::json json = {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
166 {"query", QUERY},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
167 {"variables", {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
168 {"id", id}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
169 }}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
170 };
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
171 /* TODO: do a try catch here, catch any json errors and then call
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
172 Authorize() if needed */
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
173 auto res = nlohmann::json::parse(SendRequest(json.dump()));
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
174 /* TODO: make sure that we actually need the wstring converter and see
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
175 if we can just get wide strings back from nlohmann::json */
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
176 for (const auto& list : res["data"]["MediaListCollection"]["lists"].items()) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
177 /* why are the .key() values strings?? */
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
178 AnimeList anime_list;
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
179 anime_list.name = JSON::GetString(list.value(), "/name"_json_pointer);
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
180 for (const auto& entry : list.value()["entries"].items()) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
181 Anime anime;
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
182 anime.score = JSON::GetInt(entry.value(), "/score"_json_pointer);
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
183 anime.progress = JSON::GetInt(entry.value(), "/progress"_json_pointer);
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
184 anime.status = StringToAnimeWatchingMap[JSON::GetString(entry.value(), "/status"_json_pointer)];
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
185 anime.notes = JSON::GetString(entry.value(), "/notes"_json_pointer);
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
186
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
187 anime.started.SetYear(JSON::GetInt(entry.value(), "/startedAt/year"_json_pointer));
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
188 anime.started.SetMonth(JSON::GetInt(entry.value(), "/startedAt/month"_json_pointer));
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
189 anime.started.SetDay(JSON::GetInt(entry.value(), "/startedAt/day"_json_pointer));
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
190
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
191 anime.completed.SetYear(JSON::GetInt(entry.value(), "/completedAt/year"_json_pointer));
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
192 anime.completed.SetMonth(JSON::GetInt(entry.value(), "/completedAt/month"_json_pointer));
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
193 anime.completed.SetDay(JSON::GetInt(entry.value(), "/completedAt/day"_json_pointer));
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
194
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
195 anime.updated = JSON::GetInt(entry.value(), "/updatedAt"_json_pointer);
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
196
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
197 anime.title.native = JSON::GetString(entry.value(), "/media/title/native"_json_pointer);
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
198 anime.title.english = JSON::GetString(entry.value(), "/media/title/english"_json_pointer);
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
199 anime.title.romaji = JSON::GetString(entry.value(), "/media/title/romaji"_json_pointer);
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
200
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
201 anime.id = JSON::GetInt(entry.value(), "/media/id"_json_pointer);
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
202 anime.episodes = JSON::GetInt(entry.value(), "/media/episodes"_json_pointer);
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
203 anime.type = StringToAnimeFormatMap[JSON::GetString(entry.value()["media"], "/media/format"_json_pointer)];
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
204
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
205 anime.airing = StringToAnimeAiringMap[JSON::GetString(entry.value()["media"], "/media/status"_json_pointer)];
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
206
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
207 anime.air_date.SetYear(JSON::GetInt(entry.value(), "/media/startDate/year"_json_pointer));
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
208 anime.air_date.SetMonth(JSON::GetInt(entry.value(), "/media/startDate/month"_json_pointer));
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
209 anime.air_date.SetDay(JSON::GetInt(entry.value(), "/media/startDate/day"_json_pointer));
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
210
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
211 anime.audience_score = JSON::GetInt(entry.value(), "/media/averageScore"_json_pointer);
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
212 anime.season = StringToAnimeSeasonMap[JSON::GetString(entry.value(), "/media/season"_json_pointer)];
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
213 anime.duration = JSON::GetInt(entry.value(), "/media/duration"_json_pointer);
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
214 anime.synopsis = StringUtils::TextifySynopsis(JSON::GetString(entry.value(), "/media/description"_json_pointer));
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
215
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
216 if (entry.value().contains("/media/genres"_json_pointer) && entry.value()["/media/genres"_json_pointer].is_array())
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
217 anime.genres = entry.value()["/media/genres"_json_pointer].get<std::vector<std::string>>();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
218 if (entry.value().contains("/media/synonyms"_json_pointer) && entry.value()["/media/synonyms"_json_pointer].is_array())
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
219 anime.synonyms = entry.value()["/media/synonyms"_json_pointer].get<std::vector<std::string>>();
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
220 anime_list.Add(anime);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
221 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
222 anime_lists->push_back(anime_list);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
223 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
224 return 1;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
225 #undef QUERY
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
226 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
227
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
228 int AniList::Authorize() {
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
229 /* Prompt for PIN */
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
230 QDesktopServices::openUrl(QUrl("https://anilist.co/api/v2/oauth/authorize?client_id=" CLIENT_ID "&response_type=token"));
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
231 bool ok;
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
232 QString token = QInputDialog::getText(0, "Credentials needed!", "Please enter the code given to you after logging in to AniList:", QLineEdit::Normal, "", &ok);
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
233 if (ok && !token.isEmpty()) {
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
234 session.config.anilist.auth_token = token.toStdString();
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
235 } else { // fail
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 4
diff changeset
236 return 0;
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
237 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
238 return 1;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
239 }