Ejemplo n.º 1
0
    private static async Task <int> RunAsync(MenuType menuType)
    {
        var now  = KST.Now;
        var date = now.ToDateOnly();

        Log.Information($"Action started time: {now} KST\n");

        var consumerApiKey    = GetEnvVariable(ConsumerApiKeyName);
        var consumerSecretKey = GetEnvVariable(ConsumerSecretKeyName);
        var accessToken       = GetEnvVariable(AccessTokenName);
        var accessTokenSecret = GetEnvVariable(AccessTokenSecretName);
        var twitterApiKeys    = new TwitterApiKeys(consumerApiKey, consumerSecretKey, accessToken, accessTokenSecret);

        Log.Information($"Successfully loaded Twitter API keys.");

        Log.Information("Requesting menu list...");

        var weekMenu = await WeekMenu.LoadAsync();

        var todayMenu = weekMenu.Find(date);

        if (todayMenu is null)
        {
            Log.Warning($"Cannot found menu of day {now.Day}.");
            return(0);
        }

        var menu = todayMenu[menuType];

        if (menu is null || menu.IsEmpty())
        {
            Log.Warning($"Cannot found menu of type {menuType}.");
            return(0);
        }

        Log.Information(menu.ToString());

        Log.Information("Generating image...");
        var image = await MenuImageGenerator.GenerateAsync(date, menuType, menu);

        Log.Information($"Image generated. length={image.Length}");

        var twitterClient = GetTwitterClient(twitterApiKeys);

        Log.Information("Tweeting...");
        var tweetMessage = GetMenuTweetText(date, menuType);

        Log.Information($"\n- tweet message -\n{tweetMessage}\n- tweet message -");

#if DEBUG
        Log.Information("Cannot tweet because it's debug mode.");
#else
        var tweet = await PublishTweetAsync(twitterClient, tweetMessage, image);

        Log.Information($"Done! {tweet.Url}");
#endif

        return(0);
    }
Ejemplo n.º 2
0
    private static async Task ProcessAsync(ILogger log, DateTime now, MenuType menuType)
    {
        var date = now.ToDateOnly();

        var consumerApiKey    = GetEnvVariable(ConsumerApiKeyName);
        var consumerSecretKey = GetEnvVariable(ConsumerSecretKeyName);
        var accessToken       = GetEnvVariable(AccessTokenName);
        var accessTokenSecret = GetEnvVariable(AccessTokenSecretName);
        var twitterApiKeys    = new TwitterApiKeys(consumerApiKey, consumerSecretKey, accessToken, accessTokenSecret);

        log.LogInformation($"Successfully loaded Twitter API keys.");
        log.LogInformation($"Menu type = {menuType}");

        log.LogInformation("Requesting menu list...");

        var weekMenu = await WeekMenu.LoadAsync();

        var todayMenu = weekMenu.Find(date);

        if (todayMenu is null)
        {
            throw new ProcessFaildException($"Cannot found menu of day {now.Day}.");
        }

        var menu = todayMenu[menuType];

        if (menu is null || menu.IsEmpty())
        {
            throw new ProcessFaildException($"Cannot found menu of type {menuType}.");
        }

        log.LogInformation(menu.ToString());

        var tweetText = MakeTweetText(date, menuType, menu);

        log.LogInformation("===============================");
        log.LogInformation(tweetText);
        log.LogInformation("===============================");

        var twitterClient = GetTwitterClient(twitterApiKeys);

        log.LogInformation("Tweeting...");

        // Test
        var authenticatedUser = await twitterClient.Users.GetAuthenticatedUserAsync();

        log.LogInformation($"Bot information: {authenticatedUser.Name}@{authenticatedUser.ScreenName}");

#if DEBUG
        log.LogInformation("Cannot tweet because it's debug mode.");
#else
        var tweet = await PublishTweetAsync(twitterClient, tweetText);

        log.LogInformation($"Done! {tweet.Url}");
#endif
    }
Ejemplo n.º 3
0
        public async Task MenuLoadTest()
        {
            var menu = await WeekMenu.LoadAsync();

            Assert.IsNotNull(menu);

            if (menu.Count > 0)
            {
                foreach (var todayMenu in menu)
                {
                    if (!todayMenu.Lunch.IsEmpty())
                    {
                        var imageBytes = await MenuImageGenerator.GenerateAsync(todayMenu.Date, MenuType.Lunch, todayMenu.Lunch);

                        Assert.IsNotNull(imageBytes);
                    }
                }
            }
        }