-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.cs
138 lines (111 loc) · 4.51 KB
/
Plugin.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
using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
namespace MusicBeePlugin
{
public partial class Plugin
{
private MusicBeeApiInterface mbApiInterface;
private Settings settings;
private SettingsWindow settingsWindow;
private readonly PluginInfo about = new PluginInfo();
private readonly HttpClient client = new HttpClient();
private bool isSending = false;
public PluginInfo Initialise(IntPtr apiInterfacePtr)
{
mbApiInterface = new MusicBeeApiInterface();
mbApiInterface.Initialise(apiInterfacePtr);
about.PluginInfoVersion = PluginInfoVersion;
about.Name = "Sounday";
about.Description = "Yet another customizable scrobbling plugin for MusicBee.";
about.Author = "Alexandre Breteau";
about.TargetApplication = "";
about.Type = PluginType.General;
about.VersionMajor = 1;
about.VersionMinor = 0;
about.Revision = 0;
about.MinInterfaceVersion = MinInterfaceVersion;
about.MinApiRevision = MinApiRevision;
about.ReceiveNotifications = (ReceiveNotificationFlags.PlayerEvents | ReceiveNotificationFlags.TagEvents);
about.ConfigurationPanelHeight = 0;
LoadSettings();
return about;
}
public bool Configure(IntPtr panelHandle)
{
settingsWindow = new SettingsWindow(this, settings);
settingsWindow.Show();
return true;
}
public void SaveSettings()
{
// ...
}
public void ReceiveNotification(string sourceFileUrl, NotificationType type)
{
switch (type)
{
case NotificationType.TrackChanged:
case NotificationType.PlayStateChanged:
if (mbApiInterface.Player_GetPlayState() == PlayState.Playing)
SendRequest();
break;
}
}
private void LoadSettings()
{
string storagePath = mbApiInterface.Setting_GetPersistentStoragePath();
string filePath = Path.Combine(mbApiInterface.Setting_GetPersistentStoragePath(), $"{about.Name}.settings");
settings = Settings.GetInstance(filePath);
}
private async void SendRequest()
{
if (isSending)
return;
isSending = true;
while (isSending)
{
try
{
if (string.IsNullOrEmpty(settings.RequestUri))
return;
string payload = Regex.Replace(settings.Content, @"{(\w+):(\w+)}", match =>
{
switch (match.Groups[1].Value)
{
case "property":
{
if (Enum.TryParse(match.Groups[2].Value, out FilePropertyType field))
return HttpUtility.JavaScriptStringEncode(mbApiInterface.NowPlaying_GetFileProperty(field));
break;
}
case "tag":
{
if (Enum.TryParse(match.Groups[2].Value, out MetaDataType field))
return HttpUtility.JavaScriptStringEncode(mbApiInterface.NowPlaying_GetFileTag(field));
break;
}
}
return match.Value;
});
HttpRequestMessage request = new HttpRequestMessage(new HttpMethod(settings.RequestMethod), settings.RequestUri);
StringContent content = new StringContent(payload, Encoding.UTF8, settings.MediaType);
request.Content = content;
foreach (var pair in settings.Headers)
request.Headers.Add(pair.Key, pair.Value);
var response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
isSending = false;
}
catch
{
await Task.Delay(3000);
}
}
}
}
}