2021-01-25 23:40:30 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-07-18 23:35:19 -04:00
|
|
|
|
using System.Linq;
|
2020-03-22 01:29:51 -04:00
|
|
|
|
using BirdsiteLive.Common.Settings;
|
2021-01-12 23:53:23 -05:00
|
|
|
|
using BirdsiteLive.Statistics.Domain;
|
2020-07-22 20:23:26 -04:00
|
|
|
|
using BirdsiteLive.Twitter.Extractors;
|
2020-03-22 01:29:51 -04:00
|
|
|
|
using BirdsiteLive.Twitter.Models;
|
2021-01-30 00:22:29 -05:00
|
|
|
|
using BirdsiteLive.Twitter.Tools;
|
2021-01-25 23:40:30 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-03-21 23:55:49 -04:00
|
|
|
|
using Tweetinvi;
|
2020-07-01 22:45:43 -04:00
|
|
|
|
using Tweetinvi.Models;
|
2020-07-18 23:35:19 -04:00
|
|
|
|
using Tweetinvi.Parameters;
|
2020-03-21 18:58:23 -04:00
|
|
|
|
|
|
|
|
|
namespace BirdsiteLive.Twitter
|
|
|
|
|
{
|
2021-01-18 02:07:09 -05:00
|
|
|
|
public interface ITwitterTweetsService
|
2020-03-21 18:58:23 -04:00
|
|
|
|
{
|
2020-07-22 20:19:40 -04:00
|
|
|
|
ExtractedTweet GetTweet(long statusId);
|
|
|
|
|
ExtractedTweet[] GetTimeline(string username, int nberTweets, long fromTweetId = -1);
|
2020-03-21 18:58:23 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-18 02:07:09 -05:00
|
|
|
|
public class TwitterTweetsService : ITwitterTweetsService
|
2020-03-21 18:58:23 -04:00
|
|
|
|
{
|
2021-01-30 00:22:29 -05:00
|
|
|
|
private readonly ITwitterAuthenticationInitializer _twitterAuthenticationInitializer;
|
2020-07-22 20:23:26 -04:00
|
|
|
|
private readonly ITweetExtractor _tweetExtractor;
|
2021-01-12 23:53:23 -05:00
|
|
|
|
private readonly ITwitterStatisticsHandler _statisticsHandler;
|
2021-01-18 02:07:09 -05:00
|
|
|
|
private readonly ITwitterUserService _twitterUserService;
|
2021-01-25 23:40:30 -05:00
|
|
|
|
private readonly ILogger<TwitterTweetsService> _logger;
|
2020-03-21 18:58:23 -04:00
|
|
|
|
|
|
|
|
|
#region Ctor
|
2021-01-30 00:22:29 -05:00
|
|
|
|
public TwitterTweetsService(ITwitterAuthenticationInitializer twitterAuthenticationInitializer, ITweetExtractor tweetExtractor, ITwitterStatisticsHandler statisticsHandler, ITwitterUserService twitterUserService, ILogger<TwitterTweetsService> logger)
|
2020-03-21 18:58:23 -04:00
|
|
|
|
{
|
2021-01-30 00:22:29 -05:00
|
|
|
|
_twitterAuthenticationInitializer = twitterAuthenticationInitializer;
|
2020-07-22 20:23:26 -04:00
|
|
|
|
_tweetExtractor = tweetExtractor;
|
2021-01-12 23:53:23 -05:00
|
|
|
|
_statisticsHandler = statisticsHandler;
|
2021-01-18 02:07:09 -05:00
|
|
|
|
_twitterUserService = twitterUserService;
|
2021-01-25 23:40:30 -05:00
|
|
|
|
_logger = logger;
|
2020-03-21 18:58:23 -04:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
2021-01-25 23:40:30 -05:00
|
|
|
|
|
2020-07-22 20:19:40 -04:00
|
|
|
|
public ExtractedTweet GetTweet(long statusId)
|
2020-07-01 22:45:43 -04:00
|
|
|
|
{
|
2021-01-25 23:40:30 -05:00
|
|
|
|
try
|
|
|
|
|
{
|
2021-01-30 00:22:29 -05:00
|
|
|
|
_twitterAuthenticationInitializer.EnsureAuthenticationIsInitialized();
|
2021-01-30 00:05:52 -05:00
|
|
|
|
ExceptionHandler.SwallowWebExceptions = false;
|
2021-01-25 23:40:30 -05:00
|
|
|
|
TweetinviConfig.CurrentThreadSettings.TweetMode = TweetMode.Extended;
|
2021-01-30 00:22:29 -05:00
|
|
|
|
|
2021-01-25 23:40:30 -05:00
|
|
|
|
var tweet = Tweet.GetTweet(statusId);
|
|
|
|
|
_statisticsHandler.CalledTweetApi();
|
|
|
|
|
if (tweet == null) return null; //TODO: test this
|
|
|
|
|
return _tweetExtractor.Extract(tweet);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(e, "Error retrieving tweet {TweetId}", statusId);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-07-22 20:19:40 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ExtractedTweet[] GetTimeline(string username, int nberTweets, long fromTweetId = -1)
|
2020-07-18 23:35:19 -04:00
|
|
|
|
{
|
|
|
|
|
var tweets = new List<ITweet>();
|
2021-01-25 23:40:30 -05:00
|
|
|
|
try
|
2020-07-18 23:35:19 -04:00
|
|
|
|
{
|
2021-01-30 00:22:29 -05:00
|
|
|
|
_twitterAuthenticationInitializer.EnsureAuthenticationIsInitialized();
|
|
|
|
|
ExceptionHandler.SwallowWebExceptions = false;
|
|
|
|
|
TweetinviConfig.CurrentThreadSettings.TweetMode = TweetMode.Extended;
|
|
|
|
|
|
2021-01-29 23:10:02 -05:00
|
|
|
|
var user = _twitterUserService.GetUser(username);
|
|
|
|
|
if (user == null || user.Protected) return new ExtractedTweet[0];
|
|
|
|
|
|
2021-01-25 23:40:30 -05:00
|
|
|
|
if (fromTweetId == -1)
|
|
|
|
|
{
|
|
|
|
|
var timeline = Timeline.GetUserTimeline(user.Id, nberTweets);
|
|
|
|
|
_statisticsHandler.CalledTimelineApi();
|
|
|
|
|
if (timeline != null) tweets.AddRange(timeline);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var timelineRequestParameters = new UserTimelineParameters
|
|
|
|
|
{
|
|
|
|
|
SinceId = fromTweetId,
|
|
|
|
|
MaximumNumberOfTweetsToRetrieve = nberTweets
|
|
|
|
|
};
|
|
|
|
|
var timeline = Timeline.GetUserTimeline(user.Id, timelineRequestParameters);
|
|
|
|
|
_statisticsHandler.CalledTimelineApi();
|
|
|
|
|
if (timeline != null) tweets.AddRange(timeline);
|
|
|
|
|
}
|
2020-07-18 23:35:19 -04:00
|
|
|
|
}
|
2021-01-25 23:40:30 -05:00
|
|
|
|
catch (Exception e)
|
2020-07-18 23:35:19 -04:00
|
|
|
|
{
|
2021-01-25 23:40:30 -05:00
|
|
|
|
_logger.LogError(e, "Error retrieving timeline from {Username}, from {TweetId}", username, fromTweetId);
|
2020-07-18 23:35:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-22 20:23:26 -04:00
|
|
|
|
return tweets.Select(_tweetExtractor.Extract).ToArray();
|
2020-07-18 23:35:19 -04:00
|
|
|
|
}
|
2020-03-21 18:58:23 -04:00
|
|
|
|
}
|
2021-01-18 02:07:09 -05:00
|
|
|
|
}
|