Commit 6a26c389 authored by HankG's avatar HankG

Add TwitterConnector GetUser newer/older w/tests

parent e7410308
......@@ -61,11 +61,31 @@ namespace MySocialPortalLib.Service.SocialMediaConnectors
public IEnumerable<Post> GetNewerUserTimeline(Person person, int maxPosts)
{
throw new System.NotImplementedException();
var twitterId = person?.SocialMediaAccounts[StandardSocialNetworkNames.Twitter]?.ProfileId;
if (twitterId == null)
{
Console.WriteLine($"No Twitter user for person: {person.Id} {person.Name}");
return new List<Post>();
}
var newInterval = TimelineManager.GetNextSamplingInterval(twitterId, EarliestTweetValue, LatestTweetValue);
return PullUserTweets(twitterId, newInterval, maxPosts);
}
public IEnumerable<Post> GetOlderUserTimeline(Person person, int maxPosts)
{
throw new System.NotImplementedException();
var twitterId = person?.SocialMediaAccounts[StandardSocialNetworkNames.Twitter]?.ProfileId;
if (twitterId == null)
{
Console.WriteLine($"No Twitter user for person: {person.Id} {person.Name}");
return new List<Post>();
}
var newInterval = TimelineManager.GetPreviousSamplingInterval(twitterId, EarliestTweetValue, LatestTweetValue);
if (newInterval != null && newInterval.IntervalStop != ulong.MaxValue)
{
newInterval.IntervalStop -= 1;
}
return PullUserTweets(twitterId, newInterval, maxPosts);
}
public bool UserOnSocialNetwork(Person person)
......@@ -104,16 +124,41 @@ namespace MySocialPortalLib.Service.SocialMediaConnectors
select tweet);
}
private IQueryable<Status> GetNoUpperUserStatusQuery(string username, TimelineInterval interval, int maxPosts)
{
using var twitterCtx = new TwitterContext(DoSingleUserAuth());
return (from tweet in twitterCtx.Status
where tweet.Type == StatusType.User &&
tweet.TweetMode == TweetMode.Extended &&
tweet.ScreenName == username &&
tweet.Count == maxPosts &&
tweet.SinceID == interval.IntervalStart
select tweet);
}
private IQueryable<Status> GetUpperLowerHomeStatusQuery(TimelineInterval interval, int maxPosts)
{
using var twitterCtx = new TwitterContext(DoSingleUserAuth());
return (from tweet in twitterCtx.Status
where tweet.Type == StatusType.Home &&
tweet.TweetMode == TweetMode.Extended &&
tweet.Count == maxPosts &&
tweet.MaxID == interval.IntervalStop &&
tweet.SinceID == interval.IntervalStart
select tweet);
where tweet.Type == StatusType.Home &&
tweet.TweetMode == TweetMode.Extended &&
tweet.Count == maxPosts &&
tweet.MaxID == interval.IntervalStop &&
tweet.SinceID == interval.IntervalStart
select tweet);
}
private IQueryable<Status> GetUpperLowerUserStatusQuery(string username, TimelineInterval interval, int maxPosts)
{
using var twitterCtx = new TwitterContext(DoSingleUserAuth());
return (from tweet in twitterCtx.Status
where tweet.Type == StatusType.User &&
tweet.TweetMode == TweetMode.Extended &&
tweet.Count == maxPosts &&
tweet.ScreenName == username &&
tweet.MaxID == interval.IntervalStop &&
tweet.SinceID == interval.IntervalStart
select tweet);
}
private IList<Post> PullHomeTweets(TimelineInterval interval, int maxPosts)
......@@ -131,6 +176,21 @@ namespace MySocialPortalLib.Service.SocialMediaConnectors
return PullTweets(interval, maxPosts, query);
}
private IList<Post> PullUserTweets(string username, TimelineInterval interval, int maxPosts)
{
IQueryable<Status> query;
if (interval.IntervalStop == ulong.MaxValue)
{
query = GetNoUpperUserStatusQuery(username, interval, maxPosts);
}
else
{
query = GetUpperLowerUserStatusQuery(username, interval, maxPosts);
}
return PullTweets(interval, maxPosts, query);
}
private IList<Post> PullTweets(TimelineInterval newInterval, int maxPosts, IQueryable<Status> query)
{
var posts = new List<Post>();
......
......@@ -48,7 +48,7 @@ namespace MySocialPortalLibTest.Service.SocialMediaConnectors
_testOutputHelper.WriteLine("Pulled Posts #1");
PrintPosts(pulledPosts);
Thread.Sleep(1000);
//Thread.Sleep(1000);
pulledPosts = tc.GetNewerHomeTimeline(10);
allPosts.AddRange(pulledPosts);
_testOutputHelper.WriteLine("Pulled Posts #2");
......@@ -85,6 +85,64 @@ namespace MySocialPortalLibTest.Service.SocialMediaConnectors
}
}
[Fact]
public void TestGetNewerUserTimeline()
{
var jack = new Person
{
Name = "Jack Dorsey"
};
jack.SocialMediaAccounts.Add(StandardSocialNetworkNames.Twitter, new SocialMediaAccountData{ProfileId = "jack", SocialMediaSystemName = StandardSocialNetworkNames.Twitter});
var tc = new TwitterConnector(GetTempPersonDb(), GetService());
var allPosts = new List<Post>();
var pulledPosts = tc.GetNewerUserTimeline(jack,10);
allPosts.AddRange(pulledPosts);
_testOutputHelper.WriteLine("Pulled Posts #1");
PrintPosts(pulledPosts);
pulledPosts = tc.GetNewerUserTimeline(jack,10);
allPosts.AddRange(pulledPosts);
_testOutputHelper.WriteLine("Pulled Posts #2");
PrintPosts(pulledPosts);
var postIds = new HashSet<string>(allPosts.Count());
foreach (var p in allPosts)
{
Assert.DoesNotContain(p.OriginalLinkUrlOrId, postIds);
postIds.Add(p.OriginalLinkUrlOrId);
}
}
[Fact]
public void TestGetOlderUserTimeline()
{
var jack = new Person
{
Name = "Jack Dorsey"
};
jack.SocialMediaAccounts.Add(StandardSocialNetworkNames.Twitter, new SocialMediaAccountData{ProfileId = "jack", SocialMediaSystemName = StandardSocialNetworkNames.Twitter});
var tc = new TwitterConnector(GetTempPersonDb(), GetService());
var allPosts = new List<Post>();
var pulledPosts = tc.GetOlderUserTimeline(jack,10);
allPosts.AddRange(pulledPosts);
_testOutputHelper.WriteLine("Pulled Posts #1");
PrintPosts(pulledPosts);
pulledPosts = tc.GetOlderUserTimeline(jack,10);
allPosts.AddRange(pulledPosts);
_testOutputHelper.WriteLine("Pulled Posts #2");
PrintPosts(pulledPosts);
var postIds = new HashSet<string>(allPosts.Count());
foreach (var p in allPosts)
{
Assert.DoesNotContain(p.OriginalLinkUrlOrId, postIds);
postIds.Add(p.OriginalLinkUrlOrId);
}
}
private TimelineManagementService GetService()
{
......@@ -92,9 +150,10 @@ namespace MySocialPortalLibTest.Service.SocialMediaConnectors
}
private PersonsLiteDbRepository GetTempPersonDb()
{
return new PersonsLiteDbRepository(new FileStream(Path.GetTempFileName(), FileMode.OpenOrCreate,
var db = new PersonsLiteDbRepository(new FileStream(Path.GetTempFileName(), FileMode.OpenOrCreate,
FileAccess.ReadWrite, FileShare.None, 4096,
FileOptions.RandomAccess | FileOptions.DeleteOnClose));
return db;
}
private ITimelineRepository GetTempTimelineDb()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment