comparison src/services/kitsu.cc @ 393:963047512d34

*: clang-format
author Paper <paper@tflc.us>
date Fri, 07 Nov 2025 07:16:15 -0500
parents a72d6d7b3568
children de10a647eb9b
comparison
equal deleted inserted replaced
392:a72d6d7b3568 393:963047512d34
138 138
139 /* ----------------------------------------------------------------------------- */ 139 /* ----------------------------------------------------------------------------- */
140 140
141 static std::optional<nlohmann::json> SendJSONAPIRequest(const std::string &path, 141 static std::optional<nlohmann::json> SendJSONAPIRequest(const std::string &path,
142 const std::map<std::string, std::string> &params = {}, 142 const std::map<std::string, std::string> &params = {},
143 const std::string &data = "", 143 const std::string &data = "", HTTP::Type type = HTTP::Type::Get)
144 HTTP::Type type = HTTP::Type::Get)
145 { 144 {
146 std::optional<std::string> token = AccountAccessToken(); 145 std::optional<std::string> token = AccountAccessToken();
147 if (!token) 146 if (!token)
148 return std::nullopt; 147 return std::nullopt;
149 148
549 session.SetStatusBar(Strings::Translate("Kitsu: Retrieving search results...")); 548 session.SetStatusBar(Strings::Translate("Kitsu: Retrieving search results..."));
550 549
551 std::map<std::string, std::string> params = anime_params; 550 std::map<std::string, std::string> params = anime_params;
552 params["filter[text]"] = search; 551 params["filter[text]"] = search;
553 // ... for some reason, this seems to be buggy 552 // ... for some reason, this seems to be buggy
554 //AddAnimeFilters(params); 553 // AddAnimeFilters(params);
555 554
556 std::optional<nlohmann::json> response = SendJSONAPIRequest("/anime", params); 555 std::optional<nlohmann::json> response = SendJSONAPIRequest("/anime", params);
557 if (!response) 556 if (!response)
558 return {}; 557 return {};
559 558
579 } 578 }
580 579
581 bool GetSeason(Anime::Season season) 580 bool GetSeason(Anime::Season season)
582 { 581 {
583 static const std::map<Anime::Season::Name, std::string> map = { 582 static const std::map<Anime::Season::Name, std::string> map = {
584 {Anime::Season::Name::Winter, "winter"}, 583 {Anime::Season::Name::Winter, "winter"},
585 {Anime::Season::Name::Spring, "spring"}, 584 {Anime::Season::Name::Spring, "spring"},
586 {Anime::Season::Name::Summer, "summer"}, 585 {Anime::Season::Name::Summer, "summer"},
587 {Anime::Season::Name::Autumn, "fall"}, 586 {Anime::Season::Name::Autumn, "fall" },
588 }; 587 };
589 588
590 session.SetStatusBar(Strings::Translate("Kitsu: Retrieving season data...")); 589 session.SetStatusBar(Strings::Translate("Kitsu: Retrieving season data..."));
591 590
592 std::map<std::string, std::string> params = anime_params; 591 std::map<std::string, std::string> params = anime_params;
593 params["filter[season]"] = map.at(season.season); 592 params["filter[season]"] = map.at(season.season);
594 params["filter[seasonYear]"] = Strings::ToUtf8String(season.year); 593 params["filter[seasonYear]"] = Strings::ToUtf8String(season.year);
595 // ... for some reason, this seems to be buggy 594 // ... for some reason, this seems to be buggy
596 //AddAnimeFilters(params); 595 // AddAnimeFilters(params);
597 596
598 std::optional<nlohmann::json> response = SendJSONAPIRequest("/anime", params); 597 std::optional<nlohmann::json> response = SendJSONAPIRequest("/anime", params);
599 if (!response) 598 if (!response)
600 return false; 599 return false;
601 600
614 } 613 }
615 614
616 static std::string UserStatusToString(Anime::ListStatus status) 615 static std::string UserStatusToString(Anime::ListStatus status)
617 { 616 {
618 switch (status) { 617 switch (status) {
619 case Anime::ListStatus::Planning: return "planned"; 618 case Anime::ListStatus::Planning:
620 case Anime::ListStatus::Completed: return "completed"; 619 return "planned";
621 case Anime::ListStatus::Dropped: return "dropped"; 620 case Anime::ListStatus::Completed:
622 case Anime::ListStatus::Paused: return "on_hold"; 621 return "completed";
623 default: break; 622 case Anime::ListStatus::Dropped:
623 return "dropped";
624 case Anime::ListStatus::Paused:
625 return "on_hold";
626 default:
627 break;
624 } 628 }
625 629
626 return "current"; 630 return "current";
627 } 631 }
628 632
633 637
634 if (!anime.IsInUserList()) 638 if (!anime.IsInUserList())
635 return 0; /* WTF */ 639 return 0; /* WTF */
636 640
637 nlohmann::json json = { 641 nlohmann::json json = {
638 {"data", { 642 {"data",
639 {"type", "libraryEntries"}, 643 {{"type", "libraryEntries"},
640 {"attributes", { 644 {"attributes",
641 {"status", UserStatusToString(anime.GetUserStatus())}, 645 {
642 {"progress", anime.GetUserProgress()}, 646 {"status", UserStatusToString(anime.GetUserStatus())},
643 {"reconsuming", anime.GetUserIsRewatching()}, 647 {"progress", anime.GetUserProgress()},
644 {"reconsumeCount", anime.GetUserRewatchedTimes()}, 648 {"reconsuming", anime.GetUserIsRewatching()},
645 {"notes", anime.GetUserNotes()}, 649 {"reconsumeCount", anime.GetUserRewatchedTimes()},
646 {"private", anime.GetUserIsPrivate()}, 650 {"notes", anime.GetUserNotes()},
647 // WTF is reactionSkipped? 651 {"private", anime.GetUserIsPrivate()},
648 {"startedAt", anime.GetUserDateStarted().GetAsISO8601()}, 652 // WTF is reactionSkipped?
649 {"finishedAt", anime.GetUserDateCompleted().GetAsISO8601()}, 653 {"startedAt", anime.GetUserDateStarted().GetAsISO8601()},
650 }}, 654 {"finishedAt", anime.GetUserDateCompleted().GetAsISO8601()},
651 {"relationships", { 655 }},
652 {"anime", { 656 {"relationships",
653 {"data", { 657 {{"anime",
654 {"type", "anime"}, 658 {{"data",
655 {"id", anime.GetServiceId(Anime::Service::Kitsu)}, 659 {
656 }} 660 {"type", "anime"},
657 }}, 661 {"id", anime.GetServiceId(Anime::Service::Kitsu)},
658 {"user", { 662 }}}},
659 {"data", { 663 {"user",
660 {"type", "users"}, 664 {{"data",
661 {"id", session.config.auth.kitsu.user_id}, 665 {
662 }} 666 {"type", "users"},
663 }} 667 {"id", session.config.auth.kitsu.user_id},
664 }} 668 }}}}}}}}
665 }} 669 };
666 };
667 670
668 nlohmann::json &attributes = json["data"]["attributes"]; 671 nlohmann::json &attributes = json["data"]["attributes"];
669 672
670 score = anime.GetUserScore() / 5; 673 score = anime.GetUserScore() / 5;
671 if (score > 0) { 674 if (score > 0) {