-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
133 lines (120 loc) · 4.3 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Linq;
using DiscordRPC;
namespace SpotifyRPC
{
static class Program
{
static event EventHandler<UpdateActivityEventArgs> UpdateStatus;
static event EventHandler ClearStatus;
static bool running;
static void UpdateActivity(this DiscordRpcClient discord, string songname, string artist)
{
try
{
discord.SetPresence(new RichPresence
{
Details = songname,
State = artist,
Timestamps = new Timestamps()
{
Start = DateTime.UtcNow
},
Assets = new Assets()
{
LargeImageKey = "spotify",
LargeImageText = "github.com/IDoEverything/SpotifyRPC"
}
});
Console.WriteLine("updated activity");
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
}
static void Main()
{
running = true;
Run();
Task.Run(async () =>
{
string prevtitle = null;
bool notPlaying = false;
while (true)
{
var spotifyProcess = Process.GetProcessesByName("Spotify").Where(x => x.MainWindowTitle != null).FirstOrDefault();
if (spotifyProcess != default)
{
if (!running)
{
running = true;
Run();
}
if ((spotifyProcess.MainWindowTitle == "Spotify Premium" || spotifyProcess.MainWindowTitle == "Spotify") && notPlaying == false)
{
ClearStatus?.Invoke(null, new EventArgs());
notPlaying = true;
}
else if (spotifyProcess.MainWindowTitle != prevtitle)
{
string artist = null;
string song = null;
if (spotifyProcess.MainWindowTitle.Contains(" - "))
{
var split = spotifyProcess.MainWindowTitle.Split(" - ");
artist = split[0];
song = split[1];
}
else
{
song = spotifyProcess.MainWindowTitle;
}
UpdateStatus?.Invoke(null, new UpdateActivityEventArgs { Song = song, Artist = artist });
prevtitle = spotifyProcess.MainWindowTitle;
notPlaying = false;
}
}
else
{
ClearStatus?.Invoke(null, new EventArgs());
}
await Task.Delay(500);
}
}).GetAwaiter().GetResult();
}
public static async void Run()
{
var discord = new DiscordRpcClient("809056398939783249");
discord.Initialize();
bool clear = false;
UpdateStatus += UpdateStatusHandler;
ClearStatus += ClearStatusHandler;
while (!clear)
{
await Task.Delay(1000);
}
void UpdateStatusHandler(object sender, UpdateActivityEventArgs e)
{
if (!clear)
discord.UpdateActivity(e.Song, e.Artist);
}
void ClearStatusHandler(object sender, EventArgs e)
{
discord.ClearPresence();
clear = true;
running = false;
discord.Dispose();
UpdateStatus -= UpdateStatusHandler;
ClearStatus -= ClearStatusHandler;
}
}
}
public class UpdateActivityEventArgs
{
public string Artist;
public string Song;
}
}