~cloutier/bird.makeup

df7a4202e8475b11d3551af1c4790dfc72745424 — Vincent Cloutier a month ago 48ec3bc
refactoring for IG
M src/BirdsiteLive.Common/Interfaces/SocialMediaService.cs => src/BirdsiteLive.Common/Interfaces/SocialMediaService.cs +2 -0
@@ 1,3 1,4 @@
using System.Collections;
using System.Text.RegularExpressions;
using System.Threading.Tasks;



@@ 7,6 8,7 @@ public interface ISocialMediaService
{
        Task<SocialMediaUser> GetUserAsync(string username);
        Task<SocialMediaPost?> GetPostAsync(string id);
        Task<SocialMediaPost[]> GetNewPosts(SyncUser user);
        string ServiceName { get;  }
        SocialMediaUserDal UserDal { get; }
        Regex ValidUsername { get;  }

M src/BirdsiteLive.Pipeline/Processors/RetrieveTweetsProcessor.cs => src/BirdsiteLive.Pipeline/Processors/RetrieveTweetsProcessor.cs +7 -34
@@ 4,6 4,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using BirdsiteLive.Common.Interfaces;
using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.DAL.Models;
using BirdsiteLive.Pipeline.Contracts;


@@ 17,18 18,14 @@ namespace BirdsiteLive.Pipeline.Processors.SubTasks
{
    public class RetrieveTweetsProcessor : IRetrieveTweetsProcessor
    {
        private readonly ITwitterTweetsService _twitterTweetsService;
        private readonly ICachedTwitterUserService _twitterUserService;
        private readonly ITwitterUserDal _twitterUserDal;
        private readonly ISocialMediaService _socialMediaService;
        private readonly ILogger<RetrieveTweetsProcessor> _logger;
        private readonly InstanceSettings _settings;

        #region Ctor
        public RetrieveTweetsProcessor(ITwitterTweetsService twitterTweetsService, ITwitterUserDal twitterUserDal, ICachedTwitterUserService twitterUserService, InstanceSettings settings, ILogger<RetrieveTweetsProcessor> logger)
        public RetrieveTweetsProcessor(ISocialMediaService socialMediaService, InstanceSettings settings, ILogger<RetrieveTweetsProcessor> logger)
        {
            _twitterTweetsService = twitterTweetsService;
            _twitterUserDal = twitterUserDal;
            _twitterUserService = twitterUserService;
            _socialMediaService = socialMediaService;
            _logger = logger;
            _settings = settings;
        }


@@ 59,21 56,17 @@ namespace BirdsiteLive.Pipeline.Processors.SubTasks
                    }
                    try 
                    {
                        var tweets = await RetrieveNewTweets(user);
                        _logger.LogInformation(index + "/" + syncTwitterUsers.Count() + " Got " + tweets.Length + " tweets from user " + user.Acct + " " );
                        var tweets = await _socialMediaService.GetNewPosts(user);
                        _logger.LogInformation(index + "/" + syncTwitterUsers.Count() + " Got " + tweets.Length + " posts from user " + user.Acct + " " );
                        if (tweets.Length > 0)
                        {
                            userWtData.Tweets = tweets;
                            userWtData.Tweets = (ExtractedTweet[])tweets;
                            usersWtTweets.Add(userWtData);
                            var tweetId = tweets.Last().Id;
                            await _twitterUserDal.UpdateTwitterUserAsync(user.Id, long.Parse(tweetId), user.FetchingErrorCount, user.LastSync);
                        }

                    } 
                    catch(Exception e)
                    {
                        _logger.LogError(e.Message);
                        //await _twitterUserDal.UpdateTwitterUserAsync(user.Id, user.LastTweetPostedId, user.FetchingErrorCount, now);
                    }
                });
                todo.Add(t);


@@ 89,25 82,5 @@ namespace BirdsiteLive.Pipeline.Processors.SubTasks
            await Task.WhenAll(todo);
            return usersWtTweets.ToArray();
        }

        private async Task<ExtractedTweet[]> RetrieveNewTweets(SyncTwitterUser user)
        {
            var tweets = new ExtractedTweet[0];
            
            try
            {
                if (user.LastTweetPostedId == -1)
                    tweets = await _twitterTweetsService.GetTimelineAsync(user);
                else
                    tweets = await _twitterTweetsService.GetTimelineAsync(user, user.LastTweetPostedId);
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Error retrieving TL of {Username} from {LastTweetPostedId}, purging user from cache", user.Acct, user.LastTweetPostedId);
                _twitterUserService.PurgeUser(user.Acct);
            }

            return tweets;
        }
    }
}
\ No newline at end of file

M src/BirdsiteLive.Twitter/CachedTwitterTweetsService.cs => src/BirdsiteLive.Twitter/CachedTwitterTweetsService.cs +2 -1
@@ 1,6 1,7 @@
using System;
using System.Text.Json;
using System.Threading.Tasks;
using BirdsiteLive.Common.Interfaces;
using BirdsiteLive.Common.Settings;
using BirdsiteLive.DAL.Models;
using BirdsiteLive.Twitter.Models;


