protected async override void CheckForChange_Elapsed(object stateinfo)
        {
            try
            {
                if (VideoId == null)
                {
                    VideoId = await scrapeLivestreamId(Name);

                    //Not live
                    if (VideoId == null)
                    {
                        return;
                    }

                    //New livestream
                    else
                    {
                        ViewerGraph = new DatePlot(Name, "Time since start", "Viewers");
                        liveStatus  = await fetchLiveVideoContent();

                        ViewerGraph.AddValue("Viewers", 0, liveStatus.liveStreamingDetails.actualStartTime);

                        foreach (ulong channel in ChannelConfig.Keys.Where(x => (bool)ChannelConfig[x][ONLINE]).ToList())
                        {
                            await OnMinorChangeTracked(channel, (string)ChannelConfig[channel]["Notification"]);
                        }

                        SetTimer(60000, 60000);

                        IconUrl = (await fetchChannel()).snippet.thumbnails.medium.url;
                    }
                }

                liveStatus = await fetchLiveVideoContent();

                bool isStreaming = liveStatus?.snippet?.liveBroadcastContent?.Equals("live") ?? false;

                if (!isStreaming)
                {
                    VideoId = null;
                    SetTimer(600000);
                    ViewerGraph.Dispose();
                    ViewerGraph = null;

                    foreach (var channelMessage in ToUpdate)
                    {
                        await Program.ReactionHandler.ClearHandler((IUserMessage)await ((ITextChannel)Program.Client.GetChannel(channelMessage.Key)).GetMessageAsync(channelMessage.Value));
                    }

                    ToUpdate = new Dictionary <ulong, ulong>();

                    foreach (ulong channel in ChannelConfig.Keys.Where(x => (bool)ChannelConfig[x][OFFLINE]).ToList())
                    {
                        await OnMinorChangeTracked(channel, $"{liveStatus.snippet.channelTitle} went Offline!");
                    }
                }
                else
                {
                    ViewerGraph.AddValue("Viewers", double.Parse(liveStatus.liveStreamingDetails.concurrentViewers));

                    foreach (ulong channel in ChannelConfig.Keys.Where(x => (bool)ChannelConfig[x][SHOWEMBED]).ToList())
                    {
                        await OnMajorChangeTracked(channel, await createEmbed((bool)ChannelConfig[channel][THUMBNAIL], (bool)ChannelConfig[channel][SHOWCHAT]));
                    }
                }

                await UpdateTracker();
            }
            catch (Exception e)
            {
                await Program.MopsLog(new LogMessage(LogSeverity.Error, "", $" error by {Name}", e));
            }
        }
Esempio n. 2
0
        public async override void CheckForChange_Elapsed(object stateinfo)
        {
            try
            {
                if (VideoId == null)
                {
                    VideoId = await scrapeLivestreamId(Name);

                    //Not live
                    if (VideoId == null)
                    {
                        return;
                    }

                    //New livestream
                    else
                    {
                        while (StreamInfo == null)
                        {
                            await Task.Delay(60000);
                        }

                        ViewerGraph = new DatePlot(Name, "Time since start", "Viewers");

                        ViewerGraph.AddValue("Viewers", 0, StreamInfo.liveStreamingDetails.actualStartTime);

                        foreach (ulong channel in ChannelConfig.Keys.Where(x => (bool)ChannelConfig[x][ONLINE]).ToList())
                        {
                            await OnMinorChangeTracked(channel, (string)ChannelConfig[channel]["Notification"]);
                        }

                        //SetTimer(120000, 120000);

                        IconUrl = (await fetchChannel()).snippet.thumbnails.medium.url;

                        await UpdateTracker();
                    }
                }

                if (StreamInfo == null)
                {
                    await Task.Delay(120000);
                }
                bool isStreaming = StreamInfo?.snippet?.liveBroadcastContent?.Equals("live") ?? false;

                if (!isStreaming)
                {
                    await Program.MopsLog(new LogMessage(LogSeverity.Verbose, "", $"Stream went offline for {Name}, investigate please:\n{(StreamInfo != null ? JsonConvert.SerializeObject(StreamInfo) :  "Was null")}"));

                    VideoId = null;
                    //SetTimer(900000);
                    try{
                        if (ChannelConfig.Any(x => (bool)x.Value[SENDGRAPH]))
                        {
                            var png = ViewerGraph.DrawPlot(false, $"{Name}-{DateTime.UtcNow.ToString("MM-dd-yy_hh-mm")}", true);
                            foreach (ulong channel in ChannelConfig.Keys.Where(x => (bool)ChannelConfig[x][SENDGRAPH]).ToList())
                            {
                                await(Program.Client.GetChannel(channel) as SocketTextChannel)?.SendFileAsync(png, "Graph for personal use:");
                            }
                            File.Delete(png);
                        }
                    } catch (Exception e) {
                        await Program.MopsLog(new LogMessage(LogSeverity.Error, "", $" error sending graph by {Name}", e));
                    }

                    ViewerGraph?.Dispose();
                    ViewerGraph = null;

                    ToUpdate = new Dictionary <ulong, ulong>();

                    foreach (ulong channel in ChannelConfig.Keys.Where(x => (bool)ChannelConfig[x][OFFLINE]).ToList())
                    {
                        await OnMinorChangeTracked(channel, $"{StreamInfo?.snippet?.channelTitle ?? "Streamer"} went Offline!");
                    }

                    StreamInfo = null;

                    await UpdateTracker();
                }
                else
                {
                    if (ChannelConfig.Any(x => (bool)x.Value[SHOWGRAPH]))
                    {
                        if (StreamInfo.liveStreamingDetails?.concurrentViewers != null)
                        {
                            ViewerGraph.AddValue("Viewers", double.Parse(StreamInfo.liveStreamingDetails.concurrentViewers));
                        }
                        else
                        {
                            ViewerGraph.AddValue("Viewers", 0);
                        }
                        await UpdateTracker();
                    }

                    foreach (ulong channel in ChannelConfig.Keys.Where(x => (bool)ChannelConfig[x][SHOWEMBED]).ToList())
                    {
                        await OnMajorChangeTracked(channel, await createEmbed((bool)ChannelConfig[channel][THUMBNAIL], (bool)ChannelConfig[channel][SHOWCHAT], (bool)ChannelConfig[channel][SHOWGRAPH]));
                    }
                }
            }
            catch (Exception e)
            {
                await Program.MopsLog(new LogMessage(LogSeverity.Error, "", $" error by {Name}", e));
            }
        }