private void Form1_Load(object sender, EventArgs e) { api = new TwitchLib.TwitchAPI(); readCredentails(); monitor.OnStreamOnline += onStreamOnline; monitor.OnStreamOffline += onStreamOfffline; }
private async Task StartTwitchMonitoring() { var api = new TwitchLib.TwitchAPI(clientId: ClientId); Service = new FollowerService(api); Service.SetChannelByName(Channel); await Service.StartService(); var v5 = new TwitchLib.Channels.V5(api); var follows = await v5.GetAllFollowersAsync(ChannelId); _CurrentFollowerCount = follows.Count; Service.OnNewFollowersDetected += Service_OnNewFollowersDetected; var v5Stream = CreateTwitchStream(api); if (v5Stream == null) { await Task.Delay(2000); await StartTwitchMonitoring(); return; } var myStream = await v5Stream.GetStreamByUserAsync(ChannelId); _CurrentViewerCount = myStream.Stream?.Viewers ?? 0; Logger.LogInformation($"Now monitoring Twitch with {_CurrentFollowerCount} followers and {_CurrentViewerCount} Viewers"); _Timer = new Timer(CheckViews, v5Stream, 0, 5000); }
private async void CheckViews(object state) { var api = new TwitchLib.TwitchAPI(clientId: ClientId); var v5Stream = new TwitchLib.Streams.V5(api); StreamByUser myStream = null; try { myStream = await v5Stream.GetStreamByUserAsync(ChannelId); } catch (JsonReaderException ex) { Logger.LogError($"Unable to read stream from Twitch: {ex}"); return; } if (_CurrentViewerCount != (myStream.Stream?.Viewers ?? 0)) { _CurrentViewerCount = (myStream.Stream?.Viewers ?? 0); Updated?.Invoke(null, new ServiceUpdatedEventArgs { ServiceName = Name, NewViewers = _CurrentViewerCount }); } }
static void Main(string[] args) { Console.WriteLine("AuthFlowExample using TwitchTokenGenerator.com"); Console.WriteLine(); // These events fire when authorization status changes api = new TwitchLib.TwitchAPI(); api.ThirdParty.AuthorizationFlow.OnUserAuthorizationDetected += onAuthorizationDetected; api.ThirdParty.AuthorizationFlow.OnError += onError; // auth link is created with a single call that includes the requested scopes Console.WriteLine("Getting auth link..."); List <TwitchLib.Enums.AuthScopes> scopes = new List <TwitchLib.Enums.AuthScopes>() { TwitchLib.Enums.AuthScopes.Chat_Login, TwitchLib.Enums.AuthScopes.User_Read }; var createdFlow = api.ThirdParty.AuthorizationFlow.CreateFlow("AuthFlowExample Test Application", scopes); Clipboard.SetText(createdFlow.Url); Console.WriteLine($"Go here to authorize account (copied to clipboard): {createdFlow.Url}"); // every 5 seconds, the library will check if the user has completed authorization Console.WriteLine(); Console.WriteLine("Awaiting authorization..."); api.ThirdParty.AuthorizationFlow.BeginPingingStatus(createdFlow.Id, 5000); // from here, we just wait for the events to fire Console.ReadLine(); }
public async Task OnGet() { var sw = Stopwatch.StartNew(); Uptime = await TwitchProxy.Uptime(); this.Logger.LogInformation($"Get uptime took {sw.ElapsedMilliseconds}ms"); sw.Restart(); var api = new TwitchLib.TwitchAPI(clientId: "t7y5txan5q662t7zj7p3l4wlth8zhv"); var v5Stream = new TwitchLib.Streams.V5(api); var myStream = await v5Stream.GetStreamByUserAsync("96909659"); var createdAt = myStream.Stream?.CreatedAt; this.Logger.LogInformation($"Get uptime took {sw.ElapsedMilliseconds}ms"); }
private TwitchLib.Streams.V5 CreateTwitchStream(TwitchLib.TwitchAPI api) { TwitchLib.Streams.V5 v5Stream = null; try { v5Stream = new TwitchLib.Streams.V5(api); TwitchService.ErrorsReadingViewers = 0; } catch (Exception ex) { TwitchService.ErrorsReadingViewers++; Logger.LogError(ex, $"Error reading viewers.. {TwitchService.ErrorsReadingViewers} consecutive errors"); } return(v5Stream); }
private async void GetTwitchUserDetails() { var twitchApi = new TwitchLib.TwitchAPI(accessToken: _twitchOAuthToken); var twitchDetails = await twitchApi.Root.v5.GetRoot().ConfigureAwait(false); if (twitchDetails?.Token.Valid == true) { Config.Instance.Storage.General.BotName = twitchDetails.Token.Username; Config.Instance.Storage.Credentials.TwitchClientID = twitchDetails.Token.ClientId; Config.Instance.Storage.General.BotUserID = twitchDetails.Token.UserId; Config.Instance.Storage.General.Channel = twitchDetails.Token.Username.ToLower(); } else { throw new Exception("Can't validate Twitch OAuth Token"); } }