public async Task GetViewersInfoAsync(string channelName) { if (channelName.Length == 0) { Logger.Log("Channel name is empy."); return; } // Change the name to lowercase, because twitch api only works with lowercase names channelName = channelName.ToLower(); Logger.Log($"Getting information for channel: {channelName}..."); var streamer = new StreamerInformation(channelName, ClientId); await streamer.GetChattersInformationAsync(); await streamer.GetStreamInformationAsync(); var featuredStreams = await TwitchApi.GetFeaturedStreamsAsync(TwitchViewerCounterConfiguration.Instance.GetFeaturedStreamsLocation(), TwitchViewerCounterConfiguration.Instance.GetFeaturedStreamsLanguage()); var featuredStream = StreamHelpers.CheckIfStreamIsFeatured(streamer.Stream, featuredStreams.Featured); await DisplayInformation(streamer, channelName, featuredStream); }
private async Task DisplayInformation(StreamerInformation streamerInformation, string channel, FeaturedStreamInfo featured) { if (streamerInformation.Chatters == null || streamerInformation.Stream == null) { Logger.Log($"Can't get information for channel: {channel}.", LogSeverity.Error); return; } var featuredMessage = ""; if (featured != null) { featuredMessage = "\nIs stream featured: Yes\n" + $"Priority in front page(from 0 to 10): {featured.Priority}\n" + $"Is stream sponsored: {featured.Sponsored}"; } var message = $"Displaying information for channel: {channel}\n" + $"Total viewers: {streamerInformation.Stream.Viewers}\n" + $"Viewers in chat: {streamerInformation.Chatters.ChatterCount}\n" + $"% of people in chat: {streamerInformation.PercentageOfViewersInChat:0.0%}\n" + $"Live started at: {streamerInformation.Stream.LiveStartedAt.ToLocalTime()}" + featuredMessage; await SaveToDatabase(streamerInformation, featured); Logger.Log(message, LogSeverity.Info); }
private async Task SaveToDatabase(StreamerInformation streamer, FeaturedStreamInfo featuredStream) { var streamerEntity = new Streamer { ChannelName = streamer.ChannelName, Chatters = streamer.Chatters.ChatterCount, Viewers = streamer.Stream.Viewers, PercentageOfViewersInChat = streamer.PercentageOfViewersInChat, LiveStartedAt = streamer.Stream.LiveStartedAt.UtcDateTime, Time = DateTime.Now, IsFeatured = featuredStream != null, FeaturedPriority = featuredStream == null ? -1 : featuredStream.Priority, IsSponsored = featuredStream == null ? false : featuredStream.Sponsored }; try { var context = new MongoDataContext(); var streamerRepository = new StreamerRepository(context); await streamerRepository.SaveAsync(streamerEntity); Logger.Log($"Saved {streamerEntity.ChannelName} to database.", LogSeverity.Debug); } catch (Exception ex) { Logger.Log($"Couldn't save {streamerEntity.ChannelName} to database.\n {ex}", LogSeverity.Critical); throw; } }