Mercurial > minori
comparison src/services/kitsu.cc @ 387:04a894e96355
kitsu: implement anime search
yay
| author | Paper <paper@tflc.us> |
|---|---|
| date | Thu, 06 Nov 2025 09:46:09 -0500 |
| parents | 47c9f8502269 |
| children | 83aa0ddd1a46 |
comparison
equal
deleted
inserted
replaced
| 386:e89901683d72 | 387:04a894e96355 |
|---|---|
| 494 session.SetStatusBar(Strings::Translate("Kitsu: Successfully received library data!")); | 494 session.SetStatusBar(Strings::Translate("Kitsu: Successfully received library data!")); |
| 495 | 495 |
| 496 return 1; | 496 return 1; |
| 497 } | 497 } |
| 498 | 498 |
| 499 /* :) */ | |
| 500 static const std::map<std::string, std::string> anime_params = { | |
| 501 {"include", Strings::Implode( | |
| 502 { | |
| 503 "categories", | |
| 504 "animeProductions", | |
| 505 "animeProductions.producer", | |
| 506 }, ",")} | |
| 507 }; | |
| 508 | |
| 499 bool RetrieveAnimeMetadata(int id) | 509 bool RetrieveAnimeMetadata(int id) |
| 500 { | 510 { |
| 501 /* TODO: the genres should *probably* be a std::optional */ | 511 /* TODO: the genres should *probably* be a std::optional */ |
| 502 Anime::Anime &anime = Anime::db.items[id]; | 512 Anime::Anime &anime = Anime::db.items[id]; |
| 503 if (anime.GetGenres().size() > 0 && anime.GetProducers().size()) | 513 if (anime.GetGenres().size() > 0 && anime.GetProducers().size()) |
| 507 if (!service_id) | 517 if (!service_id) |
| 508 return false; | 518 return false; |
| 509 | 519 |
| 510 session.SetStatusBar(Strings::Translate("Kitsu: Retrieving anime metadata...")); | 520 session.SetStatusBar(Strings::Translate("Kitsu: Retrieving anime metadata...")); |
| 511 | 521 |
| 512 static const std::map<std::string, std::string> params = { | 522 std::optional<nlohmann::json> response = SendJSONAPIRequest("/anime/" + service_id.value(), anime_params); |
| 513 {"include", Strings::Implode( | |
| 514 { | |
| 515 "categories", | |
| 516 "animeProductions", | |
| 517 "animeProductions.producer", | |
| 518 }, ",")} | |
| 519 }; | |
| 520 | |
| 521 std::optional<nlohmann::json> response = SendJSONAPIRequest("/anime/" + service_id.value(), params); | |
| 522 if (!response) | 523 if (!response) |
| 523 return false; | 524 return false; |
| 524 | 525 |
| 525 const auto &json = response.value(); | 526 const auto &json = response.value(); |
| 526 | 527 |
| 538 } | 539 } |
| 539 | 540 |
| 540 /* unimplemented for now */ | 541 /* unimplemented for now */ |
| 541 std::vector<int> Search(const std::string &search) | 542 std::vector<int> Search(const std::string &search) |
| 542 { | 543 { |
| 543 return {}; | 544 session.SetStatusBar(Strings::Translate("Kitsu: Retrieving search results...")); |
| 545 | |
| 546 std::map<std::string, std::string> params = anime_params; | |
| 547 params["filter[text]"] = search; | |
| 548 // ... for some reason, this seems to be buggy | |
| 549 //AddAnimeFilters(params); | |
| 550 | |
| 551 std::optional<nlohmann::json> response = SendJSONAPIRequest("/anime", params); | |
| 552 if (!response) | |
| 553 return {}; | |
| 554 | |
| 555 const auto &json = response.value(); | |
| 556 | |
| 557 if (!json.contains("/data"_json_pointer) || !json["/data"_json_pointer].is_array()) { | |
| 558 session.SetStatusBar( | |
| 559 Strings::Translate("Kitsu: Server returned bad data when trying to retrieve search results!")); | |
| 560 return {}; | |
| 561 } | |
| 562 | |
| 563 std::vector<int> ids; | |
| 564 | |
| 565 for (const auto &item : json["/data"_json_pointer]) { | |
| 566 int id = ParseAnimeJson(item); | |
| 567 if (!id) | |
| 568 return {}; | |
| 569 | |
| 570 ids.push_back(id); | |
| 571 } | |
| 572 | |
| 573 return ids; | |
| 544 } | 574 } |
| 545 | 575 |
| 546 bool GetSeason(Anime::Season season) | 576 bool GetSeason(Anime::Season season) |
| 547 { | 577 { |
| 548 return false; | 578 return false; |
