Skip to content

Commit

Permalink
Buttons and Album Artist fallback (#86)
Browse files Browse the repository at this point in the history
* Adds fallback for the Album Artist field if the "Artist" field isn't filled

* Merge button changes

Also adds tidal and foobar2000 support

Co-Authored-By: jojo2357 <[email protected]>

* Fixes non-urlencoded strings from breaking lastfm

Also adds buttons to the configuration file

* Adds buttons to local, and groundwork for MM5

* Update DiscordPresenceConfig.ini

Adds amazon and MM5 discord applications

* Enables foobar2000 support.

Do note that it doesn't have full support. Album title is not transmitted, resulting in lack of album art
Closes #69

* Update README.MD

And unimportant changes

* Spacing issue

---------

Co-authored-by: jojo2357 <[email protected]>
  • Loading branch information
Smaltin and jojo2357 authored Nov 12, 2023
1 parent 02bebe8 commit 3897226
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 49 deletions.
18 changes: 18 additions & 0 deletions DiscordPresenceConfig.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ microsoft.media.player=true=807774172574253056
wmplayer=true=807774172574253056
wavelink=true=927328178618376212
tidal=true=922625678271197215
amazon music=true=808571949227049000
mediamonkeyengine=true=1096929771511873587
foobar2000=true=1009193842211299428

verbose=false
debug missing player=false
#spotify-style-rp:
Expand Down Expand Up @@ -38,3 +42,17 @@ large music.ui asset=https://cdn.discordapp.com/app-assets/801209905020272681/80
# Windows Media Player
small microsoft.media.player asset=https://cdn.discordapp.com/app-assets/801209905020272681/801224923547959376.png
large microsoft.media.player asset=https://cdn.discordapp.com/app-assets/801209905020272681/801224923547959376.png

# Buttons

# view artist on Last.fm
//button text=View ${artist} on Last.fm
//button url=https://last.fm/music/${artist}

# view track on Last.fm
//button text=View Last.fm info
//button url=https://www.last.fm/music/${artist}/_/${title}

# rickroll your friends
//button text=Mystery Button!
//button url=https://www.youtube.com/watch?v=dQw4w9WgXcQ
182 changes: 143 additions & 39 deletions MDRP/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
using CSCore.CoreAudioAPI;
using DiscordRPC;
using DiscordRPC.Message;
using IWshRuntimeLibrary;
using Microsoft.Toolkit.Uwp.Notifications;
using Newtonsoft.Json.Linq;
using File = System.IO.File;

Expand All @@ -23,7 +21,7 @@ namespace MDRP
internal partial class Program
{
public static LangHelper langHelper = new LangHelper();
public const string Version = "1.7.2";
public const string Version = "1.7.3";
public const string Github = "https://github.com/jojo2357/Music-Discord-Rich-Presence";
public static readonly string Title = langHelper.get(LocalizableStrings.MDRP_FULL);
private const int titleLength = 64;
Expand All @@ -45,8 +43,11 @@ internal partial class Program
{ "musicbee", new DiscordRpcClient("820837854385012766", autoEvents: false) },
{ "spotify", new DiscordRpcClient("802222525110812725", autoEvents: false) },
{ "tidal", new DiscordRpcClient("922625678271197215", autoEvents: false) },
{ "tidalplayer", new DiscordRpcClient("922625678271197215", autoEvents: false) },
{ "wavelink", new DiscordRpcClient("927328178618376212", autoEvents: false) },
{ "amazon music", new DiscordRpcClient("807774172574253056", autoEvents: false) },
{ "mediamonkeyengine", new DiscordRpcClient("1096929771511873587", autoEvents: false) },
{ "foobar2000", new DiscordRpcClient("1009193842211299428", autoEvents: false) },
{ "", new DiscordRpcClient("821398156905283585", autoEvents: false) }
};

Expand Down Expand Up @@ -81,15 +82,18 @@ internal partial class Program
{ "spotify", ConsoleColor.DarkGreen },
{ "musicbee", ConsoleColor.Yellow },
{ "tidal", ConsoleColor.Gray },
{ "tidalplayer", ConsoleColor.Gray },
{ "wavelink", ConsoleColor.DarkBlue },
{ "amazon music", ConsoleColor.Gray },
{ "foobar2000", ConsoleColor.DarkCyan},
{"mediamonkeyengine", ConsoleColor.Yellow}
};

private static string _presenceDetails = string.Empty;

