-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
212 lines (196 loc) · 7.98 KB
/
Form1.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using System;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using CefSharp;
using CefSharp.WinForms;
using DiscordRPC;
using DiscordRPC.Logging;
using System.Net;
using System.Threading.Tasks;
namespace Monstecat_Desktop_Player
{
public partial class Form1 : Form
{
public ChromiumWebBrowser browser;
public DiscordRpcClient discord;
public string currentSong = "";
public DateTime songStart = DateTime.UtcNow;
public void InitSettings()
{
if (!Settings.readSettingsFile()){
Settings.createSettingsFile();
Settings.readSettingsFile();
}
}
public void InitBrowser()
{
CefSettings settings = new CefSettings();
settings.CachePath = Environment.CurrentDirectory + @"\CEF";
Cef.Initialize(settings);
browser = new ChromiumWebBrowser(Settings.URL);
this.Controls.Add(browser);
browser.Dock = DockStyle.Fill;
}
public void InitDiscord()
{
discord = new DiscordRpcClient("679284045141770250");
discord.Logger = new ConsoleLogger() { Level = LogLevel.Warning };
discord.OnReady += (sender, e) =>
{
Console.WriteLine("Received Ready from user {0}", e.User.Username);
};
discord.OnPresenceUpdate += (sender, e) =>
{
Console.WriteLine("Received Update! {0}", e.Presence);
};
discord.Initialize();
}
public void InitButtons()
{
if (Settings.usePrevButton)
browser.GetMainFrame().ExecuteJavaScriptAsync("navigator.mediaSession.setActionHandler('previoustrack', () => " + Settings.prevButton + ");");
if (Settings.usePauseButton)
{
browser.GetMainFrame().ExecuteJavaScriptAsync("navigator.mediaSession.setActionHandler('pause', () => " + Settings.pauseButton + ");");
browser.GetMainFrame().ExecuteJavaScriptAsync("navigator.mediaSession.setActionHandler('play', () => " + Settings.pauseButton + ");");
}
if (Settings.useNextButton)
browser.GetMainFrame().ExecuteJavaScriptAsync("navigator.mediaSession.setActionHandler('nexttrack', () => " + Settings.nextButton + ");");
}
public async void browserUpdateThread()
{
while (true)
{
if (browser.IsBrowserInitialized)
{
InitButtons();
InitSettings();
try
{
string songTitle = await getSongTitle();
if (currentSong != songTitle)
{
//only executes on SongUpdate
songStart = DateTime.UtcNow;
currentSong = songTitle;
String Artists = await getArtists();
#pragma warning disable CS4014
pullImage();//waiting on this call is not needed, it writes directly onto file if posible
#pragma warning restore CS4014
WriteFile("SongTitle.txt", songTitle + Settings.afterTitle);
WriteFile("Artists.txt", Artists + Settings.afterArtist);
WriteFile("songinfo.txt", songTitle + Settings.afterTitle + Artists + Settings.afterArtist);
}
}
catch
{
currentSong = "";
songStart = DateTime.UtcNow;
Console.WriteLine("Error grabbing song text. Probably hasn't loaded yet.");
}
}
Thread.Sleep(Settings.songUpdatePause);
}
}
public void DiscordUpdateThread()
{
{
while (true)
{
if (discord.IsInitialized)
{
Assets discordAsset = new Assets()
{
LargeImageKey = "monstercat_logo_trans",
LargeImageText = "Monstercat"
};
if (currentSong != "")
{
discord.SetPresence(new RichPresence()
{
Details = "Now Playing",
State = currentSong,
Timestamps = new Timestamps()
{
Start = songStart
},
Assets = discordAsset
});
}
else
{
discord.SetPresence(new RichPresence()
{
Details = "Nothing Playing",
Assets = discordAsset
});
}
}
Thread.Sleep(Settings.discordUpdatePause);
}
};
}
private async Task<string> getSongTitle()
{
//maybe at Code to prevent crahes if TitlePull fails
var songTitleresult = await browser.GetMainFrame()
.EvaluateScriptAsync("(function() { return " + Settings.songTitleScr + " })();", null);
string songtitle = (string)songTitleresult.Result;
return songtitle;
}
private async Task<string> getArtists()
{
string Artists = "";
var ArtistCount = await browser.GetMainFrame()
.EvaluateScriptAsync("(function() { return " + Settings.artistCountScr + " })();", null);
string s = "" + ArtistCount.Result;
if (int.TryParse(s, out int count))
{
for (int i = 0; i < count; i++)
{
var currentArtist = await browser.GetMainFrame()
.EvaluateScriptAsync("(function() { return " + Settings.artistPreCountScr + i + Settings.artistPostCountScr + "})();", null);
Artists += currentArtist.Result + Settings.betweenArtist;
}
Artists = Artists.Substring(0, Artists.Length - Settings.betweenArtist.Length);
}
return Artists;
}
private async Task pullImage()
{
var songimagescr = await browser.GetMainFrame()
.EvaluateScriptAsync("(function() { return " + Settings.imageScr + "})();", null);
String imagescr = (string)songimagescr.Result;
if (imagescr != null && imagescr.Contains("\""))
{
imagescr = imagescr.Split('"')[1];
Console.WriteLine(imagescr);
using (WebClient client = new WebClient())
{
client.DownloadFile(new Uri(imagescr), Path.Combine(Settings.DataFilePath(), "SongImage.png"));
}
}
}
private void WriteFile(string filename,string infile)
{
using (StreamWriter outputFile = new StreamWriter(Path.Combine(Settings.DataFilePath(), filename)))
{
outputFile.Write(infile);
}
}
public Form1()
{
InitializeComponent();
InitBrowser();
InitDiscord();
InitSettings();
Thread songThread = new Thread(browserUpdateThread);
Thread discordThread = new Thread(DiscordUpdateThread);
songThread.IsBackground = true;
songThread.Start();
discordThread.IsBackground = true;
discordThread.Start();
}
}
}