Initial releaase
This commit is contained in:
parent
ce43ef1245
commit
a95b1736dd
18
known.json
18
known.json
|
@ -6,7 +6,23 @@
|
||||||
"AppleMusic.exe"
|
"AppleMusic.exe"
|
||||||
],
|
],
|
||||||
"search_endpoint": "https://music.apple.com/search?term=",
|
"search_endpoint": "https://music.apple.com/search?term=",
|
||||||
"client_id": "1301881165984301106"
|
"client_id": "1245257240890310686"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Spotify",
|
||||||
|
"process_names": [
|
||||||
|
"Spotify.exe"
|
||||||
|
],
|
||||||
|
"search_endpoint": "https://open.spotify.com/search/",
|
||||||
|
"client_id": "1245257414715113573"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Tidal",
|
||||||
|
"process_names": [
|
||||||
|
"TIDAL.exe"
|
||||||
|
],
|
||||||
|
"search_endpoint": "https://listen.tidal.com/search?q=",
|
||||||
|
"client_id": "1245257493966225488"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
|
@ -6,6 +6,7 @@
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
#include "../backend.hpp"
|
#include "../backend.hpp"
|
||||||
|
#include "../utils.hpp"
|
||||||
|
|
||||||
using namespace winrt;
|
using namespace winrt;
|
||||||
using namespace Windows::Media::Control;
|
using namespace Windows::Media::Control;
|
||||||
|
@ -49,13 +50,21 @@ std::shared_ptr<MediaInfo> backend::getMediaInformation() {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string artist = toStdString(mediaProperties.Artist());
|
std::string artist = toStdString(mediaProperties.Artist());
|
||||||
|
std::string albumName = toStdString(mediaProperties.AlbumTitle());
|
||||||
if (artist == "")
|
if (artist == "")
|
||||||
artist = toStdString(mediaProperties.AlbumArtist()); // Needed for some apps
|
artist = toStdString(mediaProperties.AlbumArtist()); // Needed for some apps
|
||||||
|
|
||||||
|
if (artist.find("\x14") != std::string::npos) {
|
||||||
|
albumName = artist.substr(artist.find("\x14") + 1);
|
||||||
|
artist = artist.substr(0, artist.find("\x14"));
|
||||||
|
utils::trim(artist);
|
||||||
|
utils::trim(albumName);
|
||||||
|
}
|
||||||
|
|
||||||
return std::make_shared<MediaInfo>(
|
return std::make_shared<MediaInfo>(
|
||||||
playbackInfo.PlaybackStatus() == GlobalSystemMediaTransportControlsSessionPlaybackStatus::Paused,
|
playbackInfo.PlaybackStatus() == GlobalSystemMediaTransportControlsSessionPlaybackStatus::Paused,
|
||||||
toStdString(mediaProperties.Title()), artist, toStdString(mediaProperties.AlbumTitle()),
|
toStdString(mediaProperties.Title()), artist, albumName, toStdString(currentSession.SourceAppUserModelId()),
|
||||||
toStdString(currentSession.SourceAppUserModelId()), thumbnailData, endTime, elapsedTime);
|
thumbnailData, endTime, elapsedTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
36
src/main.cpp
36
src/main.cpp
|
@ -44,8 +44,8 @@ int main() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string currentlyPlayingSong =
|
std::string currentlyPlayingSong = mediaInformation->songTitle + mediaInformation->songArtist +
|
||||||
mediaInformation->songTitle + mediaInformation->songArtist + mediaInformation->songAlbum;
|
mediaInformation->songAlbum + std::to_string(mediaInformation->songDuration);
|
||||||
|
|
||||||
if (currentlyPlayingSong == lastPlayingSong)
|
if (currentlyPlayingSong == lastPlayingSong)
|
||||||
continue;
|
continue;
|
||||||
|
@ -53,42 +53,48 @@ int main() {
|
||||||
lastPlayingSong = currentlyPlayingSong;
|
lastPlayingSong = currentlyPlayingSong;
|
||||||
|
|
||||||
std::string currentMediaSource = mediaInformation->playbackSource;
|
std::string currentMediaSource = mediaInformation->playbackSource;
|
||||||
std::cout << currentMediaSource << std::endl;
|
|
||||||
if (currentMediaSource != lastMediaSource)
|
if (currentMediaSource != lastMediaSource) {
|
||||||
Discord_Shutdown(); // reinitialize with new client id
|
lastMediaSource = currentMediaSource;
|
||||||
|
Discord_Shutdown();
|
||||||
|
} // reinitialize with new client id
|
||||||
|
|
||||||
std::string serviceName = utils::getAppName(lastMediaSource);
|
std::string serviceName = utils::getAppName(lastMediaSource);
|
||||||
|
|
||||||
|
std::string activityState = "by " + mediaInformation->songArtist;
|
||||||
DiscordRichPresence activity{};
|
DiscordRichPresence activity{};
|
||||||
activity.type = ActivityType::LISTENING;
|
activity.type = ActivityType::LISTENING;
|
||||||
activity.details = mediaInformation->songTitle.c_str();
|
activity.details = mediaInformation->songTitle.c_str();
|
||||||
activity.state = std::string("by " + mediaInformation->songArtist).c_str();
|
activity.state = activityState.c_str();
|
||||||
activity.smallImageText = serviceName.c_str();
|
activity.smallImageText = serviceName.c_str();
|
||||||
std::string artworkURL = utils::getArtworkURL(mediaInformation->songTitle + " " + mediaInformation->songArtist +
|
std::string artworkURL = utils::getArtworkURL(mediaInformation->songTitle + " " + mediaInformation->songArtist +
|
||||||
" " + mediaInformation->songAlbum);
|
" " + mediaInformation->songAlbum);
|
||||||
|
|
||||||
activity.smallImageKey = "icon";
|
activity.smallImageKey = "appicon";
|
||||||
if (artworkURL == "") {
|
if (artworkURL == "") {
|
||||||
activity.smallImageKey = "";
|
activity.smallImageKey = "";
|
||||||
activity.largeImageText = "icon";
|
activity.largeImageKey = "appicon";
|
||||||
} else {
|
} else {
|
||||||
activity.largeImageText = mediaInformation->songAlbum.c_str();
|
|
||||||
activity.largeImageKey = artworkURL.c_str();
|
activity.largeImageKey = artworkURL.c_str();
|
||||||
}
|
}
|
||||||
|
activity.largeImageText = mediaInformation->songAlbum.c_str();
|
||||||
|
|
||||||
if (mediaInformation->songDuration != 0) {
|
if (mediaInformation->songDuration != 0) {
|
||||||
int64_t remainingTime = mediaInformation->songDuration - mediaInformation->songElapsedTime;
|
int64_t remainingTime = mediaInformation->songDuration - mediaInformation->songElapsedTime;
|
||||||
activity.startTimestamp = time(nullptr) - mediaInformation->songElapsedTime;
|
activity.startTimestamp = time(nullptr) - (mediaInformation->songElapsedTime / 1000);
|
||||||
activity.endTimestamp = time(nullptr) + remainingTime;
|
activity.endTimestamp = time(nullptr) + (remainingTime / 1000);
|
||||||
}
|
}
|
||||||
std::string endpointURL = utils::getSearchEndpoint(lastMediaSource);
|
std::string endpointURL = utils::getSearchEndpoint(lastMediaSource);
|
||||||
|
|
||||||
if (endpointURL != "") {
|
|
||||||
activity.button1name = std::string("Search on " + serviceName).c_str();
|
|
||||||
std::string searchQuery = mediaInformation->songTitle + " " + mediaInformation->songArtist;
|
std::string searchQuery = mediaInformation->songTitle + " " + mediaInformation->songArtist;
|
||||||
activity.button1link = std::string(endpointURL + utils::urlEncode(searchQuery)).c_str();
|
std::string buttonName = "Search on " + serviceName;
|
||||||
|
std::string buttonText = endpointURL + utils::urlEncode(searchQuery);
|
||||||
|
|
||||||
|
if (endpointURL != "") {
|
||||||
|
activity.button1name = buttonName.c_str();
|
||||||
|
activity.button1link = buttonText.c_str();
|
||||||
}
|
}
|
||||||
|
|
||||||
lastMediaSource = currentMediaSource;
|
|
||||||
Discord_UpdatePresence(&activity);
|
Discord_UpdatePresence(&activity);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -13,6 +13,20 @@
|
||||||
#define CONFIG_FILENAME "known.json"
|
#define CONFIG_FILENAME "known.json"
|
||||||
|
|
||||||
namespace utils {
|
namespace utils {
|
||||||
|
inline std::string ltrim(std::string& s) {
|
||||||
|
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { return !std::isspace(ch); }));
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::string rtrim(std::string& s) {
|
||||||
|
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { return !std::isspace(ch); }).base(), s.end());
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
inline std::string trim(std::string& s) {
|
||||||
|
ltrim(s);
|
||||||
|
rtrim(s);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
inline std::string urlEncode(std::string str) {
|
inline std::string urlEncode(std::string str) {
|
||||||
std::string new_str = "";
|
std::string new_str = "";
|
||||||
char c;
|
char c;
|
||||||
|
@ -39,10 +53,12 @@ namespace utils {
|
||||||
}
|
}
|
||||||
return new_str;
|
return new_str;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline size_t curlWriteCallback(char* contents, size_t size, size_t nmemb, void* userp) {
|
inline size_t curlWriteCallback(char* contents, size_t size, size_t nmemb, void* userp) {
|
||||||
((std::string*)userp)->append((char*)contents, size * nmemb);
|
((std::string*)userp)->append((char*)contents, size * nmemb);
|
||||||
return size * nmemb;
|
return size * nmemb;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::string getRequest(std::string url) {
|
inline std::string getRequest(std::string url) {
|
||||||
CURL* curl;
|
CURL* curl;
|
||||||
CURLcode res;
|
CURLcode res;
|
||||||
|
@ -52,6 +68,7 @@ namespace utils {
|
||||||
if (curl) {
|
if (curl) {
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteCallback);
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteCallback);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buf);
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buf);
|
||||||
res = curl_easy_perform(curl);
|
res = curl_easy_perform(curl);
|
||||||
curl_easy_cleanup(curl);
|
curl_easy_cleanup(curl);
|
||||||
|
@ -59,6 +76,7 @@ namespace utils {
|
||||||
curl_global_cleanup();
|
curl_global_cleanup();
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::string getArtworkURL(std::string query) {
|
inline std::string getArtworkURL(std::string query) {
|
||||||
std::string response =
|
std::string response =
|
||||||
getRequest("https://itunes.apple.com/search?media=music&entity=song&term=" + urlEncode(query));
|
getRequest("https://itunes.apple.com/search?media=music&entity=song&term=" + urlEncode(query));
|
||||||
|
@ -69,6 +87,7 @@ namespace utils {
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
inline nlohmann::json getApp(std::string processName) {
|
inline nlohmann::json getApp(std::string processName) {
|
||||||
std::ifstream i("known.json");
|
std::ifstream i("known.json");
|
||||||
std::stringstream s;
|
std::stringstream s;
|
||||||
|
@ -94,21 +113,21 @@ namespace utils {
|
||||||
if (!std::filesystem::exists(CONFIG_FILENAME))
|
if (!std::filesystem::exists(CONFIG_FILENAME))
|
||||||
return DEFAULT_CLIENT_ID;
|
return DEFAULT_CLIENT_ID;
|
||||||
auto app = getApp(processName);
|
auto app = getApp(processName);
|
||||||
return app["client_id"] == "" ? DEFAULT_CLIENT_ID : app["client_id"];
|
return app.contains("client_id") ? app["client_id"].get<std::string>() : DEFAULT_CLIENT_ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::string getAppName(std::string processName) {
|
inline std::string getAppName(std::string processName) {
|
||||||
if (!std::filesystem::exists(CONFIG_FILENAME))
|
if (!std::filesystem::exists(CONFIG_FILENAME))
|
||||||
return DEFAULT_APP_NAME;
|
return DEFAULT_APP_NAME;
|
||||||
auto app = getApp(processName);
|
auto app = getApp(processName);
|
||||||
return app["name"] == "" ? DEFAULT_APP_NAME : app["name"];
|
return app.contains("name") ? app["name"].get<std::string>() : DEFAULT_APP_NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::string getSearchEndpoint(std::string processName) {
|
inline std::string getSearchEndpoint(std::string processName) {
|
||||||
if (!std::filesystem::exists(CONFIG_FILENAME))
|
if (!std::filesystem::exists(CONFIG_FILENAME))
|
||||||
return "";
|
return "";
|
||||||
auto app = getApp(processName);
|
auto app = getApp(processName);
|
||||||
return app["search_endpoint"] == "" ? "" : app["search_endpoint"];
|
return app.contains("search_endpoint") ? app["search_endpoint"].get<std::string>() : "";
|
||||||
}
|
}
|
||||||
} // namespace utils
|
} // namespace utils
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
add_subdirectory("discord-rpc")
|
add_subdirectory("discord-rpc")
|
||||||
SET(ENABLE_PROGRAMS OFF)
|
SET(ENABLE_PROGRAMS OFF FORCE)
|
||||||
SET(ENABLE_TESTING OFF)
|
SET(ENABLE_TESTING OFF FORCE)
|
||||||
add_subdirectory("mbedtls")
|
add_subdirectory("mbedtls")
|
||||||
SET(CURL_USE_MBEDTLS ON)
|
SET(CURL_USE_MBEDTLS ON)
|
||||||
SET(CURL_USE_LIBPSL OFF)
|
SET(CURL_USE_LIBPSL OFF)
|
||||||
|
|
Loading…
Reference in New Issue