private static readonly string[] ValidPlayers =
{
"music.ui", "spotify", "musicbee", "microsoft.media.player", "wmplayer", "tidal", "wavelink", "amazon music"
"music.ui", "spotify", "musicbee", "microsoft.media.player", "wmplayer", "tidal", "tidalplayer", "wavelink", "amazon music", "foobar2000", "mediamonkeyengine"
};

private static readonly string[] RequiresPipeline = { "musicbee" };
Expand All @@ -104,8 +108,11 @@ internal partial class Program
{ "wmplayer", "Windows Media Player" },
{ "music.ui", "Groove Music Player" },
{ "tidal", "Tidal Music" },
{ "tidalplayer", "Tidal Music" },
{ "wavelink", "Wave Link" },
{ "amazon music", "Amazon Music" }
{ "amazon music", "Amazon Music" },
{ "foobar2000", "FooBar2000"},
{ "mediamonkeyengine", "MediaMonkey5"}
};

private static readonly Dictionary<string, string> BigAssets = new Dictionary<string, string>
Expand All @@ -119,7 +126,10 @@ internal partial class Program
},
{ "spotify", "https://cdn.discordapp.com/app-assets/802222525110812725/802222954821582869.png" },
{ "tidal", "https://cdn.discordapp.com/app-assets/922625678271197215/978018192393920562.png" },
{ "tidalplayer", "https://cdn.discordapp.com/app-assets/922625678271197215/978018192393920562.png" },
{ "wavelink", "https://cdn.discordapp.com/app-assets/927328178618376212/927329727180574760.png" },
{ "foobar2000", "https://cdn.discordapp.com/app-assets/1009193842211299428/1009196080853950464.png" },
{"mediamonkeyengine", "https://cdn.discordapp.com/app-assets/1096929771511873587/1096930933325713519.png"}
};

//might just combine these later
Expand All @@ -134,7 +144,10 @@ internal partial class Program
},
{ "spotify", "https://cdn.discordapp.com/app-assets/802222525110812725/802222992683827200.png" },
{ "tidal", "https://cdn.discordapp.com/app-assets/922625678271197215/978018192637198406.png" },
{ "tidalplayer", "https://cdn.discordapp.com/app-assets/922625678271197215/978018192637198406.png" },
{ "wavelink", "https://cdn.discordapp.com/app-assets/927328178618376212/927329727218319410.png" },
{ "foobar2000", "https://cdn.discordapp.com/app-assets/1009193842211299428/1009196080950423563.png" },
{"mediamonkeyengine", "https://cdn.discordapp.com/app-assets/1096929771511873587/1096930933325713519.png"}
};

private static readonly Dictionary<string, string> Whatpeoplecallthisplayer = new Dictionary<string, string>
Expand All @@ -145,8 +158,11 @@ internal partial class Program
{ "wmplayer", "Windows Media Player" },
{ "spotify", "Spotify" },
{ "tidal", "Tidal Music" },
{ "tidalplayer", "Tidal Music" },
{ "wavelink", "Wave Link" },
{ "amazon music", "Amazon Music" }
{ "amazon music", "Amazon Music" },
{ "foobar2000", "FooBar2000"},
{"mediamonkeyengine", "MediaMonkey5"}
};

private static readonly Dictionary<string, string> InverseWhatpeoplecallthisplayer =
Expand All @@ -158,7 +174,9 @@ internal partial class Program
{ "spotify", "spotify" },
{ "Tidal Music", "tidal" },
{ "Wave Link", "wavelink" },
{ "Amazon Music", "anazon music" }
{ "Amazon Music", "anazon music" },
{ "FooBar2000", "foobar2000"},
{"MediaMonkey5", "mediamonkeyengine"}
};

private static readonly string defaultPlayer = "groove";
Expand Down Expand Up @@ -208,6 +226,8 @@ internal partial class Program

private static string lineData = "", currentTitle = "";
private static bool foundFirst = false, foundSecond = false;
private static string buttonText = "Click Me!", buttonURL = "https://archive.org/donate/";
private static bool foundButtonText = false, foundButtonURL = false;
private static bool foundImageRemotely = false;

private static readonly Regex _smallAssetRex =
Expand All @@ -216,6 +236,31 @@ internal partial class Program
private static readonly Regex _largeAssetReg =
new Regex("(?<=^large\\s?)\\b[\\w\\\\.]+\\b(?=\\s?asset)", RegexOptions.IgnoreCase);

private static String GetArtist(JsonResponse lastMessage)
{
if (lastMessage != null)
{
if (lastMessage.Artist != "" || lastMessage.AlbumArtist != "")
{
if (lastMessage.Artist != "")
{
return lastMessage.Artist;
}
return lastMessage.AlbumArtist;
}
}
else if (_currentTrack.Artist != "" || _currentTrack.AlbumArtist != "")
{
if (_currentTrack.Artist != "")
{
return _currentTrack.Artist;
}

return _currentTrack.AlbumArtist;
}
return langHelper[LocalizableStrings.UNKNOWN_ARTIST];
}

