Skip to content

Commit

Permalink
IMP: Add Twitter Helper Service
Browse files Browse the repository at this point in the history
- Thanks Nik for the help with this.
  • Loading branch information
Owain committed Jul 7, 2022
1 parent eeb3c3b commit f533219
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 51 deletions.
59 changes: 59 additions & 0 deletions Core/Services/TwitterHelper.cs
Original file line number Diff line number Diff line change
@@ -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<TweetModel> GetAllTweets(int tweetsToSkip, int tweetsToReturn);
}
public class TwitterHelper : ITwitterHelper
{
private readonly ILogger<TwitterHelper> logger;
private readonly IOptions<TwitterSettings> settings;

public TwitterHelper(ILogger<TwitterHelper> logger, IOptions<TwitterSettings> settings)
{
this.logger = logger;
this.settings = settings;
}

public List<TweetModel> 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<TweetModel> FetchTweets = new List<TweetModel>();


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;
}

}
}
2 changes: 2 additions & 0 deletions Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using h5yr.Settings;
using H5YR.Core.Services;

namespace h5yr
{
Expand Down Expand Up @@ -46,6 +47,7 @@ public void ConfigureServices(IServiceCollection services)
});

services.Configure<TwitterSettings>(_config.GetSection("Twitter"));
services.AddTransient<ITwitterHelper, TwitterHelper>();
}

/// <summary>
Expand Down
57 changes: 6 additions & 51 deletions ViewComponents/TweetsViewComponent.cs
Original file line number Diff line number Diff line change
@@ -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> 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")
Expand All @@ -32,45 +19,13 @@ public IViewComponentResult Invoke(string loadmore = "false")

List<TweetModel> tweets;

tweets = GetAllTweets(0, 12);
tweets = twitterHelper.GetAllTweets(0, 12);

return View(tweets);
}


private List<TweetModel> 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<TweetModel> FetchTweets = new List<TweetModel>();


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;
}


}
}
Expand Down

0 comments on commit f533219

Please sign in to comment.