2020-03-21 18:58:23 -04:00
|
|
|
|
using System;
|
2020-07-18 23:35:19 -04:00
|
|
|
|
using System.Collections.Generic;
|
2020-07-22 20:19:40 -04:00
|
|
|
|
using System.IO;
|
2020-07-18 23:35:19 -04:00
|
|
|
|
using System.Linq;
|
2020-03-22 01:29:51 -04:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
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;
|
2020-03-21 23:55:49 -04:00
|
|
|
|
using Tweetinvi;
|
2020-07-01 22:45:43 -04:00
|
|
|
|
using Tweetinvi.Models;
|
2020-07-22 20:19:40 -04:00
|
|
|
|
using Tweetinvi.Models.Entities;
|
2020-07-18 23:35:19 -04:00
|
|
|
|
using Tweetinvi.Parameters;
|
2020-03-21 18:58:23 -04:00
|
|
|
|
|
|
|
|
|
namespace BirdsiteLive.Twitter
|
|
|
|
|
{
|
|
|
|
|
public interface ITwitterService
|
|
|
|
|
{
|
2020-03-22 01:29:51 -04:00
|
|
|
|
TwitterUser GetUser(string username);
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class TwitterService : ITwitterService
|
|
|
|
|
{
|
|
|
|
|
private readonly TwitterSettings _settings;
|
2020-07-22 20:23:26 -04:00
|
|
|
|
private readonly ITweetExtractor _tweetExtractor;
|
2021-01-12 23:53:23 -05:00
|
|
|
|
private readonly ITwitterStatisticsHandler _statisticsHandler;
|
2020-03-21 18:58:23 -04:00
|
|
|
|
|
|
|
|
|
#region Ctor
|
2021-01-12 23:53:23 -05:00
|
|
|
|
public TwitterService(TwitterSettings settings, ITweetExtractor tweetExtractor, ITwitterStatisticsHandler statisticsHandler)
|
2020-03-21 18:58:23 -04:00
|
|
|
|
{
|
|
|
|
|
_settings = settings;
|
2020-07-22 20:23:26 -04:00
|
|
|
|
_tweetExtractor = tweetExtractor;
|
2021-01-12 23:53:23 -05:00
|
|
|
|
_statisticsHandler = statisticsHandler;
|
2020-07-18 23:35:19 -04:00
|
|
|
|
Auth.SetApplicationOnlyCredentials(_settings.ConsumerKey, _settings.ConsumerSecret, true);
|
2020-03-21 18:58:23 -04:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
2020-03-21 23:55:49 -04:00
|
|
|
|
|
2020-03-22 01:29:51 -04:00
|
|
|
|
public TwitterUser GetUser(string username)
|
2020-03-21 23:55:49 -04:00
|
|
|
|
{
|
|
|
|
|
var user = User.GetUserFromScreenName(username);
|
2021-01-12 23:53:23 -05:00
|
|
|
|
_statisticsHandler.CalledUserApi();
|
2020-03-22 16:38:15 -04:00
|
|
|
|
if (user == null) return null;
|
2020-03-22 01:29:51 -04:00
|
|
|
|
|
2021-01-13 23:57:48 -05:00
|
|
|
|
// Expand URLs
|
|
|
|
|
var description = user.Description;
|
|
|
|
|
foreach (var descriptionUrl in user.Entities?.Description?.Urls?.OrderByDescending(x => x.URL.Length))
|
|
|
|
|
description = description.Replace(descriptionUrl.URL, descriptionUrl.ExpandedURL);
|
|
|
|
|
|
2020-03-22 01:29:51 -04:00
|
|
|
|
return new TwitterUser
|
|
|
|
|
{
|
2020-03-22 16:38:15 -04:00
|
|
|
|
Acct = username,
|
2020-03-22 01:29:51 -04:00
|
|
|
|
Name = user.Name,
|
2021-01-13 23:57:48 -05:00
|
|
|
|
Description = description,
|
2020-03-22 16:45:26 -04:00
|
|
|
|
Url = $"https://twitter.com/{username}",
|
2020-03-22 16:38:15 -04:00
|
|
|
|
ProfileImageUrl = user.ProfileImageUrlFullSize,
|
|
|
|
|
ProfileBackgroundImageUrl = user.ProfileBackgroundImageUrlHttps,
|
|
|
|
|
ProfileBannerURL = user.ProfileBannerURL
|
2020-03-22 01:29:51 -04:00
|
|
|
|
};
|
2020-03-21 23:55:49 -04:00
|
|
|
|
}
|
2020-07-01 22:45:43 -04:00
|
|
|
|
|
2020-07-22 20:19:40 -04:00
|
|
|
|
public ExtractedTweet GetTweet(long statusId)
|
2020-07-01 22:45:43 -04:00
|
|
|
|
{
|
2020-07-22 20:19:40 -04:00
|
|
|
|
TweetinviConfig.CurrentThreadSettings.TweetMode = TweetMode.Extended;
|
2020-07-01 22:45:43 -04:00
|
|
|
|
var tweet = Tweet.GetTweet(statusId);
|
2021-01-12 23:53:23 -05:00
|
|
|
|
_statisticsHandler.CalledTweetApi();
|
|
|
|
|
if (tweet == null) return null; //TODO: test this
|
2020-07-22 20:23:26 -04:00
|
|
|
|
return _tweetExtractor.Extract(tweet);
|
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
|
|
|
|
{
|
|
|
|
|
TweetinviConfig.CurrentThreadSettings.TweetMode = TweetMode.Extended;
|
|
|
|
|
|
|
|
|
|
var user = User.GetUserFromScreenName(username);
|
2021-01-12 23:53:23 -05:00
|
|
|
|
_statisticsHandler.CalledUserApi();
|
2020-07-18 23:35:19 -04:00
|
|
|
|
var tweets = new List<ITweet>();
|
|
|
|
|
if (fromTweetId == -1)
|
|
|
|
|
{
|
|
|
|
|
var timeline = Timeline.GetUserTimeline(user.Id, nberTweets);
|
2021-01-12 23:53:23 -05:00
|
|
|
|
_statisticsHandler.CalledTimelineApi();
|
2020-07-18 23:35:19 -04:00
|
|
|
|
if (timeline != null) tweets.AddRange(timeline);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var timelineRequestParameters = new UserTimelineParameters
|
|
|
|
|
{
|
|
|
|
|
SinceId = fromTweetId,
|
|
|
|
|
MaximumNumberOfTweetsToRetrieve = nberTweets
|
|
|
|
|
};
|
|
|
|
|
var timeline = Timeline.GetUserTimeline(user.Id, timelineRequestParameters);
|
2021-01-12 23:53:23 -05:00
|
|
|
|
_statisticsHandler.CalledTimelineApi();
|
2020-07-18 23:35:19 -04:00
|
|
|
|
if (timeline != null) tweets.AddRange(timeline);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|