public static async Task HandleIncomingConnections()
{
// While a user hasn't visited the `shutdown` url, keep on handling requests
Expand Down Expand Up @@ -523,7 +568,7 @@ private static void HandleLocalRequests()
}
});
InvokeActiveClient();
SetConsole(_lastTrack.Title, _lastTrack.Artist, _lastTrack.AlbumTitle,
SetConsole(_lastTrack.Title, GetArtist(null), _lastTrack.AlbumTitle,
currentAlbum);
}
else if
Expand Down Expand Up @@ -552,16 +597,12 @@ private static void HandleLocalRequests()
string newDetailsWithTitle = Functions.CapLength(
lineData.Split('\n')[0]
.Replace("${artist}",
(_currentTrack.Artist == ""
? langHelper[LocalizableStrings.UNKNOWN_ARTIST]
: _currentTrack.Artist)).Replace("${title}", _currentTrack.Title)
(GetArtist(null))).Replace("${title}", _currentTrack.Title)
.Replace("${album}", _currentTrack.AlbumTitle), titleLength);
string newStateWithArtist = Functions.CapLength(
lineData.Split('\n')[1]
.Replace("${artist}",
(_currentTrack.Artist == ""
? langHelper[LocalizableStrings.UNKNOWN_ARTIST]
: _currentTrack.Artist)).Replace("${title}", _currentTrack.Title)
(GetArtist(null))).Replace("${title}", _currentTrack.Title)
.Replace("${album}", _currentTrack.AlbumTitle), artistLength);
if (activeClient.CurrentPresence == null ||
activeClient.CurrentPresence.Details != newDetailsWithTitle ||
Expand Down Expand Up @@ -601,7 +642,7 @@ private static void HandleLocalRequests()
}
}

activeClient.SetPresence(new RichPresence
RichPresence richPresence = new RichPresence
{
Details = newDetailsWithTitle,
State = newStateWithArtist,
Expand All @@ -612,8 +653,20 @@ private static void HandleLocalRequests()
SmallImageKey = GetSmallImageKey(),
SmallImageText = GetSmallImageText()
}
});
SetConsole(_currentTrack.Title, _currentTrack.Artist, _currentTrack.AlbumTitle,
};

if (foundButtonText || foundButtonURL)
{
richPresence.Buttons = new []{new Button
{
Label = PrepareFormatStringCappedLocal(GetArtist(null), _currentTrack.AlbumTitle, _currentTrack.Title, buttonText, 32),
Url = PrepareEscapedFormatStringLocal(GetArtist(null), _currentTrack.AlbumTitle, _currentTrack.Title, buttonURL)
}};
}

activeClient.SetPresence(richPresence);

SetConsole(_currentTrack.Title, GetArtist(null), _currentTrack.AlbumTitle,
currentAlbum);
InvokeActiveClient();
}
Expand Down Expand Up @@ -734,23 +787,10 @@ private static void HandleRemoteRequests()
currentTitle = lastMessage.Title;
WrongArtistFlag = HasNameNotQuite(new Album(lastMessage.Album.Name), _playerName);


activeClient.SetPresence(new RichPresence
RichPresence richPresence = new RichPresence
{
Details = Functions.CapLength(
lineData.Split('\n')[0]
.Replace("${artist}",
(lastMessage.Artist == ""
? langHelper[LocalizableStrings.UNKNOWN_ARTIST]
: lastMessage.Artist)).Replace("${title}", lastMessage.Title)
.Replace("${album}", lastMessage.Album.Name), titleLength),
State = Functions.CapLength(
lineData.Split('\n')[1]
.Replace("${artist}",
(lastMessage.Artist == ""
? langHelper[LocalizableStrings.UNKNOWN_ARTIST]
: lastMessage.Artist)).Replace("${title}", lastMessage.Title)
.Replace("${album}", lastMessage.Album.Name), artistLength),
Details = PrepareFormatStringCapped(lastMessage, lineData.Split('\n')[0], titleLength),
State = PrepareFormatStringCapped(lastMessage, lineData.Split('\n')[1], titleLength),

Timestamps = _isPlaying
? new Timestamps
Expand All @@ -765,8 +805,21 @@ private static void HandleRemoteRequests()
LargeImageText = Functions.GetLargeImageText(lastMessage.Album.Name),
SmallImageKey = GetSmallImageKey(),
SmallImageText = GetSmallImageText()
}
});
},
};

