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;
|
2022-05-05 20:15:07 -04:00
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Threading.Tasks;
|
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-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 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;
|
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;
|
2022-05-05 20:15:07 -04:00
|
|
|
|
private HttpClient _httpClient = new HttpClient();
|
2020-03-21 18:58:23 -04:00
|
|
|
|
|
|
|
|
|
#region Ctor
|
2022-05-08 19:19:09 -04:00
|
|
|
|
public TwitterTweetsService(ITwitterAuthenticationInitializer twitterAuthenticationInitializer, 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;
|
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
|
|
|
|
|
2022-05-05 20:15:07 -04:00
|
|
|
|
|
2020-07-22 20:19:40 -04:00
|
|
|
|
public ExtractedTweet GetTweet(long statusId)
|
2022-05-05 20:15:07 -04:00
|
|
|
|
{
|
|
|
|
|
return GetTweetAsync(statusId).Result;
|
|
|
|
|
}
|
|
|
|
|
public async Task<ExtractedTweet> GetTweetAsync(long statusId)
|
2020-07-01 22:45:43 -04:00
|
|
|
|
{
|
2021-01-25 23:40:30 -05:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-05-05 20:15:07 -04:00
|
|
|
|
await _twitterAuthenticationInitializer.EnsureAuthenticationIsInitialized();
|
|
|
|
|
JsonDocument tweet;
|
2022-05-08 19:10:08 -04:00
|
|
|
|
using (var request = new HttpRequestMessage(new HttpMethod("GET"), "https://api.twitter.com/2/tweets?ids=" + statusId + "&tweet.fields=id,created_at,text,author_id,in_reply_to_user_id,referenced_tweets,attachments,withheld,geo,entities,public_metrics,possibly_sensitive,source,lang,context_annotations,conversation_id,reply_settings&user.fields=name,username"))
|
2022-05-05 20:15:07 -04:00
|
|
|
|
{
|
|
|
|
|
request.Headers.TryAddWithoutValidation("Authorization", "Bearer " + _twitterAuthenticationInitializer.Token);
|
|
|
|
|
|
|
|
|
|
var httpResponse = await _httpClient.SendAsync(request);
|
|
|
|
|
httpResponse.EnsureSuccessStatusCode();
|
|
|
|
|
var c = await httpResponse.Content.ReadAsStringAsync();
|
|
|
|
|
tweet = JsonDocument.Parse(c);
|
|
|
|
|
}
|
2021-01-30 00:22:29 -05:00
|
|
|
|
|
2021-01-25 23:40:30 -05:00
|
|
|
|
_statisticsHandler.CalledTweetApi();
|
|
|
|
|
if (tweet == null) return null; //TODO: test this
|
2022-05-08 19:26:51 -04:00
|
|
|
|
return tweet.RootElement.GetProperty("data").EnumerateArray().Select<JsonElement, ExtractedTweet>(Extract).ToArray().First();
|
2021-01-25 23:40:30 -05:00
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2022-05-05 20:15:07 -04:00
|
|
|
|
return GetTimelineAsync(username, nberTweets, fromTweetId).Result;
|
|
|
|
|
}
|
|
|
|
|
public async Task<ExtractedTweet[]> GetTimelineAsync(string username, int nberTweets, long fromTweetId = -1)
|
|
|
|
|
{
|
2021-01-30 00:22:29 -05:00
|
|
|
|
|
2022-05-05 20:15:07 -04:00
|
|
|
|
await _twitterAuthenticationInitializer.EnsureAuthenticationIsInitialized();
|
2021-01-29 23:10:02 -05:00
|
|
|
|
|
2021-02-01 20:07:53 -05:00
|
|
|
|
var user = _twitterUserService.GetUser(username);
|
|
|
|
|
if (user == null || user.Protected) return new ExtractedTweet[0];
|
|
|
|
|
|
2022-05-07 18:35:35 -04:00
|
|
|
|
var reqURL = "https://api.twitter.com/2/users/"
|
|
|
|
|
+ user.Id +
|
|
|
|
|
"/tweets?expansions=in_reply_to_user_id,attachments.media_keys,entities.mentions.username,referenced_tweets.id.author_id&tweet.fields=id"
|
|
|
|
|
+ "&max_results=5"
|
|
|
|
|
+ "" ; // ?since_id=2324234234
|
2022-05-05 20:15:07 -04:00
|
|
|
|
JsonDocument tweets;
|
|
|
|
|
try
|
2021-02-01 20:07:53 -05:00
|
|
|
|
{
|
2022-05-07 18:35:35 -04:00
|
|
|
|
using (var request = new HttpRequestMessage(new HttpMethod("GET"), reqURL))
|
2022-05-05 20:15:07 -04:00
|
|
|
|
{
|
|
|
|
|
request.Headers.TryAddWithoutValidation("Authorization", "Bearer " + _twitterAuthenticationInitializer.Token);
|
|
|
|
|
|
|
|
|
|
var httpResponse = await _httpClient.SendAsync(request);
|
|
|
|
|
httpResponse.EnsureSuccessStatusCode();
|
|
|
|
|
var c = await httpResponse.Content.ReadAsStringAsync();
|
|
|
|
|
tweets = JsonDocument.Parse(c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_statisticsHandler.CalledTweetApi();
|
|
|
|
|
if (tweets == null) return null; //TODO: test this
|
2020-07-18 23:35:19 -04:00
|
|
|
|
}
|
2022-05-05 20:15:07 -04:00
|
|
|
|
catch (Exception e)
|
2020-07-18 23:35:19 -04:00
|
|
|
|
{
|
2022-05-05 20:15:07 -04:00
|
|
|
|
_logger.LogError(e, "Error retrieving timeline ", username);
|
|
|
|
|
return null;
|
2020-07-18 23:35:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-08 19:19:09 -04:00
|
|
|
|
return tweets.RootElement.GetProperty("data").EnumerateArray().Select<JsonElement, ExtractedTweet>(Extract).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-09 20:31:18 -04:00
|
|
|
|
private ExtractedTweet Extract(JsonElement tweet)
|
2022-05-08 19:19:09 -04:00
|
|
|
|
{
|
|
|
|
|
bool IsRetweet = false;
|
|
|
|
|
bool IsReply = false;
|
|
|
|
|
long? replyId = null;
|
|
|
|
|
JsonElement replyAccount;
|
|
|
|
|
string? replyAccountString = null;
|
|
|
|
|
JsonElement referenced_tweets;
|
|
|
|
|
if(tweet.TryGetProperty("in_reply_to_user_id", out replyAccount))
|
|
|
|
|
{
|
|
|
|
|
replyAccountString = replyAccount.GetString();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
if(tweet.TryGetProperty("referenced_tweets", out referenced_tweets))
|
|
|
|
|
{
|
|
|
|
|
var first = referenced_tweets.EnumerateArray().ToList()[0];
|
|
|
|
|
if (first.GetProperty("type").GetString() == "retweeted")
|
|
|
|
|
{
|
|
|
|
|
IsRetweet = true;
|
2022-05-09 20:31:18 -04:00
|
|
|
|
var originalAuthor = _twitterUserService.GetUser(Int64.Parse(tweet.GetProperty("author_id").GetString()));
|
2022-05-08 19:19:09 -04:00
|
|
|
|
var statusId = Int64.Parse(first.GetProperty("id").GetString());
|
|
|
|
|
var extracted = GetTweet(statusId);
|
|
|
|
|
extracted.IsRetweet = true;
|
2022-05-09 20:31:18 -04:00
|
|
|
|
extracted.OriginalAuthor = originalAuthor;
|
2022-05-08 19:19:09 -04:00
|
|
|
|
return extracted;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
if (first.GetProperty("type").GetString() == "replied_to")
|
|
|
|
|
{
|
|
|
|
|
IsReply = true;
|
|
|
|
|
replyId = Int64.Parse(first.GetProperty("id").GetString());
|
|
|
|
|
}
|
|
|
|
|
if (first.GetProperty("type").GetString() == "quoted")
|
|
|
|
|
{
|
|
|
|
|
IsReply = true;
|
|
|
|
|
replyId = Int64.Parse(first.GetProperty("id").GetString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var extractedTweet = new ExtractedTweet
|
|
|
|
|
{
|
|
|
|
|
Id = Int64.Parse(tweet.GetProperty("id").GetString()),
|
|
|
|
|
InReplyToStatusId = replyId,
|
|
|
|
|
InReplyToAccount = replyAccountString,
|
|
|
|
|
MessageContent = tweet.GetProperty("text").GetString(),
|
|
|
|
|
Media = Array.Empty<ExtractedMedia>(),
|
|
|
|
|
CreatedAt = DateTime.Now, // tweet.GetProperty("data").GetProperty("in_reply_to_status_id").GetDateTime(),
|
|
|
|
|
IsReply = IsReply,
|
|
|
|
|
IsThread = false,
|
|
|
|
|
IsRetweet = IsRetweet,
|
2022-05-09 20:31:18 -04:00
|
|
|
|
RetweetUrl = "https://t.co/123",
|
|
|
|
|
OriginalAuthor = null,
|
2022-05-08 19:19:09 -04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return extractedTweet;
|
2020-07-18 23:35:19 -04:00
|
|
|
|
}
|
2020-03-21 18:58:23 -04:00
|
|
|
|
}
|
2022-05-07 18:54:06 +00:00
|
|
|
|
}
|