Ejemplo n.º 1
0
        public override sealed async Task RunOnceAsync()
        {
            HashSet <String> fullnames = new HashSet <String>();
            ICollection <T>  things    = new List <T>();
            int processedItems         = 0;

            bot.IncrementStatisticIfExists("requestRate");
            Log.Verbose("Processing listing of {Type}", typeof(T).Name);
            await Listing.ForEachAsync(thing =>
            {
                processedItems++;
                if (processedItems % 100 == 1 && processedItems != 1)
                {
                    bot.IncrementStatisticIfExists("requestRate");
                }
                if (RequireUnique && fullnames.Contains(thing.FullName))
                {
                    return;
                }
                if (thing != null && PreRunItem(thing))
                {
                    fullnames.Add(thing.FullName);
                    things.Add(thing);
                }
            });

            Log.Verbose("Found {ProcessedItems}, {Items} of which will be processed", processedItems, things.Count);
            if (RequireInOrder)
            {
                foreach (T thing in things)
                {
                    await RunItemAsync(thing);
                }
            }
            else
            {
                await Task.WhenAll(things.Select(thing => RunItemAsync(thing)));
            }
            try
            {
                await PushMetricAsync(new Models.MetricData()
                {
                    Timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds(),
                    Value     = things.Count,
                });
            }
            catch (StatusPagePushException ex)
            {
                if (!(ex.InnerException is ArgumentNullException))
                {
                    Log.Warning("Failed to push to StatusPage: {ExceptionMessage}", ex.Message);
                }
            }
            await PostRunItemsAsync(things);
        }
Ejemplo n.º 2
0
        public async Task PostDankMeme(EduardoContext context)
        {
            Subreddit subreddit = await _reddit.GetSubredditAsync("dankmemes");

            Listing <Post> postListing = subreddit.GetPosts(Subreddit.Sort.Hot, 100);
            List <Post>    posts       = new List <Post>(100);

            await postListing.ForEachAsync(post =>
            {
                posts.Add(post);
            });

            Post randomPost = posts[_rng.Next(0, posts.Count)];

            using Stream stream = await NetworkHelper.GetStreamAsync(randomPost.Url.AbsoluteUri);

            await context.Channel.SendFileAsync(stream, $"{randomPost.Title}.png");
        }