1
0

Compare commits

...

3 Commits
1.0 ... master

Author SHA1 Message Date
28752c83f3 Update README.md 2024-02-22 17:28:01 +01:00
4ce20fff51 Added Config File
Added Ignore Webpages (firefox)
Added Ignore Spotify
2024-02-22 17:25:13 +01:00
7733308120
Update README.md 2024-02-22 08:35:57 +01:00
5 changed files with 115 additions and 35 deletions

27
DiscordMediaRP/Config.cs Normal file
View File

@ -0,0 +1,27 @@
using Microsoft.Extensions.Logging;
namespace DiscordMediaRP;
public struct Config
{
public required string DiscordKey;
public LogLevel? LogLevel;
public string? LargeImageKey;
public bool? UseSpotify;
public string[]? WebbrowserIgnoreSites;
public Config WithDiscordKey(string key)
{
return this with { DiscordKey = key };
}
public Config WithLargeImageKey(string key)
{
return this with { LargeImageKey = key };
}
public Config WithLogLevel(LogLevel level)
{
return this with { LogLevel = level };
}
}

View File

@ -1,4 +1,5 @@
using System.Reflection;
using System.Diagnostics;
using System.Reflection;
using Windows.Media;
using Windows.Media.Control;
using DiscordRPC;
@ -20,8 +21,9 @@ public class DisMediaRP : IDisposable
private readonly DiscordRpcClient _discordRpcClient;
private RichPresence _currentStatus;
private bool _running = true;
private Config config;
private static RichPresence DefaultPresence(string largeImageKey)
private static RichPresence DefaultPresence(string? largeImageKey)
{
return new RichPresence()
{
@ -29,7 +31,7 @@ public class DisMediaRP : IDisposable
State = "https://github.com/C9Glax/DiscordMediaRichPresence",
Assets = new()
{
LargeImageKey = largeImageKey,
LargeImageKey = largeImageKey ?? "",
SmallImageKey = "music",
LargeImageText = "C9Glax/DiscordMediaRichPresence",
SmallImageText = "https://www.flaticon.com/de/autoren/alfanz"
@ -37,15 +39,12 @@ public class DisMediaRP : IDisposable
};
}
public DisMediaRP(string applicationId, LogLevel? logLevel, string largeImageKey = "cat") : this(applicationId, new Logger(logLevel ?? LogLevel.Information), largeImageKey)
public DisMediaRP(Config config)
{
}
public DisMediaRP(string applicationId, ILogger? logger = null, string largeImageKey = "cat")
{
this._logger = logger;
this._currentStatus = DefaultPresence(largeImageKey);
this._discordRpcClient = new DiscordRpcClient(applicationId, logger: new DisLogger(this._logger));
this.config = config;
this._logger = new Logger(config.LogLevel ?? LogLevel.Information);
this._currentStatus = DefaultPresence(config.LargeImageKey);
this._discordRpcClient = new DiscordRpcClient(config.DiscordKey, logger: new DisLogger(this._logger));
this._discordRpcClient.Initialize();
this._discordRpcClient.OnError += (sender, args) =>
{
@ -61,7 +60,7 @@ public class DisMediaRP : IDisposable
{
if (mediaSession is null)
{
this._currentStatus = DefaultPresence(largeImageKey);
this._currentStatus = DefaultPresence(config.LargeImageKey);
}
this._discordRpcClient.SetPresence(this._currentStatus);
@ -93,6 +92,9 @@ public class DisMediaRP : IDisposable
this._logger?.LogDebug(ObjectToString(mediaSession));
this._logger?.LogDebug(ObjectToString(mediaProperties));
if (!UseMediaSession(mediaSession))
return;
string details = $"{mediaProperties.Title}";
if (mediaProperties.Artist.Length > 0)
details += $" - {mediaProperties.Artist}";
@ -108,6 +110,9 @@ public class DisMediaRP : IDisposable
this._logger?.LogDebug(ObjectToString(mediaSession));
this._logger?.LogDebug(ObjectToString(playbackInfo));
if (!UseMediaSession(mediaSession))
return;
string playbackState = playbackInfo.PlaybackStatus switch
{
GlobalSystemMediaTransportControlsSessionPlaybackStatus.Paused => "pause",
@ -136,6 +141,9 @@ public class DisMediaRP : IDisposable
this._logger?.LogDebug(ObjectToString(mediaSession));
this._logger?.LogDebug(ObjectToString(timelineProperties));
if (!UseMediaSession(mediaSession))
return;
if (timelineProperties.LastUpdatedTime < DateTimeOffset.UnixEpoch)
return;
@ -165,6 +173,27 @@ public class DisMediaRP : IDisposable
this._discordRpcClient.SetPresence(this._currentStatus);
}
private bool UseMediaSession(MediaManager.MediaSession mediaSession)
{
string processId = mediaSession.ControlSession.SourceAppUserModelId;
if (processId == "spotify.exe")
return config.UseSpotify ?? true;
if (processId == "firefox.exe")
{
string[] windowNames = Process.GetProcesses().Where(proc => processId.Contains(proc.ProcessName, StringComparison.InvariantCultureIgnoreCase)).Select(proc => proc.MainWindowTitle).ToArray();
return !windowNames.Any(name =>
{
foreach (string site in config.WebbrowserIgnoreSites ?? Array.Empty<string>())
if (name.Contains(site, StringComparison.InvariantCultureIgnoreCase))
return true;
return false;
});
}
return true;
}
public void Dispose()
{
_mediaManager.Dispose();

View File

@ -11,6 +11,7 @@
<PackageReference Include="DiscordRichPresence" Version="1.2.1.24" />
<PackageReference Include="Dubya.WindowsMediaController" Version="2.5.3" />
<PackageReference Include="GlaxLogger" Version="1.0.6" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>

View File

@ -2,31 +2,40 @@
using DiscordMediaRP;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
Config? c = null;
if (File.Exists("config.json"))
c = JsonConvert.DeserializeObject<Config>(File.ReadAllText("config.json"));
int discordKeyIndex = Array.IndexOf(args, "-d");
if (discordKeyIndex > -1)
if (discordKeyIndex + 1 < args.Length)
if (c is null)
c = new Config()
{
DiscordKey = args[discordKeyIndex + 1]
};
else
c = c.Value.WithDiscordKey(args[discordKeyIndex + 1]);
else
throw new IndexOutOfRangeException("No Discord ApplicationKey provided");
else if(c is null)
throw new ArgumentNullException(nameof(Config.DiscordKey));
int loglevelIndex = Array.IndexOf(args, "-l");
LogLevel? level = null;
if(loglevelIndex > -1)
if(loglevelIndex + 1 < args.Length)
level = loglevelIndex < args.Length ? Enum.Parse<LogLevel>(args[loglevelIndex + 1]) : null;
if (loglevelIndex + 1 < args.Length)
c = c.Value.WithLogLevel(Enum.Parse<LogLevel>(args[loglevelIndex + 1]));
else
throw new IndexOutOfRangeException(nameof(loglevelIndex));
int discordKeyIndex = Array.IndexOf(args, "-d");
string discordKey;
if (discordKeyIndex > -1)
if(discordKeyIndex + 1 < args.Length)
discordKey = args[discordKeyIndex + 1];
else
throw new IndexOutOfRangeException("No Discord ApplicationKey provided");
else
throw new ArgumentNullException(nameof(discordKey));
int imageKeyIndex = Array.IndexOf(args, "-i");
string imageKey = "cat";
if(imageKeyIndex > -1)
if (imageKeyIndex + 1 < args.Length)
imageKey = args[imageKeyIndex + 1];
c = c.Value.WithLargeImageKey(args[imageKeyIndex + 1]);
else
throw new IndexOutOfRangeException(nameof(imageKeyIndex));
DisMediaRP _ = new (discordKey, level, imageKey);
DisMediaRP _ = new (c.Value);

View File

@ -1,11 +1,25 @@
# DiscordMediaRichPresence
## Usage
`DiscordMediaRP.exe -d <Your AppliocationKey> [-l Debug]`
`DiscordMediaRP.exe [-d <Your ApplicationKey>] [-l Debug]`
Your Application should have 5 Assets:
- cat
- play
- pause
- stop
- music
If you omit the ApplicationKey as argument, you need to use a `config.json`:
```json
{
"DiscordKey": "<Your ApplicationKey>",
"UseSpotify": false,
"WebbrowserIgnoreSites": [
"Youtube",
"Reddit"
]
}
```
Your Discord-Application should have 5 Assets:
* cat
* play
* pause
* stop
* music
![image](https://github.com/C9Glax/DiscordMediaRichPresence/assets/13404778/f28c1c4a-8297-4d99-a0ec-0538ffdd427b)