public void IsReadyToPost(int?numberOfClipsPerDay, string nowString, string stampString, bool enableNumberOfClipsPerDay, bool expectedResult)
        {
            var stamp           = DateTime.Parse(stampString);
            var existingHistory = new BroadcasterHistoryContainer {
                Stamp = stamp
            };
            var existingHistories = new List <BroadcasterHistoryContainer> {
                existingHistory
            };
            var yesterday = DateTime.Parse(nowString).AddDays(-1);
            var pendingBroadcasterConfig = new PendingBroadcasterConfig {
                NumberOfClipsPerDay = numberOfClipsPerDay, ExistingHistories = existingHistories
            };
            var broadcasters = new List <PendingBroadcasterConfig> {
                pendingBroadcasterConfig
            };
            var pendingChannelConfigContainer = new PendingChannelConfigContainer {
                Broadcasters = broadcasters
            };
            var channelContainers = new List <PendingChannelConfigContainer> {
                pendingChannelConfigContainer
            };
            var result = _Helper.ReadyToPostContainers(channelContainers, yesterday, enableNumberOfClipsPerDay);
            var anyReadyBroadcasters = result.SelectMany(s => s.Broadcasters).Any();

            Assert.That(anyReadyBroadcasters, Is.EqualTo(expectedResult));
        }
Пример #2
0
        public static async Task Run([TimerTrigger("%PostClipsFunctionCron%")] TimerInfo myTimer, ILogger log)
        {
            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("local.settings.json", true)
                         .AddEnvironmentVariables()
                         .Build();
            var configWrapper = new ConfigurationWrapper(config);
            //TODO use config wrapper
            var topClipsEndpoint          = Environment.GetEnvironmentVariable("TwitchTopClipsEndpoint");
            var clientId                  = Environment.GetEnvironmentVariable("TwitchClientId");
            var accept                    = Environment.GetEnvironmentVariable("TwitchAcceptHeaderValue");
            var botToken                  = Environment.GetEnvironmentVariable("BotToken");
            var connectionString          = Environment.GetEnvironmentVariable("TopTwitchClipBotConnectionString");
            var enableNumberOfClipsPerDay = bool.Parse(Environment.GetEnvironmentVariable("EnableNumberOfClipsPerDay"));
            var clipOrderMappings         = configWrapper.Get <List <ClipOrderMapping> >("ClipOrderMappings");
            var now        = DateTime.UtcNow;
            var yesterday  = now.AddDays(-1);
            var logWrapper = new LoggerWrapper(log);

            using (var twitchWrapper = new TwitchWrapper())
                using (var discordWrapper = new DiscordWrapper(botToken))
                    using (var context = new TopTwitchClipBotContext(connectionString))
                    {
                        var helper = new PostClipsHelper(logWrapper, context, twitchWrapper, discordWrapper);
                        logWrapper.LogInformation("Posting clips.");
                        await discordWrapper.LogInAsync();

                        var containers = await context.GetPendingChannelConfigsAsync(now.Hour);

                        var afterTimeBetweenClipsContainers = helper.AfterTimeBetweenClips(containers, now);
                        var readyToPostContainers           = helper.ReadyToPostContainers(afterTimeBetweenClipsContainers, yesterday, enableNumberOfClipsPerDay);
                        var pendingClipContainers           = await helper.BuildClipContainers(topClipsEndpoint, clientId, accept, readyToPostContainers);

                        var clipsWithMinViews    = helper.ClipsWithMinViews(pendingClipContainers);
                        var unseenClipContainers = helper.BuildUnseenClipContainers(clipsWithMinViews);
                        var results     = helper.AtATimeContainers(unseenClipContainers, clipOrderMappings);
                        var unseenClips = results.SelectMany(s => s.UnseenClips).ToList();
                        await helper.InsertHistories(unseenClips);

                        var channelContainers = await helper.BuildChannelContainers(results);

                        foreach (var channelContainer in channelContainers)
                        {
                            await helper.SendMessagesAsync(channelContainer);
                        }
                        await discordWrapper.LogOutAsync();

                        logWrapper.LogInformation("Posted clips.");
                    }
        }