cloutier--bird.makeup/src/BirdsiteLive.Twitter/TwitterTweetsService.cs

111 lines
4.6 KiB
C#
Raw Normal View History

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;
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 18:58:23 -04:00
namespace BirdsiteLive.Twitter
{
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
}
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;
private readonly ITwitterStatisticsHandler _statisticsHandler;
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
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;
_statisticsHandler = statisticsHandler;
_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;
using (var request = new HttpRequestMessage(new HttpMethod("GET"), "https://api.twitter.com/2/tweets?ids=" + statusId))
{
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-07 12:37:40 -04:00
return _tweetExtractor.Extract(tweet.RootElement);
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
var user = _twitterUserService.GetUser(username);
if (user == null || user.Protected) return new ExtractedTweet[0];
2022-05-05 20:15:07 -04:00
JsonDocument tweets;
try
{
2022-05-05 20:15:07 -04:00
using (var request = new HttpRequestMessage(new HttpMethod("GET"), "https://api.twitter.com/2/users/" + user + "/tweets?expansions=in_reply_to_user_id,attachments.media_keys,entities.mentions.username,referenced_tweets.id.author_id&tweet.fields=id"))
{
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-07 12:37:40 -04:00
return tweets.RootElement.GetProperty("data").EnumerateArray().Select<JsonElement, ExtractedTweet>(_tweetExtractor.Extract).ToArray();
2020-07-18 23:35:19 -04:00
}
2020-03-21 18:58:23 -04:00
}
}