コード例 #1
0
        protected async override void CheckForChange_Elapsed(object stateinfo)
        {
            try
            {
                TwitchClipResult clips = await getClips();

                foreach (var datetime in TrackedClips.Keys.ToList())
                {
                    if (datetime.AddMinutes(30) <= DateTime.UtcNow)
                    {
                        TrackedClips.Remove(datetime);
                        await UpdateTracker();
                    }
                }

                foreach (Clip clip in clips?.clips ?? new List <Clip>())
                {
                    var embed = createEmbed(clip);
                    foreach (ulong channel in ChannelConfig.Keys.ToList())
                    {
                        if (clip.views >= (uint)ChannelConfig[channel][VIEWTHRESHOLD])
                        {
                            await OnMajorChangeTracked(channel, embed, (string)ChannelConfig[channel]["Notification"]);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                await Program.MopsLog(new LogMessage(LogSeverity.Error, "", $" error by {Name}", e));
            }
        }
コード例 #2
0
        private async Task <TwitchClipResult> NextPage(string name, TwitchClipResult clips = null, string cursor = "")
        {
            if (clips == null)
            {
                clips       = new TwitchClipResult();
                clips.clips = new List <Clip>();
            }
            try
            {
                var acceptHeader = new KeyValuePair <string, string>("Accept", "application/vnd.twitchtv.v5+json");
                var tmpResult    = await FetchJSONDataAsync <TwitchClipResult>($"https://api.twitch.tv/kraken/clips/top?client_id={Program.Config["Twitch"]}&channel={name}&period=day{(!cursor.Equals("") ? $"&cursor={cursor}" : "")}", acceptHeader);

                if (tmpResult.clips != null)
                {
                    foreach (var clip in tmpResult.clips.Where(p => !TrackedClips.ContainsKey(p.created_at) && p.created_at > DateTime.UtcNow.AddMinutes(-30)))
                    {
                        if (clip.vod != null && !TrackedClips.Any(x => {
                            double matchingDuration = 0;

                            if (clip.vod.offset < x.Value.Key)
                            {
                                matchingDuration = (clip.vod.offset + clip.duration > x.Value.Key + x.Value.Value) ? x.Value.Value : clip.vod.offset + clip.duration - x.Value.Key;
                            }
                            else
                            {
                                matchingDuration = (x.Value.Key + x.Value.Value > clip.vod.offset + clip.duration) ? clip.duration : x.Value.Key + x.Value.Value - clip.vod.offset;
                            }

                            double matchingPercentage = matchingDuration / clip.duration;
                            return(matchingPercentage > 0.2);
                        }))
                        {
                            TrackedClips.Add(clip.created_at, new KeyValuePair <int, double>(clip.vod.offset, clip.duration));
                            clips.clips.Add(clip);
                        }
                        else if (clip.vod == null)
                        {
                            TrackedClips.Add(clip.created_at, new KeyValuePair <int, double>(-60, clip.duration));
                            clips.clips.Add(clip);
                        }

                        await UpdateTracker();
                    }
                    if (!tmpResult._cursor.Equals(""))
                    {
                        return(await NextPage(name, clips, tmpResult._cursor));
                    }
                }
                return(clips);
            }
            catch (Exception e)
            {
                await Program.MopsLog(new LogMessage(LogSeverity.Error, "", $" error by {Name}", e));

                return(new TwitchClipResult());
            }
        }