Skip to content

Commit

Permalink
feat: use Windows 10 notification center api
Browse files Browse the repository at this point in the history
  • Loading branch information
Genteure committed Aug 26, 2022
1 parent e2bd7be commit 626e0a5
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
6 changes: 5 additions & 1 deletion BililiveRecorder.WPF/BililiveRecorder.WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>Strings.resx</DependentUpon>
</Compile>
<Compile Include="StreamStartedNotification.cs" />
<Compile Include="Update.cs" />
<Compile Include="WorkDirectoryLoader.cs" />
<Compile Include="WpfLogEventSink.cs" />
Expand Down Expand Up @@ -344,6 +345,9 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions">
<Version>6.0.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications">
<Version>7.1.2</Version>
</PackageReference>
<PackageReference Include="ModernWpfUI">
<Version>0.9.4</Version>
</PackageReference>
Expand Down Expand Up @@ -411,4 +415,4 @@
</PropertyGroup>
<Exec Command="$(PkgNuGet_CommandLine)\tools\NuGet pack BililiveRecorder.nuspec -Version %(myAssemblyInfo.Version) -Properties Configuration=Release -OutputDirectory $(NupkgReleaseDir) -BasePath $(OutDir) -NonInteractive -ForceEnglishOutput" />
</Target>
</Project>
</Project>
3 changes: 3 additions & 0 deletions BililiveRecorder.WPF/Pages/RootPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ in the start menu to launch.
if (!recorder.Config.Global.WpfNotifyStreamStart)
return;

_ = StreamStartedNotification.ShowAsync(room);

return;
_ = this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
if (Application.Current.MainWindow is NewMainWindow nmw)
Expand Down
7 changes: 7 additions & 0 deletions BililiveRecorder.WPF/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using BililiveRecorder.ToolBox;
using Esprima;
using Jint.Runtime;
using Microsoft.Toolkit.Uwp.Notifications;
using Sentry;
using Sentry.Extensibility;
using Serilog;
Expand Down Expand Up @@ -221,6 +222,12 @@ internal static int RunWpfReal()
}
finally
{
try
{
ToastNotificationManagerCompat.Uninstall();
}
catch (Exception)
{ }
cancel.Cancel();
#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
update.WaitForUpdatesOnShutdownAsync().GetAwaiter().GetResult();
Expand Down
81 changes: 81 additions & 0 deletions BililiveRecorder.WPF/StreamStartedNotification.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using BililiveRecorder.Core;
using Microsoft.Toolkit.Uwp.Notifications;

#nullable enable
namespace BililiveRecorder.WPF
{
internal static class StreamStartedNotification
{
public static async Task ShowAsync(IRoom room)
{
var tempPath = Path.Combine(Path.GetTempPath(), "brec-notifi");
Uri? cover = null, face = null;
DateTime? time = null;
try
{
Directory.CreateDirectory(tempPath);

var json = room.RawBilibiliApiJsonData;

var live_start_time = json?["room_info"]?["live_start_time"]?.ToObject<long?>();
if (live_start_time.HasValue && live_start_time > 0)
{
time = DateTimeOffset.FromUnixTimeSeconds(live_start_time.Value).LocalDateTime;
}

var coverUrl = json?["room_info"]?["cover"]?.ToObject<string>();
var faceUrl = json?["anchor_info"]?["base_info"]?["face"]?.ToObject<string>();

var coverFile = Path.Combine(tempPath, Path.GetFileName(coverUrl));
var faceFile = Path.Combine(tempPath, Path.GetFileName(faceUrl));

using var client = new HttpClient();
client.Timeout = TimeSpan.FromSeconds(5);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.UserAgent.Clear();

if (!string.IsNullOrEmpty(faceUrl))
{
using var faceFs = new FileStream(faceFile, FileMode.Create, FileAccess.Write, FileShare.None);
await (await client.GetStreamAsync(faceUrl).ConfigureAwait(false)).CopyToAsync(faceFs).ConfigureAwait(false);
face = new Uri(faceFile);
}

if (!string.IsNullOrWhiteSpace(coverUrl))
{
using var coverFs = new FileStream(coverFile, FileMode.Create, FileAccess.Write, FileShare.None);
await (await client.GetStreamAsync(coverUrl).ConfigureAwait(false)).CopyToAsync(coverFs).ConfigureAwait(false);
cover = new Uri(coverFile);
}
}
catch (Exception)
{ }

var roomUrl = new Uri("https://live.bilibili.com/" + room.RoomConfig.RoomId);
var builder = new ToastContentBuilder()
.AddHeader("BililiveRecorder-StreamStarted", "B站录播姬开播通知", "")
.AddText(room.Name + " 开播了")
.AddText(room.Title)
.AddText($"{room.AreaNameParent} · {room.AreaNameChild}")
.SetProtocolActivation(roomUrl)
.SetToastDuration(ToastDuration.Long)
.AddButton(new ToastButton().SetContent("打开直播间").SetProtocolActivation(roomUrl))
;

if (time.HasValue)
builder.AddCustomTimeStamp(time.Value);

if (face is not null)
builder.AddAppLogoOverride(face, ToastGenericAppLogoCrop.Circle);

if (cover is not null)
builder.AddInlineImage(cover);

builder.Show();
}
}
}

0 comments on commit 626e0a5

Please sign in to comment.