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."); } }
public async Task BuildClipContainers() { const string broadcaster = "broadcaster"; var pendingBroadcasterConfig = new PendingBroadcasterConfig { Broadcaster = broadcaster }; var broadcasters = new List <PendingBroadcasterConfig> { pendingBroadcasterConfig }; var container = new PendingChannelConfigContainer { Broadcasters = broadcasters }; var containers = new List <PendingChannelConfigContainer> { container }; const string topClipsEndpoint = "https://twitch.tv/topclips"; const string clientId = "123"; const string accept = "application/json"; var channelEndpoint = $"{topClipsEndpoint}&channel={broadcaster}"; var clip = new Clip(); var clips = new List <Clip> { clip }; var getClipsResponse = new GetClipsResponse { Clips = clips }; _TwitchWrapper.Setup(s => s.GetClips(channelEndpoint, clientId, accept)).ReturnsAsync(getClipsResponse); var results = await _Helper.BuildClipContainers(topClipsEndpoint, clientId, accept, containers); _TwitchWrapper.VerifyAll(); Assert.That(results.Count, Is.EqualTo(1)); var result = results[0]; Assert.That(result.PendingChannelConfigContainer, Is.EqualTo(container)); Assert.That(result.PendingClipContainers[0].Clips, Contains.Item(clip)); }