From f5332192fb3bc03bca055bfe7b005f81584031eb Mon Sep 17 00:00:00 2001 From: Owain Date: Thu, 7 Jul 2022 12:17:13 +0100 Subject: [PATCH] IMP: Add Twitter Helper Service - Thanks Nik for the help with this. --- Core/Services/TwitterHelper.cs | 59 +++++++++++++++++++++++++++ Startup.cs | 2 + ViewComponents/TweetsViewComponent.cs | 57 +++----------------------- 3 files changed, 67 insertions(+), 51 deletions(-) create mode 100644 Core/Services/TwitterHelper.cs diff --git a/Core/Services/TwitterHelper.cs b/Core/Services/TwitterHelper.cs new file mode 100644 index 0000000..22980fc --- /dev/null +++ b/Core/Services/TwitterHelper.cs @@ -0,0 +1,59 @@ +using h5yr.Settings; +using h5yr.ViewComponents; +using Microsoft.Extensions.Options; +using Tweetinvi; +using Tweetinvi.Models; + +namespace H5YR.Core.Services +{ + public interface ITwitterHelper + { + List GetAllTweets(int tweetsToSkip, int tweetsToReturn); + } + public class TwitterHelper : ITwitterHelper + { + private readonly ILogger logger; + private readonly IOptions settings; + + public TwitterHelper(ILogger logger, IOptions settings) + { + this.logger = logger; + this.settings = settings; + } + + public List GetAllTweets(int tweetsToSkip, int tweetsToReturn) + { + // You need to make sure your app on dev.twitter.com has read and write permissions if you wish to tweet! + var creds = new TwitterCredentials(settings.Value.ConsumerKey, settings.Value.ConsumerSecret, settings.Value.AccessToken, settings.Value.AccessTokenSecret); + var userClient = new TwitterClient(creds); + + var searchResults = userClient.Search.SearchTweetsAsync("#h5yr"); + + + List FetchTweets = new List(); + + + foreach (var tweet in searchResults.Result.Skip(tweetsToSkip).Take(tweetsToReturn)) + { + FetchTweets.Add(new TweetModel() + { + + Username = tweet.CreatedBy.ToString(), + Avatar = tweet.CreatedBy.ProfileImageUrl, + Content = tweet.Text, + ScreenName = tweet.CreatedBy.ScreenName.ToString(), + TweetedOn = tweet.CreatedAt, + NumberOfTweets = FetchTweets.Count(), + ReplyToTweet = tweet.IdStr, + Url = tweet.Url + + }); + + + }; + + return FetchTweets; + } + + } +} \ No newline at end of file diff --git a/Startup.cs b/Startup.cs index 2b6cc20..dd35321 100644 --- a/Startup.cs +++ b/Startup.cs @@ -1,4 +1,5 @@ using h5yr.Settings; +using H5YR.Core.Services; namespace h5yr { @@ -46,6 +47,7 @@ public void ConfigureServices(IServiceCollection services) }); services.Configure(_config.GetSection("Twitter")); + services.AddTransient(); } /// diff --git a/ViewComponents/TweetsViewComponent.cs b/ViewComponents/TweetsViewComponent.cs index addb779..c8fc53c 100644 --- a/ViewComponents/TweetsViewComponent.cs +++ b/ViewComponents/TweetsViewComponent.cs @@ -1,28 +1,15 @@ -using h5yr.Settings; +using H5YR.Core.Services; using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Options; -using Tweetinvi; -using Tweetinvi.Models; namespace h5yr.ViewComponents { public class TweetsViewComponent : ViewComponent { - private readonly string _consumerKey; - private readonly string _consumerSecret; - private readonly string _accessToken; - private readonly string _accessTokenSecret; + private readonly ITwitterHelper twitterHelper; - public TweetsViewComponent(IOptions twitterSettings) + public TweetsViewComponent(ITwitterHelper twitterHelper) { - var ts = twitterSettings.Value; - if (ts != null) - { - _consumerKey = ts.ConsumerKey; - _consumerSecret = ts.ConsumerSecret; - _accessToken = ts.AccessToken; - _accessTokenSecret = ts.AccessTokenSecret; - } + this.twitterHelper = twitterHelper; } public IViewComponentResult Invoke(string loadmore = "false") @@ -32,45 +19,13 @@ public IViewComponentResult Invoke(string loadmore = "false") List tweets; - tweets = GetAllTweets(0, 12); + tweets = twitterHelper.GetAllTweets(0, 12); return View(tweets); } - private List GetAllTweets(int tweetsToSkip, int tweetsToReturn) - { - // You need to make sure your app on dev.twitter.com has read and write permissions if you wish to tweet! - var creds = new TwitterCredentials(_consumerKey, _consumerSecret, _accessToken, _accessTokenSecret); - var userClient = new TwitterClient(creds); - - var searchResults = userClient.Search.SearchTweetsAsync("#h5yr"); - - - List FetchTweets = new List(); - - - foreach (var tweet in searchResults.Result.Skip(tweetsToSkip).Take(tweetsToReturn)) - { - FetchTweets.Add(new TweetModel() - { - - Username = tweet.CreatedBy.ToString(), - Avatar = tweet.CreatedBy.ProfileImageUrl, - Content = tweet.Text, - ScreenName = tweet.CreatedBy.ScreenName.ToString(), - TweetedOn = tweet.CreatedAt, - NumberOfTweets = FetchTweets.Count(), - ReplyToTweet = tweet.IdStr, - Url = tweet.Url - - }); - - - }; - - return FetchTweets; - } + } }