Skip to content

Commit

Permalink
add custom artwork url, requested by bri
Browse files Browse the repository at this point in the history
  • Loading branch information
Cynosphere committed Mar 27, 2022
1 parent 7411059 commit 1f00224
Showing 1 changed file with 65 additions and 35 deletions.
100 changes: 65 additions & 35 deletions mb_DiscordRichPresence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public PluginInfo Initialise(IntPtr apiInterfacePtr)
about.MinInterfaceVersion = MinInterfaceVersion;
about.MinApiRevision = MinApiRevision;
about.ReceiveNotifications = (ReceiveNotificationFlags.PlayerEvents | ReceiveNotificationFlags.TagEvents);
about.ConfigurationPanelHeight = 24; // height in pixels that musicbee should reserve in a panel for config settings. When set, a handle to an empty panel will be passed to the Configure function
about.ConfigurationPanelHeight = 48; // height in pixels that musicbee should reserve in a panel for config settings. When set, a handle to an empty panel will be passed to the Configure function

InitialiseDiscord();

Expand All @@ -73,28 +73,28 @@ private void HandleReadyCallback(ref DiscordRpc.DiscordUser user) { }
private void HandleErrorCallback(int errorCode, string message) { }
private void HandleDisconnectedCallback(int errorCode, string message) { }

private async Task FetchArtRequest(string key, string url)
{
HttpResponseMessage lastFmInfoResp = await Client.GetAsync(url);
if (lastFmInfoResp.IsSuccessStatusCode)
{
string jsonString = await lastFmInfoResp.Content.ReadAsStringAsync();
JObject jsonData = JsonConvert.DeserializeObject<JObject>(jsonString);
try
{
var images = jsonData["album"]["image"].Children<JObject>();
string albumArtUrl = (string)((JObject)images.Where(x => (string)x["size"] == "large").FirstOrDefault())["#text"];
albumArtCache.Add(key, albumArtUrl);
}
catch
{
albumArtCache.Add(key, "unknown");
}
}
else
{
albumArtCache.Add(key, "unknown");
}
private async Task FetchArtRequest(string key, string url)
{
HttpResponseMessage lastFmInfoResp = await Client.GetAsync(url);
if (lastFmInfoResp.IsSuccessStatusCode)
{
string jsonString = await lastFmInfoResp.Content.ReadAsStringAsync();
JObject jsonData = JsonConvert.DeserializeObject<JObject>(jsonString);
try
{
var images = jsonData["album"]["image"].Children<JObject>();
string albumArtUrl = (string)((JObject)images.Where(x => (string)x["size"] == "large").FirstOrDefault())["#text"];
albumArtCache.Add(key, albumArtUrl);
}
catch
{
albumArtCache.Add(key, "unknown");
}
}
else
{
albumArtCache.Add(key, "unknown");
}
}

private async Task FetchArt(string track, string artist, string albumArtist, string album)
Expand Down Expand Up @@ -123,7 +123,7 @@ private async Task FetchArt(string track, string artist, string albumArtist, str
}
}

private async Task UpdatePresence(string artist, string track, string album, bool playing, int index, int totalTracks, string imageUrl, string yearStr)
private void UpdatePresence(string artist, string track, string album, bool playing, int index, int totalTracks, string imageUrl, string yearStr)
{
presence.largeImageKey = "albumart";

Expand Down Expand Up @@ -154,7 +154,7 @@ private async Task UpdatePresence(string artist, string track, string album, boo
presence.largeImageText = album;
}

if (imageUrl != "" && imageUrl != "unknown")
if (imageUrl != "" && imageUrl != "unknown")
presence.largeImageKey = imageUrl;

string bitrate = mbApiInterface.NowPlaying_GetFileProperty(FilePropertyType.Bitrate);
Expand Down Expand Up @@ -187,8 +187,6 @@ private async Task UpdatePresence(string artist, string track, string album, boo
long duration = this.mbApiInterface.NowPlaying_GetDuration() / 1000;
long end = now + duration;

TimeSpan t = DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1));

