Mercurial > minori
comparison src/services/anilist.cpp @ 75:d3e9310598b1
*: refactor some stuff
text: "TextParagraph"s are now called sections, because that's the
actual word for it :P
text: new classes: Line and OneLineSection, solves many problems with
paragraphs that are only one line long (ex. going out of bounds)
http: reworked http stuff to allow threaded get requests, also moved it
to its own file to (hopefully) remove clutter
eventually I'll make a threaded post request method and use that in
the "basic" function
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Wed, 04 Oct 2023 01:42:30 -0400 |
parents | 5ccb99bfa605 |
children | 3364fadc8a36 |
comparison
equal
deleted
inserted
replaced
74:5ccb99bfa605 | 75:d3e9310598b1 |
---|---|
1 #include "services/anilist.h" | 1 #include "services/anilist.h" |
2 #include "core/anime.h" | 2 #include "core/anime.h" |
3 #include "core/anime_db.h" | 3 #include "core/anime_db.h" |
4 #include "core/config.h" | 4 #include "core/config.h" |
5 #include "core/json.h" | 5 #include "core/json.h" |
6 #include "core/http.h" | |
6 #include "core/session.h" | 7 #include "core/session.h" |
7 #include "core/strings.h" | 8 #include "core/strings.h" |
8 #include "gui/translate/anilist.h" | 9 #include "gui/translate/anilist.h" |
9 #include <QDesktopServices> | 10 #include <QDesktopServices> |
10 #include <QInputDialog> | 11 #include <QInputDialog> |
11 #include <QLineEdit> | 12 #include <QLineEdit> |
12 #include <QMessageBox> | 13 #include <QMessageBox> |
13 #include <QUrl> | 14 #include <QUrl> |
14 #include <chrono> | 15 #include <chrono> |
15 #include <curl/curl.h> | |
16 #include <exception> | 16 #include <exception> |
17 #define CLIENT_ID "13706" | 17 #define CLIENT_ID "13706" |
18 | 18 |
19 using namespace nlohmann::literals::json_literals; | 19 using namespace nlohmann::literals::json_literals; |
20 | 20 |
35 bool Authenticated() const { return !AuthToken().empty(); } | 35 bool Authenticated() const { return !AuthToken().empty(); } |
36 }; | 36 }; |
37 | 37 |
38 static Account account; | 38 static Account account; |
39 | 39 |
40 static size_t CurlWriteCallback(void* contents, size_t size, size_t nmemb, void* userdata) { | |
41 reinterpret_cast<std::string*>(userdata)->append(reinterpret_cast<char*>(contents), size * nmemb); | |
42 return size * nmemb; | |
43 } | |
44 | |
45 /* A wrapper around cURL to send requests to AniList */ | |
46 std::string SendRequest(std::string data) { | 40 std::string SendRequest(std::string data) { |
47 struct curl_slist* list = NULL; | 41 std::vector<std::string> headers = { |
48 std::string userdata; | 42 "Authorization: Bearer " + account.AuthToken(), |
49 CURL* curl = curl_easy_init(); | 43 "Accept: application/json", |
50 if (curl) { | 44 "Content-Type: application/json" |
51 std::string bearer = "Authorization: Bearer " + account.AuthToken(); | 45 }; |
52 list = curl_slist_append(list, "Accept: application/json"); | 46 return HTTP::PerformBasicPostRequest("https://graphql.anilist.co", data, headers); |
53 list = curl_slist_append(list, "Content-Type: application/json"); | |
54 list = curl_slist_append(list, bearer.c_str()); | |
55 curl_easy_setopt(curl, CURLOPT_URL, "https://graphql.anilist.co"); | |
56 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str()); | |
57 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); | |
58 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &userdata); | |
59 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &CurlWriteCallback); | |
60 /* Use system certs... useful on Windows. */ | |
61 curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA); | |
62 CURLcode res = curl_easy_perform(curl); | |
63 session.IncrementRequests(); | |
64 curl_slist_free_all(list); | |
65 curl_easy_cleanup(curl); | |
66 if (res != CURLE_OK) { | |
67 QMessageBox box(QMessageBox::Icon::Critical, "", | |
68 QString("curl_easy_perform(curl) failed!: ") + QString(curl_easy_strerror(res))); | |
69 box.exec(); | |
70 return ""; | |
71 } | |
72 return userdata; | |
73 } | |
74 return ""; | |
75 } | 47 } |
76 | 48 |
77 void ParseListStatus(std::string status, Anime::Anime& anime) { | 49 void ParseListStatus(std::string status, Anime::Anime& anime) { |
78 std::unordered_map<std::string, Anime::ListStatus> map = { | 50 std::unordered_map<std::string, Anime::ListStatus> map = { |
79 {"CURRENT", Anime::ListStatus::CURRENT }, | 51 {"CURRENT", Anime::ListStatus::CURRENT }, |