mirror of
https://github.com/barelyprofessional/KfChatDotNet.git
synced 2026-05-02 04:22:04 -04:00
Added Parti stream integration
This commit is contained in:
@@ -32,6 +32,7 @@ public class BotServices
|
||||
private BetBolt? _betBolt;
|
||||
private Yeet? _yeet;
|
||||
public AlmanacShill? AlmanacShill;
|
||||
private Parti? _parti;
|
||||
|
||||
private Task? _websocketWatchdog;
|
||||
private Task? _howlggGetUserTimer;
|
||||
@@ -293,6 +294,20 @@ public class BotServices
|
||||
_logger.Info("Built the almanac shill task");
|
||||
}
|
||||
|
||||
private async Task BuildParti()
|
||||
{
|
||||
var settings = await SettingsProvider.GetMultipleValuesAsync([BuiltIn.Keys.Proxy, BuiltIn.Keys.PartiEnabled]);
|
||||
if (!settings[BuiltIn.Keys.PartiEnabled].ToBoolean())
|
||||
{
|
||||
_logger.Debug("Parti is disabled");
|
||||
return;
|
||||
}
|
||||
_parti = new Parti(settings[BuiltIn.Keys.YeetProxy].Value, _cancellationToken);
|
||||
_parti.OnPartiChannelLiveNotification += OnPartiChannelLiveNotification;
|
||||
await _parti.StartWsClient();
|
||||
_logger.Info("Built Parti Websocket connection");
|
||||
}
|
||||
|
||||
private async Task WebsocketWatchdog()
|
||||
{
|
||||
using var timer = new PeriodicTimer(TimeSpan.FromSeconds(10));
|
||||
@@ -907,6 +922,45 @@ public class BotServices
|
||||
_ = new YtDlpCapture($"https://kick.com/{channel.ChannelSlug}", _cancellationToken).CaptureAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnPartiChannelLiveNotification(object sender, PartiChannelLiveNotificationModel data)
|
||||
{
|
||||
var settings = SettingsProvider
|
||||
.GetMultipleValuesAsync([BuiltIn.Keys.PartiChannels, BuiltIn.Keys.CaptureEnabled]).Result;
|
||||
var channels = settings[BuiltIn.Keys.PartiChannels].JsonDeserialize<List<PartiChannelModel>>();
|
||||
if (channels == null)
|
||||
{
|
||||
_logger.Error("Caught a null when deserializing Parti channels");
|
||||
return;
|
||||
}
|
||||
|
||||
var channel = channels.FirstOrDefault(ch => ch.Username == data.Username);
|
||||
if (channel == null)
|
||||
{
|
||||
_logger.Debug($"Got a Parti live notification for a channel we don't care about: {data.Username}");
|
||||
return;
|
||||
}
|
||||
|
||||
using var db = new ApplicationDbContext();
|
||||
var user = db.Users.FirstOrDefault(u => u.KfId == channel.ForumId);
|
||||
if (user == null)
|
||||
{
|
||||
_logger.Error($"Caught a null when retrieving forum ID {channel.ForumId}");
|
||||
return;
|
||||
}
|
||||
|
||||
var url = $"https://parti.com/creator/{data.SocialMedia}/{data.Username}/";
|
||||
if (data.SocialMedia == "discord")
|
||||
{
|
||||
url += "0";
|
||||
}
|
||||
_chatBot.SendChatMessage($"@{user.KfUsername} is live! {data.EventTitle} {url}", true);
|
||||
if (channel.AutoCapture && settings[BuiltIn.Keys.CaptureEnabled].ToBoolean())
|
||||
{
|
||||
_logger.Info($"{channel.Username} is configured to auto capture");
|
||||
_ = new YtDlpCapture(url, _cancellationToken).CaptureAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnStopStreamBroadcast(object sender, KickModels.StopStreamBroadcastEventModel? e)
|
||||
{
|
||||
|
||||
126
KfChatDotNetBot/Services/Parti.cs
Normal file
126
KfChatDotNetBot/Services/Parti.cs
Normal file
@@ -0,0 +1,126 @@
|
||||
using System.Net;
|
||||
using System.Net.WebSockets;
|
||||
using System.Text.Json;
|
||||
using KfChatDotNetBot.Models;
|
||||
using NLog;
|
||||
using Websocket.Client;
|
||||
|
||||
namespace KfChatDotNetBot.Services;
|
||||
|
||||
public class Parti : IDisposable
|
||||
{
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
private WebsocketClient? _wsClient;
|
||||
private Uri _wsUri = new("wss://ws-backend.parti.com/");
|
||||
// Parti can go a long ass time without sending a message and there's no ping/pong
|
||||
// So let's just set a really high timeout
|
||||
private int _reconnectTimeout = 300;
|
||||
private string? _proxy;
|
||||
public delegate void OnPartiChannelLiveNotificationEventHandler(object sender, PartiChannelLiveNotificationModel data);
|
||||
public delegate void OnWsDisconnectionEventHandler(object sender, DisconnectionInfo e);
|
||||
public event OnPartiChannelLiveNotificationEventHandler? OnPartiChannelLiveNotification;
|
||||
public event OnWsDisconnectionEventHandler? OnWsDisconnection;
|
||||
private CancellationToken _cancellationToken;
|
||||
|
||||
public Parti(string? proxy = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
_proxy = proxy;
|
||||
_cancellationToken = cancellationToken;
|
||||
_logger.Info("Parti WebSocket client created");
|
||||
}
|
||||
|
||||
public async Task StartWsClient()
|
||||
{
|
||||
_logger.Debug("StartWsClient() called, creating client");
|
||||
await CreateWsClient();
|
||||
}
|
||||
|
||||
private async Task CreateWsClient()
|
||||
{
|
||||
var factory = new Func<ClientWebSocket>(() =>
|
||||
{
|
||||
var clientWs = new ClientWebSocket();
|
||||
clientWs.Options.SetRequestHeader("Origin", "https://parti.com");
|
||||
clientWs.Options.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:126.0) Gecko/20100101 Firefox/126.0");
|
||||
if (_proxy == null) return clientWs;
|
||||
_logger.Debug($"Using proxy address {_proxy}");
|
||||
clientWs.Options.Proxy = new WebProxy(_proxy);
|
||||
return clientWs;
|
||||
});
|
||||
|
||||
var client = new WebsocketClient(_wsUri, factory)
|
||||
{
|
||||
ReconnectTimeout = TimeSpan.FromSeconds(_reconnectTimeout),
|
||||
IsReconnectionEnabled = false
|
||||
};
|
||||
|
||||
client.ReconnectionHappened.Subscribe(WsReconnection);
|
||||
client.MessageReceived.Subscribe(WsMessageReceived);
|
||||
client.DisconnectionHappened.Subscribe(WsDisconnection);
|
||||
|
||||
_wsClient = client;
|
||||
|
||||
_logger.Debug("Websocket client has been built, about to start");
|
||||
await client.Start();
|
||||
_logger.Debug("Websocket client started!");
|
||||
}
|
||||
|
||||
public bool IsConnected()
|
||||
{
|
||||
return _wsClient is { IsRunning: true };
|
||||
}
|
||||
|
||||
private void WsDisconnection(DisconnectionInfo disconnectionInfo)
|
||||
{
|
||||
_logger.Error($"Client disconnected from Parti (or never successfully connected). Type is {disconnectionInfo.Type}");
|
||||
_logger.Error($"Close Status => {disconnectionInfo.CloseStatus}; Close Status Description => {disconnectionInfo.CloseStatusDescription}");
|
||||
_logger.Error(disconnectionInfo.Exception);
|
||||
OnWsDisconnection?.Invoke(this, disconnectionInfo);
|
||||
}
|
||||
|
||||
private void WsReconnection(ReconnectionInfo reconnectionInfo)
|
||||
{
|
||||
_logger.Error($"Websocket connection dropped and reconnected. Reconnection type is {reconnectionInfo.Type}");
|
||||
if (reconnectionInfo.Type == ReconnectionType.Initial)
|
||||
{
|
||||
_logger.Info("Sending subscribe payload to Parti");
|
||||
_wsClient?.Send("{\"subscribe_options\":{\"NewChannelLiveNotify\":{}}}");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void WsMessageReceived(ResponseMessage message)
|
||||
{
|
||||
if (message.Text == null)
|
||||
{
|
||||
_logger.Info("Parti sent a null message");
|
||||
return;
|
||||
}
|
||||
_logger.Trace($"Received event from Parti: {message.Text}");
|
||||
|
||||
try
|
||||
{
|
||||
var payload = JsonSerializer.Deserialize<PartiChannelLiveNotificationModel>(message.Text);
|
||||
if (payload == null)
|
||||
{
|
||||
throw new Exception(
|
||||
"Caught a null when trying to deserialize the Parti livestream notification payload");
|
||||
}
|
||||
OnPartiChannelLiveNotification?.Invoke(this, payload);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Error("Failed to handle message from Parti");
|
||||
_logger.Error(e);
|
||||
_logger.Error("--- Payload ---");
|
||||
_logger.Error(message.Text);
|
||||
_logger.Error("--- End of Payload ---");
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_wsClient?.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user