From b942b58b3478bc450c72aa264230df4853cf51dc Mon Sep 17 00:00:00 2001 From: EinTim23 Date: Fri, 1 Nov 2024 15:04:43 +0100 Subject: [PATCH] remove submodule --- CMakeLists.txt | 2 +- src/main.cpp | 53 +++++++++++++++------------ src/utils.hpp | 85 +++++++++++++++++++++++++++++-------------- vendor/CMakeLists.txt | 1 + vendor/discord-rpc | 1 - vendor/mbedtls | 2 +- 6 files changed, 91 insertions(+), 53 deletions(-) delete mode 160000 vendor/discord-rpc diff --git a/CMakeLists.txt b/CMakeLists.txt index ab4f704..805bb81 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,5 +4,5 @@ file(GLOB_RECURSE SOURCES "src/*.cpp") add_executable (rpc ${SOURCES}) set_property(TARGET rpc PROPERTY CXX_STANDARD 20) add_subdirectory("vendor") -target_link_libraries(rpc PUBLIC WindowsApp discord-rpc) +target_link_libraries(rpc PUBLIC WindowsApp discord-rpc libcurl_static mbedcrypto mbedx509 mbedtls) target_include_directories(rpc PRIVATE vendor) \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 92b2b90..a5bdb34 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -54,34 +54,41 @@ int main() { std::string currentMediaSource = mediaInformation->playbackSource; std::cout << currentMediaSource << std::endl; - if (currentMediaSource != lastMediaSource) - Discord_Shutdown(); //reinitialize with new client id - - std::string serviceName = utils::getAppName(lastMediaSource); - DiscordRichPresence activity{}; + if (currentMediaSource != lastMediaSource) + Discord_Shutdown(); // reinitialize with new client id + + std::string serviceName = utils::getAppName(lastMediaSource); + DiscordRichPresence activity{}; activity.type = ActivityType::LISTENING; - activity.details = mediaInformation->songTitle.c_str(); - activity.state = std::string("by " + mediaInformation->songArtist).c_str(); - activity.smallImageText = serviceName.c_str(); - activity.smallImageKey = "icon"; + activity.details = mediaInformation->songTitle.c_str(); + activity.state = std::string("by " + mediaInformation->songArtist).c_str(); + activity.smallImageText = serviceName.c_str(); + std::string artworkURL = utils::getArtworkURL(mediaInformation->songTitle + " " + mediaInformation->songArtist + + " " + mediaInformation->songAlbum); - activity.largeImageText = mediaInformation->songAlbum.c_str(); - activity.largeImageKey = ""; + activity.smallImageKey = "icon"; + if (artworkURL == "") { + activity.smallImageKey = ""; + activity.largeImageText = "icon"; + } else { + activity.largeImageText = mediaInformation->songAlbum.c_str(); + activity.largeImageKey = artworkURL.c_str(); + } - if(mediaInformation->songDuration != 0) { - int64_t remainingTime = mediaInformation->songDuration - mediaInformation->songElapsedTime; - activity.startTimestamp = time(nullptr) - mediaInformation->songElapsedTime; - activity.endTimestamp = time(nullptr) + remainingTime; - } - std::string endpointURL = utils::getSearchEndpoint(lastMediaSource); + if (mediaInformation->songDuration != 0) { + int64_t remainingTime = mediaInformation->songDuration - mediaInformation->songElapsedTime; + activity.startTimestamp = time(nullptr) - mediaInformation->songElapsedTime; + activity.endTimestamp = time(nullptr) + remainingTime; + } + std::string endpointURL = utils::getSearchEndpoint(lastMediaSource); - if(endpointURL != "") { - activity.button1name = std::string("Search on " + serviceName).c_str(); - std::string searchQuery = mediaInformation->songTitle + " " + mediaInformation->songArtist; - activity.button1link = std::string(endpointURL + utils::urlEncode(searchQuery)).c_str(); - } + if (endpointURL != "") { + activity.button1name = std::string("Search on " + serviceName).c_str(); + std::string searchQuery = mediaInformation->songTitle + " " + mediaInformation->songArtist; + activity.button1link = std::string(endpointURL + utils::urlEncode(searchQuery)).c_str(); + } lastMediaSource = currentMediaSource; - Discord_UpdatePresence(&activity); + Discord_UpdatePresence(&activity); } } \ No newline at end of file diff --git a/src/utils.hpp b/src/utils.hpp index 5c532c8..472fd60 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -1,5 +1,7 @@ #ifndef _UTILS_ #define _UTILS_ +#include + #include #include #include @@ -11,6 +13,62 @@ #define CONFIG_FILENAME "known.json" namespace utils { + inline std::string urlEncode(std::string str) { + std::string new_str = ""; + char c; + int ic; + const char* chars = str.c_str(); + char bufHex[10]; + int len = strlen(chars); + + for (int i = 0; i < len; i++) { + c = chars[i]; + ic = c; + if (c == ' ') + new_str += '+'; + else if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') + new_str += c; + else { + sprintf_s(bufHex, sizeof(bufHex), "%X", c); + if (ic < 16) + new_str += "%0"; + else + new_str += "%"; + new_str += bufHex; + } + } + return new_str; + } + inline size_t curlWriteCallback(char* contents, size_t size, size_t nmemb, void* userp) { + ((std::string*)userp)->append((char*)contents, size * nmemb); + return size * nmemb; + } + inline std::string getRequest(std::string url) { + CURL* curl; + CURLcode res; + std::string buf; + curl_global_init(CURL_GLOBAL_ALL); + curl = curl_easy_init(); + if (curl) { + curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteCallback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buf); + res = curl_easy_perform(curl); + curl_easy_cleanup(curl); + } + curl_global_cleanup(); + return buf; + } + inline std::string getArtworkURL(std::string query) { + std::string response = + getRequest("https://itunes.apple.com/search?media=music&entity=song&term=" + urlEncode(query)); + nlohmann::json j = nlohmann::json::parse(response); + auto results = j["results"]; + if (results.size() > 0) { + return results[0]["artworkUrl100"].get(); + } + return ""; + } inline nlohmann::json getApp(std::string processName) { std::ifstream i("known.json"); std::stringstream s; @@ -52,33 +110,6 @@ namespace utils { auto app = getApp(processName); return app["search_endpoint"] == "" ? "" : app["search_endpoint"]; } - - inline std::string urlEncode(std::string str) { - std::string new_str = ""; - char c; - int ic; - const char* chars = str.c_str(); - char bufHex[10]; - int len = strlen(chars); - - for (int i = 0; i < len; i++) { - c = chars[i]; - ic = c; - if (c == ' ') - new_str += '+'; - else if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') - new_str += c; - else { - sprintf(bufHex, "%X", c); - if (ic < 16) - new_str += "%0"; - else - new_str += "%"; - new_str += bufHex; - } - } - return new_str; - } } // namespace utils #undef DEFAULT_APP_NAME diff --git a/vendor/CMakeLists.txt b/vendor/CMakeLists.txt index 122ae17..81b4bdc 100644 --- a/vendor/CMakeLists.txt +++ b/vendor/CMakeLists.txt @@ -3,6 +3,7 @@ SET(ENABLE_PROGRAMS OFF) SET(ENABLE_TESTING OFF) add_subdirectory("mbedtls") SET(CURL_USE_MBEDTLS ON) +SET(CURL_USE_LIBPSL OFF) SET(BUILD_STATIC_LIBS ON) SET(BUILD_SHARED_LIBS OFF) SET(BUILD_CURL_EXE OFF) diff --git a/vendor/discord-rpc b/vendor/discord-rpc deleted file mode 160000 index d6b2214..0000000 --- a/vendor/discord-rpc +++ /dev/null @@ -1 +0,0 @@ -Subproject commit d6b2214e96fdb7cad468cbe7fc100c0b80522d43 diff --git a/vendor/mbedtls b/vendor/mbedtls index cd0fb1d..107ea89 160000 --- a/vendor/mbedtls +++ b/vendor/mbedtls @@ -1 +1 @@ -Subproject commit cd0fb1d17852077297816e30823b8125ba073357 +Subproject commit 107ea89daaefb9867ea9121002fbbdf926780e98