diff --git a/include/crowsite/requests/jellyfin.h b/include/crowsite/requests/jellyfin.h index e26e02f..457b945 100644 --- a/include/crowsite/requests/jellyfin.h +++ b/include/crowsite/requests/jellyfin.h @@ -5,4 +5,20 @@ #ifndef CROWSITE_JELLYFIN_H #define CROWSITE_JELLYFIN_H +#include + +namespace cs::jellyfin +{ + + void setToken(std::string_view token); + + void processUserData(); + + std::string generateAuthHeader(); + std::string getUserData(); + + bool hasUser(std::string_view username); + +} + #endif //CROWSITE_JELLYFIN_H diff --git a/src/crowsite/requests/curl.cpp b/src/crowsite/requests/curl.cpp index 1467e49..81b6d37 100644 --- a/src/crowsite/requests/curl.cpp +++ b/src/crowsite/requests/curl.cpp @@ -17,8 +17,9 @@ namespace cs auto* name = (const char*) userdata; std::string site{name}; - blt::scoped_buffer response{size * nmemb}; + blt::scoped_buffer response{size * nmemb + 1}; memcpy(response.ptr(), ptr, size * nmemb); + response[size * nmemb] = '\0'; if (responses.find(site) != responses.end()){ std::string res{response.ptr()}; @@ -66,7 +67,6 @@ namespace cs void request::get(const std::string& domain, const std::string& data) { - BLT_WARN("Domain: %s", domain.c_str()); auto full = domain + data; curl_easy_setopt(handler, CURLOPT_URL, full.c_str()); curl_easy_setopt(handler, CURLOPT_WRITEDATA, domain.c_str()); diff --git a/src/crowsite/requests/jellyfin.cpp b/src/crowsite/requests/jellyfin.cpp index bb9d409..661a623 100644 --- a/src/crowsite/requests/jellyfin.cpp +++ b/src/crowsite/requests/jellyfin.cpp @@ -2,4 +2,54 @@ // Created by brett on 8/9/23. // #include -#include \ No newline at end of file +#include +#include +#include +#include +#include + +struct +{ + std::string token; + HASHMAP user_ids; +} GLOBALS; + +void cs::jellyfin::setToken(std::string_view token) +{ + GLOBALS.token = token; +} + +void cs::jellyfin::processUserData() +{ + auto data = getUserData(); + + auto json = crow::json::load(data); + + for (const auto& user : json){ + auto username = user["Name"].s(); + auto userid = user["Id"].s(); + //BLT_TRACE("Processing %s = %s", username.operator std::string().c_str(), userid.operator std::string().c_str()); + GLOBALS.user_ids[username] = userid; + } +} + +std::string cs::jellyfin::getUserData() +{ +#define url "https://media.tpgc.me/Users" + + cs::request request; + request.setAuthHeader(generateAuthHeader()); + request.get(url); + + return cs::request::getResponse(url); +} + +std::string cs::jellyfin::generateAuthHeader() +{ + return "MediaBrowser Client=Crowsite, Device=YourMom, Token=" + GLOBALS.token; +} + +bool cs::jellyfin::hasUser(std::string_view username) +{ + return GLOBALS.user_ids.find(std::string(username)) != GLOBALS.user_ids.end(); +} diff --git a/src/main.cpp b/src/main.cpp index 0624d07..29e3eea 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -48,6 +49,8 @@ int main(int argc, const char** argv) blt::arg_parse parser; parser.addArgument(blt::arg_builder("token").build()); auto args = parser.parse_args(argc, argv); + cs::jellyfin::setToken(blt::arg_parse::get(args["token"])); + cs::jellyfin::processUserData(); // blt::string::StringBuffer buffer; // std::stringstream stream; @@ -101,12 +104,7 @@ int main(int argc, const char** argv) // // return 0; - cs::request get; - get.setAuthHeader("MediaBrowser Client=Crowsite, Device=YourMom, Token=" + blt::arg_parse::get(args["token"])); - get.get("https://media.tpgc.me/Users"); - const auto& f = cs::request::getResponse("https://media.tpgc.me/Users"); - BLT_TRACE(f); BLT_INFO("Starting site %s.", SITE_NAME); crow::mustache::set_global_base(SITE_FILES_PATH);