@@ 49,7 50,7 @@ namespace BirdsiteLive.Twitter
        }
        #endregion

        public async Task<ExtractedTweet[]> GetTimelineAsync(SyncTwitterUser user, long id)
        public async Task<ExtractedTweet[]> GetTimelineAsync(SyncUser user, long id)
        {
            var res = await _twitterService.GetTimelineAsync(user, id);
            return res;

M src/BirdsiteLive.Twitter/TwitterService.cs => src/BirdsiteLive.Twitter/TwitterService.cs +28 -1
@@ 1,4 1,5 @@
using System;
using System.Linq;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;


@@ 15,13 16,15 @@ namespace BirdsiteLive.Twitter
    public class TwitterService : ISocialMediaService
    {
        private readonly ITwitterTweetsService _twitterTweetsService;
        private readonly ITwitterUserService _twitterUserService;
        private readonly ICachedTwitterUserService _twitterUserService;
        private readonly ITwitterUserDal _userDal;

        #region Ctor
        public TwitterService(ICachedTwitterTweetsService twitterService, ICachedTwitterUserService twitterUserService, ITwitterUserDal userDal, InstanceSettings settings)
        {
            _twitterTweetsService = twitterService;
            _twitterUserService = twitterUserService;
            _userDal = userDal;
            UserDal = userDal;
        }
        #endregion


@@ 34,6 37,30 @@ namespace BirdsiteLive.Twitter
            return post;
        }

        public async Task<SocialMediaPost[]> GetNewPosts(SyncUser user)
        {
            var tweets = new ExtractedTweet[0];
            
            try
            {
                if (user.LastTweetPostedId == -1)
                    tweets = await _twitterTweetsService.GetTimelineAsync(user);
                else
                    tweets = await _twitterTweetsService.GetTimelineAsync(user, user.LastTweetPostedId);
            }
            catch (Exception e)
            {
                _twitterUserService.PurgeUser(user.Acct);
            }
            if (tweets.Length > 0)
            {
                var tweetId = tweets.Last().Id;
                await _userDal.UpdateTwitterUserAsync(user.Id, long.Parse(tweetId), user.FetchingErrorCount, user.LastSync);
            }

            return tweets;
        }

        public string ServiceName { get; } = "Twitter";
        
        // https://help.twitter.com/en/managing-your-account/twitter-username-rules

M src/BirdsiteLive.Twitter/TwitterTweetsService.cs => src/BirdsiteLive.Twitter/TwitterTweetsService.cs +4 -4
@@ 25,7 25,7 @@ namespace BirdsiteLive.Twitter
    public interface ITwitterTweetsService
    {
        Task<ExtractedTweet> GetTweetAsync(long statusId);
        Task<ExtractedTweet[]> GetTimelineAsync(SyncTwitterUser user, long fromTweetId = -1);
        Task<ExtractedTweet[]> GetTimelineAsync(SyncUser user, long fromTweetId = -1);
    }

    public class TwitterTweetsService : ITwitterTweetsService


@@ 111,7 111,7 @@ namespace BirdsiteLive.Twitter
            }
        }

        public async Task<ExtractedTweet[]> GetTimelineAsync(SyncTwitterUser user, long fromTweetId = -1)
        public async Task<ExtractedTweet[]> GetTimelineAsync(SyncUser user, long fromTweetId = -1)
        {

            var client = await _twitterAuthenticationInitializer.MakeHttpClient();


@@ 248,7 248,7 @@ namespace BirdsiteLive.Twitter
            return extractedTweets.ToArray();
        }

        private async Task<List<ExtractedTweet>> TweetFromSidecar(SyncTwitterUser user, long fromId, bool withReplies)
        private async Task<List<ExtractedTweet>> TweetFromSidecar(SyncUser user, long fromId, bool withReplies)
        {
            try
            {


@@ 331,7 331,7 @@ namespace BirdsiteLive.Twitter
            }
        }

        private async Task<List<ExtractedTweet>> TweetFromNitter(SyncTwitterUser user, long fromId, bool withReplies,
        private async Task<List<ExtractedTweet>> TweetFromNitter(SyncUser user, long fromId, bool withReplies,
            bool lowtrust)
        {
            // https://status.d420.de/

M src/dotMakeup.Instagram/InstagramService.cs => src/dotMakeup.Instagram/InstagramService.cs +5 -0
@@ 59,6 59,11 @@ public class InstagramService : ISocialMediaService
        #endregion


        public Task<SocialMediaPost[]> GetNewPosts(SyncUser user)
        {
            throw new NotImplementedException();
        }

        public string ServiceName { get; } = "Instagram";
        public Regex ValidUsername { get;  } = new Regex(@"^[a-zA-Z0-9_\.]{1,30}$");
        public Regex UserMention { get;  } = new Regex(@"(^|.?[ \n\.]+)@([a-zA-Z0-9_\.]+)(?=\s|$|[\[\]<>,;:'\.’!?/—\|-]|(. ))");