if (playing)
{
long pos = (this.mbApiInterface.Player_GetPosition() / 1000);
Expand Down Expand Up @@ -221,14 +219,30 @@ public bool Configure(IntPtr panelHandle)
Panel configPanel = (Panel) Panel.FromHandle(panelHandle);

CheckBox showYear = new CheckBox();
showYear.Name = "ShowYear";
showYear.Text = "Show year next to album";
showYear.Height = 16;
showYear.ForeColor = Color.FromArgb(mbApiInterface.Setting_GetSkinElementColour(SkinElement.SkinInputPanelLabel, ElementState.ElementStateDefault, ElementComponent.ComponentForeground));
showYear.Checked = newConfig.showYear;
showYear.CheckedChanged += ShowYearValueChanged;

configPanel.Controls.AddRange(new Control[] { showYear });
Label customArtworkUrlLabel = new Label();
customArtworkUrlLabel.Height = 16;
customArtworkUrlLabel.Width = 128;
customArtworkUrlLabel.ForeColor = Color.FromArgb(mbApiInterface.Setting_GetSkinElementColour(SkinElement.SkinInputPanelLabel, ElementState.ElementStateDefault, ElementComponent.ComponentForeground));
customArtworkUrlLabel.Text = "Custom Artwork URL";
customArtworkUrlLabel.Top = 24;
customArtworkUrlLabel.TextAlign = ContentAlignment.MiddleLeft;

TextBox customArtworkUrl = (TextBox) mbApiInterface.MB_AddPanel(configPanel, PluginPanelDock.TextBox);
customArtworkUrl.Height = 16;
customArtworkUrl.Width = 192;
customArtworkUrl.ForeColor = Color.FromArgb(mbApiInterface.Setting_GetSkinElementColour(SkinElement.SkinInputPanelLabel, ElementState.ElementStateDefault, ElementComponent.ComponentForeground));
customArtworkUrl.Text = newConfig.customArtworkUrl;
customArtworkUrl.TextChanged += CustomArtworkUrlValueChanged;
customArtworkUrl.Top = 24;
customArtworkUrl.Left = customArtworkUrlLabel.Width;

configPanel.Controls.AddRange(new Control[] { showYear, customArtworkUrlLabel, customArtworkUrl });
}
return false;
}
Expand All @@ -237,6 +251,11 @@ private void ShowYearValueChanged(object sender, EventArgs args) {
newConfig.showYear = (sender as CheckBox).Checked;
}

private void CustomArtworkUrlValueChanged(object sender, EventArgs args)
{
newConfig.customArtworkUrl = (sender as TextBox).Text;
}

// called by MusicBee when the user clicks Apply or Save in the MusicBee Preferences screen.
// its up to you to figure out whether anything has changed and needs updating
public void SaveSettings()
Expand Down Expand Up @@ -332,16 +351,25 @@ public void ReceiveNotification(string sourceFileUrl, NotificationType type)
{
try
{
await FetchArt(trackTitle, originalArtist, albumArtist, album);
string imageUrl = "";
if (config.customArtworkUrl != "")
{
imageUrl = config.customArtworkUrl + "?" + (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
}
else
{
await FetchArt(trackTitle, originalArtist, albumArtist, album);
string imageUrl = albumArtCache[$"{albumArtist}_{album}"];
if (imageUrl == "" && imageUrl == "unknown") {
imageUrl = albumArtCache[$"{originalArtist}_{album}"];
imageUrl = albumArtCache[$"{albumArtist}_{album}"];
if (imageUrl == "" && imageUrl == "unknown")
imageUrl = albumArtCache[$"{originalArtist}_{trackTitle}"];
{
imageUrl = albumArtCache[$"{originalArtist}_{album}"];
if (imageUrl == "" && imageUrl == "unknown")
imageUrl = albumArtCache[$"{originalArtist}_{trackTitle}"];
}
}
await UpdatePresence(artist, trackTitle, album, isPlaying, index + 1, tracks.Length, imageUrl, year);
UpdatePresence(artist, trackTitle, album, isPlaying, index + 1, tracks.Length, imageUrl, year);
}
catch (Exception err)
{
Expand All @@ -357,9 +385,11 @@ public class Configuration : ICloneable
public Configuration()
{
this.showYear = true;
this.customArtworkUrl = "";
}

public bool showYear { get; set; }
public string customArtworkUrl { get; set; }

public object Clone()
{
Expand Down

0 comments on commit 1f00224

Please sign in to comment.