if (foundButtonText || foundButtonURL)
{
richPresence.Buttons = new []{new Button
{
Label = PrepareFormatStringCapped(lastMessage, buttonText, 32),
Url = PrepareEscapedFormatString(lastMessage, buttonURL)
}};
}

activeClient.SetPresence(richPresence);


InvokeActiveClient();

if (ScreamAtUser && !presenceIsRich && !NotifiedAlbums.Contains(currentAlbum) &&
Expand All @@ -793,12 +846,53 @@ private static void HandleRemoteRequests()
}
}

SetConsole(lastMessage.Title, lastMessage.Artist, lastMessage.Album.Name,
SetConsole(lastMessage.Title, GetArtist(lastMessage), lastMessage.Album.Name,
lastMessage.Album);
if (!_isPlaying) Timer.Restart();
}
}

private static string PrepareFormatStringCappedLocal(string artist, string album, string title, string instring,
int capLength)
{
return Functions.CapLength(
PrepareFormatStringLocal(artist, album, title, instring), capLength);
}

private static string PrepareFormatStringCapped(JsonResponse lastMessage, string instring, int capLength)
{
return Functions.CapLength(
PrepareFormatString(lastMessage, instring), capLength);
}

private static string PrepareFormatStringLocal(string artist, string album, string title, string instring)
{
return instring.Replace("${artist}",
(artist)).Replace("${title}", title)
.Replace("${album}", album);
}

private static string PrepareFormatString(JsonResponse lastMessage, string instring)
{
return instring.Replace("${artist}",
(GetArtist(lastMessage))).Replace("${title}", lastMessage.Title)
.Replace("${album}", lastMessage.Album.Name);
}

private static string PrepareEscapedFormatStringLocal(string artist, string album, string title, string instring)
{
return instring.Replace("${artist}",
WebUtility.UrlEncode(Uri.EscapeUriString(artist))).Replace("${title}", WebUtility.UrlEncode(Uri.EscapeUriString(title)))
.Replace("${album}", WebUtility.UrlEncode(Uri.EscapeUriString(album)));
}

private static string PrepareEscapedFormatString(JsonResponse lastMessage, string instring)
{
return instring.Replace("${artist}",
WebUtility.UrlEncode(Uri.EscapeUriString(GetArtist(lastMessage)))).Replace("${title}", WebUtility.UrlEncode(Uri.EscapeUriString(lastMessage.Title)))
.Replace("${album}", WebUtility.UrlEncode(Uri.EscapeUriString(lastMessage.Album.Name)));
}

private static string GetSmallImageText()
{
if (_isPlaying)
Expand Down Expand Up @@ -862,7 +956,7 @@ private static string GetLargeImageKey(Album activeAlbum, string activeTitle)
}
}

return BigAssets[_playerName];
return BigAssets.ContainsKey(_playerName) ? BigAssets[_playerName] : BigAssets[defaultPlayer];
}

private static void UnsetAllPresences()
Expand Down Expand Up @@ -1205,12 +1299,22 @@ private static void LoadSettings()
else if (!foundFirst && firstPortion == "first line")
{
foundFirst = true;
lineData = String.Join("=", explodedLine.Skip(1)) + lineData;
lineData = String.Join("=", explodedLine.Skip(1).ToArray()) + lineData;
}
else if (!foundSecond && firstPortion == "second line")
{
foundSecond = true;
lineData = lineData + "\n" + String.Join("=", explodedLine.Skip(1));
lineData = lineData + "\n" + String.Join("=", explodedLine.Skip(1).ToArray());
}
else if (!foundButtonText && firstPortion == "button text")
{
foundButtonText = true;
buttonText = String.Join("=", explodedLine.Skip(1).ToArray());
}
else if (!foundButtonURL && firstPortion == "button url")
{
foundButtonURL = true;
buttonURL = String.Join("=", explodedLine.Skip(1).ToArray());
}
else if (firstPortion == "get remote artwork")
{
Expand Down
Binary file modified MDRP/bin/Release/MDRP.exe
Binary file not shown.
2 changes: 1 addition & 1 deletion MDRP/util/ExternalAPI/ExternalArtManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private async Task<String> ExternalAlbumLookup(Album album, string backupTitle)
string text = "";
using (StreamReader reader = new StreamReader(result.GetResponseStream(), Encoding.UTF8))
{
text = reader.ReadToEnd();
text = await reader.ReadToEndAsync();
}
result.Close();

Expand Down
Loading

0 comments on commit 3897226

Please sign in to comment.