-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHeraldNotifier.cs
More file actions
197 lines (173 loc) · 7.53 KB
/
HeraldNotifier.cs
File metadata and controls
197 lines (173 loc) · 7.53 KB
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
using Observatory.Framework;
using Observatory.Framework.Interfaces;
using System.Diagnostics;
using System;
using System.Net;
using System.Text.Json;
using System.Web;
namespace Observatory.Herald
{
public class HeraldNotifier : IObservatoryNotifier
{
private IObservatoryCore Core;
private AboutInfo _aboutInfo = new()
{
FullName = "Observatory Herald",
ShortName = "Herald",
Description = "Herald is a core plugin for Observatory, designed to provide cloud-based higher-quality speech synthesis.",
AuthorName = "Vithigar",
Links = new()
{
new AboutLink("github", "https://github.com/Xjph/ObservatoryHerald"),
new AboutLink("Documentation", "https://observatory.xjph.net/usage/plugins/herald"),
}
};
public HeraldNotifier()
{
heraldSettings = DefaultSettings;
}
private HeraldSettings DefaultSettings
{
get => new ()
{
SelectedAPI = "Observatory API (Patreon)",
SelectedVoice = "American - Christopher",
SelectedRate = "Default",
PatreonApiEndpoint = "https://api.observatory.xjph.net/",
CacheSize = 100,
SettingsVersion = Version,
UserID = string.Empty,
SubscriptionKey = string.Empty,
AzureRegion = string.Empty,
OpenAIEndpoint = string.Empty,
OpenAIKey = string.Empty,
OpenAIModel = string.Empty,
OpenAIVoice = string.Empty
};
}
public AboutInfo AboutInfo => _aboutInfo;
public static Guid Guid => new("AEA9A421-A19C-421F-A47B-5678956EC202");
public bool OverrideAudioNotifications => true;
public string Version => typeof(HeraldNotifier).Assembly.GetName().Version.ToString();
public PluginUI PluginUI => new (PluginUI.UIType.None, null);
public object Settings
{
get => heraldSettings;
set
{
// Blow away old settings pre-patreon.
var savedSettings = (HeraldSettings)value;
var settingsVersion = new Version(savedSettings.SettingsVersion ?? "0.0");
if (settingsVersion < new Version(2,0))
{
heraldSettings = DefaultSettings;
}
else
{
heraldSettings = savedSettings;
}
}
}
public void Load(IObservatoryCore observatoryCore)
{
Core = observatoryCore;
var apiManager = new ApiRequestManager(
heraldSettings, observatoryCore.HttpClient, observatoryCore.PluginStorageFolder, observatoryCore.GetPluginErrorLogger(this));
heraldSpeech = new HeraldQueue(apiManager, observatoryCore.GetPluginErrorLogger(this), observatoryCore);
if (string.IsNullOrWhiteSpace(heraldSettings.UserID))
heraldSettings.UserID = apiManager.GetNewUserId().Result;
heraldSettings.Test = TestVoice;
heraldSettings.Authenticate = () => { Authenticate(heraldSettings.UserID); };
}
public PluginUpdateInfo CheckForPluginUpdate()
{
var response = SyncResult(Core.HttpClient.GetAsync("https://api.github.com/repos/xjph/ObservatoryHerald/releases"));
if (response.StatusCode == HttpStatusCode.OK)
{
var responseBody = SyncResult(response.Content.ReadAsStringAsync());
var releases = JsonDocument.Parse(responseBody).RootElement.EnumerateArray();
Version? latestVersion = null;
string latestVersionUrl = string.Empty;
foreach (var release in releases)
{
var tag = release.GetProperty("tag_name").ToString();
var verstrings = tag[1..].Split('.');
var ver = verstrings.Select(verString => { _ = int.TryParse(verString, out int ver); return ver; }).ToArray();
if (ver.Length == 3 || ver.Length == 4)
{
Version githubVersion = new(ver[0], ver[1], ver[2], ver.Length == 3 ? 0 : ver[3]);
if (latestVersion == null || githubVersion > latestVersion)
{
latestVersion = githubVersion;
latestVersionUrl = release.GetProperty("html_url").ToString();
}
}
}
if (latestVersion > typeof(HeraldNotifier).Assembly.GetName().Version)
{
return new()
{
Status = PluginUpdateStatus.UpdateAvailable,
Url = latestVersionUrl,
};
}
}
return new();
}
private static T SyncResult<T>(Task<T> task)
{
if (task.Wait(500) && task.IsCompletedSuccessfully)
{
return task.Result;
}
else
{
throw new TimeoutException("The task did not complete within the expected time.");
}
}
private static void Authenticate(string userId)
{
var clientId = "KajbPvbqug8DvcCQuUmto1tlA7_Ai8QgmcHzidP8viYqespuSVu1nU9knLzfhfww";
var redirectUri = HttpUtility.UrlEncode("https://api.observatory.xjph.net/handleOAuth");
var authUrl = $"https://www.patreon.com/oauth2/authorize?response_type=code&client_id={clientId}&redirect_uri={redirectUri}&state={userId}";
Process.Start(new ProcessStartInfo(authUrl) { UseShellExecute = true });
}
private void TestVoice()
{
heraldSpeech.Enqueue(
new NotificationArgs()
{
Title = "Herald voice testing",
Detail = $"This is {heraldSettings.SelectedVoice.Split(" - ")[1]}."
},
GetAzureNameFromSetting(heraldSettings.SelectedVoice),
GetAzureStyleNameFromSetting(heraldSettings.SelectedVoice),
heraldSettings.Rate[heraldSettings.SelectedRate].ToString());
}
public void OnNotificationEvent(NotificationArgs notificationEventArgs)
{
if (Core.IsLogMonitorBatchReading) return;
if (notificationEventArgs.Rendering.HasFlag(NotificationRendering.NativeVocal))
heraldSpeech.Enqueue(
notificationEventArgs,
GetAzureNameFromSetting(heraldSettings.SelectedVoice),
GetAzureStyleNameFromSetting(heraldSettings.SelectedVoice),
heraldSettings.Rate[heraldSettings.SelectedRate].ToString());
}
private string GetAzureNameFromSetting(string settingName)
{
var voiceInfo = (JsonElement)heraldSettings.Voices[settingName];
return voiceInfo.GetProperty("ShortName").GetString();
}
private string GetAzureStyleNameFromSetting(string settingName)
{
string[] settingParts = settingName.Split(" - ");
if (settingParts.Length == 3)
return settingParts[2];
else
return string.Empty;
}
private HeraldSettings heraldSettings;
private HeraldQueue heraldSpeech;
}
}