2020-07-18 23:35:19 -04:00
|
|
|
|
using System;
|
2021-01-19 02:37:24 -05:00
|
|
|
|
using System.Linq;
|
2020-07-18 23:35:19 -04:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Threading.Tasks.Dataflow;
|
2021-01-19 02:37:24 -05:00
|
|
|
|
using BirdsiteLive.Common.Extensions;
|
2021-01-22 21:23:27 -05:00
|
|
|
|
using BirdsiteLive.Common.Settings;
|
2020-07-18 23:35:19 -04:00
|
|
|
|
using BirdsiteLive.DAL.Contracts;
|
2023-01-01 15:18:54 -05:00
|
|
|
|
using BirdsiteLive.Pipeline.Models;
|
2020-07-18 23:35:19 -04:00
|
|
|
|
using BirdsiteLive.Pipeline.Contracts;
|
2021-01-16 00:34:09 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-07-18 23:35:19 -04:00
|
|
|
|
|
|
|
|
|
namespace BirdsiteLive.Pipeline.Processors
|
|
|
|
|
{
|
|
|
|
|
public class RetrieveTwitterUsersProcessor : IRetrieveTwitterUsersProcessor
|
|
|
|
|
{
|
|
|
|
|
private readonly ITwitterUserDal _twitterUserDal;
|
2023-03-25 13:53:07 -04:00
|
|
|
|
private readonly IFollowersDal _followersDal;
|
2023-04-01 19:55:20 -04:00
|
|
|
|
private readonly InstanceSettings _instanceSettings;
|
2021-01-16 00:34:09 -05:00
|
|
|
|
private readonly ILogger<RetrieveTwitterUsersProcessor> _logger;
|
2023-01-10 20:30:07 -05:00
|
|
|
|
private static Random rng = new Random();
|
2021-01-22 21:23:27 -05:00
|
|
|
|
|
2021-01-19 02:37:24 -05:00
|
|
|
|
public int WaitFactor = 1000 * 60; //1 min
|
2020-07-18 23:35:19 -04:00
|
|
|
|
|
|
|
|
|
#region Ctor
|
2023-04-01 19:55:20 -04:00
|
|
|
|
public RetrieveTwitterUsersProcessor(ITwitterUserDal twitterUserDal, IFollowersDal followersDal, InstanceSettings instanceSettings, ILogger<RetrieveTwitterUsersProcessor> logger)
|
2020-07-18 23:35:19 -04:00
|
|
|
|
{
|
|
|
|
|
_twitterUserDal = twitterUserDal;
|
2023-03-25 13:53:07 -04:00
|
|
|
|
_followersDal = followersDal;
|
2023-04-01 19:55:20 -04:00
|
|
|
|
_instanceSettings = instanceSettings;
|
2021-01-16 00:34:09 -05:00
|
|
|
|
_logger = logger;
|
2020-07-18 23:35:19 -04:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2023-01-01 15:18:54 -05:00
|
|
|
|
public async Task GetTwitterUsersAsync(BufferBlock<UserWithDataToSync[]> twitterUsersBufferBlock, CancellationToken ct)
|
2020-07-18 23:35:19 -04:00
|
|
|
|
{
|
2021-01-19 02:37:24 -05:00
|
|
|
|
for (; ; )
|
2020-07-18 23:35:19 -04:00
|
|
|
|
{
|
|
|
|
|
ct.ThrowIfCancellationRequested();
|
|
|
|
|
|
2023-04-02 10:23:45 -04:00
|
|
|
|
if (_instanceSettings.ParallelTwitterRequests == 0)
|
2020-07-18 23:35:19 -04:00
|
|
|
|
{
|
2023-04-02 10:23:45 -04:00
|
|
|
|
while (true)
|
|
|
|
|
await Task.Delay(10000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var users = await _twitterUserDal.GetAllTwitterUsersWithFollowersAsync(2000, _instanceSettings.n_start, _instanceSettings.n_end, _instanceSettings.m);
|
2022-12-26 10:47:26 -05:00
|
|
|
|
|
2023-04-02 10:23:45 -04:00
|
|
|
|
var userCount = users.Any() ? Math.Min(users.Length, 200) : 1;
|
|
|
|
|
var splitUsers = users.OrderBy(a => rng.Next()).ToArray().Split(userCount).ToList();
|
2021-01-19 02:37:24 -05:00
|
|
|
|
|
2023-04-02 10:23:45 -04:00
|
|
|
|
foreach (var u in splitUsers)
|
|
|
|
|
{
|
|
|
|
|
ct.ThrowIfCancellationRequested();
|
|
|
|
|
UserWithDataToSync[] toSync = await Task.WhenAll(
|
|
|
|
|
u.Select(async x => new UserWithDataToSync
|
|
|
|
|
{ User = x, Followers = await _followersDal.GetFollowersAsync(x.Id) }
|
|
|
|
|
)
|
|
|
|
|
);
|
2021-01-19 02:37:24 -05:00
|
|
|
|
|
2023-04-02 10:23:45 -04:00
|
|
|
|
await twitterUsersBufferBlock.SendAsync(toSync, ct);
|
2021-01-19 02:37:24 -05:00
|
|
|
|
|
2020-07-18 23:35:19 -04:00
|
|
|
|
}
|
2023-04-02 10:23:45 -04:00
|
|
|
|
|
|
|
|
|
await Task.Delay(10, ct); // this is somehow necessary
|
2020-07-18 23:35:19 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-26 14:14:25 -05:00
|
|
|
|
}
|