private void AddTimeLineTweets() { IEnumerable <Tweetinvi.Core.Interfaces.ITweet> tweets; if (User.GetAuthenticatedUser().ScreenName == this.Selecteduser.ScreenName) { var homeTimelineParameter = new HomeTimelineParameters { MaxId = this.TimeLineTweets.Last().Id, MaximumNumberOfTweetsToRetrieve = 15 }; tweets = Timeline.GetHomeTimeline(homeTimelineParameter); } else { var userTimelineParameters = new UserTimelineParameters { MaxId = this.TimeLineTweets.Last().Id, MaximumNumberOfTweetsToRetrieve = 15 }; tweets = Timeline.GetUserTimeline(this.Selecteduser.Id, userTimelineParameters); } var tweetsList = tweets.ToList(); tweetsList.RemoveAt(0); foreach (var tweet in tweetsList) { this.TimeLineTweets.Add((Tweet)tweet); } }
private ICollection <ITweet> GetLatestTweets() { var homeTimelineParameters = new HomeTimelineParameters { MaximumNumberOfTweetsToRetrieve = this.monitorSettings.BatchSize, ExcludeReplies = false, IncludeEntities = true }; if (this.currentBatchLowestTweetId.HasValue) { homeTimelineParameters.MaxId = this.currentBatchLowestTweetId.Value - 1; } if (this.highestTweetId > 0) { homeTimelineParameters.SinceId = this.highestTweetId; } this.logger.LogDebug( "GetHomeTimeline: count = {0}, max_id = {1}, since_id = {2}", homeTimelineParameters.MaximumNumberOfTweetsToRetrieve, homeTimelineParameters.MaxId, homeTimelineParameters.SinceId); var tweets = Timeline.GetHomeTimeline(homeTimelineParameters); return(tweets != null?tweets.ToList() : new List <ITweet>()); }
public void Refresh() { // Get more control over the request with a HomeTimelineParameters var homeTimelineParameter = new HomeTimelineParameters { MaximumNumberOfTweetsToRetrieve = 10 }; var tweetsHome = Timeline.GetHomeTimeline(homeTimelineParameter); if (tweetsHome == null) { Console.WriteLine(ExceptionHandler.GetLastException().TwitterDescription); } foreach (var item in tweetsHome) { if (!RecievedTweets.Contains(item.Id) && !initRun) { NewTweetRecieved(new OnNewTweetArgs { SenderUsername = item.CreatedBy.Name, Content = item.Text, MediaUrls = item.Media }); } else { RecievedTweets.Add(item.Id); } } initRun = false; }
public List <string> GetStatuses() { _log.WriteIn(); var result = new List <string>(); long?sinceId = -1; var homeParams = new HomeTimelineParameters() { MaximumNumberOfTweetsToRetrieve = 200 }; // homeParams.AddCustomQueryParameter("tweet_mode", "extended"); // Avoid truncated tweets - Useless now while (this.GetRemainingRequests() > 0) { if (sinceId != -1) { homeParams.SinceId = (long)sinceId; } _log.Write($"GetHomeTimeLine with sinceId: {sinceId}"); var statuses = _user.GetHomeTimeline(homeParams); _log.Write($"Did we get something? {statuses != null}"); if (statuses == null) { break; // couldn't fetch more } foreach (var tweet in statuses) { var text = tweet.RetweetedTweet?.FullText ?? tweet.FullText; text = this.CleanTweet(text); if (this.IsNiceTweet(text)) { result.Add(text); } sinceId = tweet.Id; } } return(result); }
public Task <IList <Model.Tweet> > RetrieveTweetsAsync() { return(Task.Run(() => { IList <Model.Tweet> retrievedTweets = new List <Model.Tweet>(); // Get Home Timeline // Get more control over the request with a HomeTimelineParameters var homeTimelineParameters = new HomeTimelineParameters { MaximumNumberOfTweetsToRetrieve = 100, // ... setup additional parameters }; var tweets = Timeline.GetHomeTimeline(homeTimelineParameters); foreach (var tweet in tweets) { retrievedTweets.Add(MapTweetinviTweetToTweet(tweet)); } return retrievedTweets; })); }
public static IEnumerator HomeTimeline(Client client, HomeTimelineParameters parameters, Action <List <Tweet> > callback) { yield return(client.GET <List <Tweet> > (Helper.BuildRESTURL("statuses/home_timeline"), parameters, callback)); }
// GET: Feed public async Task <ActionResult> Home() { var authenticateResult = await HttpContext.GetOwinContext().Authentication.AuthenticateAsync("ExternalCookie"); if (Session["Tweets"] is null || (DateTime)Session["TwitterTimer"] < DateTime.Now.Subtract(new TimeSpan(0, 2, 0))) { //grab user access token and secret from claim var oauthToken = authenticateResult.Identity.Claims.FirstOrDefault(x => x.Type == ("urn:twitter:access_token")).Value; var oauthSecret = authenticateResult.Identity.Claims.FirstOrDefault(x => x.Type == ("urn:twitter:access_token_secret")).Value; string Key = WebConfigurationManager.AppSettings["TwitterKey"]; string Secret = WebConfigurationManager.AppSettings["TwitterSecret"]; RateLimit.RateLimitTrackerMode = RateLimitTrackerMode.TrackAndAwait; //access Twitter API and get user's current home timeline Auth.SetUserCredentials(Key, Secret, oauthToken, oauthSecret); var twitterUser = Tweetinvi.User.GetAuthenticatedUser(); // Get more control over the request with a HomeTimelineParameters var homeTimelineParameter = new HomeTimelineParameters { MaximumNumberOfTweetsToRetrieve = 100 }; Session["Tweets"] = (twitterUser.GetHomeTimeline(homeTimelineParameter)).ToArray(); Session["TwitterTimer"] = DateTime.Now; } var userTimeline = Session["Tweets"] as Tweetinvi.Models.ITweet[]; using (var context = new EntityFramework()) { //get the current userID long user = Convert.ToInt64(authenticateResult.Identity.Claims.FirstOrDefault(x => x.Type == "urn:twitter:userid").Value); HttpClient client = new HttpClient(); StringBuilder sourceText = new StringBuilder(); //get the current topics the user follows FeedModel feed = new FeedModel { FollowedTopics = ((from category in context.Set <Category>() where ( (from subcat in context.User_Category where subcat.UserID.Equals(user) select subcat.CategoryID) .ToList()) .Contains(category.ID) select category.CID) .ToList().Select(x => new Category { CID = x })).ToList(), FollowedSources = ((from source in context.Set <Publisher>() where ( (from subcat in context.User_Publisher where subcat.UserID.Equals(user) select subcat.PublisherID) .ToList()) .Contains(source.PID) select new { source.PID, source.Name }) .ToList().Select(x => new Publisher { PID = x.PID, Name = x.Name })).ToList(), Bookmarked_Articles = (from bookmark in context.Set <Bookmarked_Article>() where bookmark.UserID == user select new { bookmark.Title }) .ToList().Select(x => new Bookmarked_Article { Title = x.Title }).ToList() }; feed.Articles = new List <Bookmarked_Article>(); foreach (var source in feed.FollowedSources) { sourceText.Append(String.Format("{0},", source.PID)); } //grab 100 headlines based on the users interests string newsRequest = String.Format("https://newsapi.org/v2/top-headlines?sources={0}&pagesize=100&apiKey=8919f2f78e174c058c8e9745f90524fa", sourceText.ToString()); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, newsRequest); var result = await client.SendAsync(request); int articleCount = 0; if (result.IsSuccessStatusCode) { var articleJSON = JObject.Parse(result.Content.ReadAsStringAsync().Result); foreach (var article in articleJSON["articles"]) { feed.Articles.Add( new Bookmarked_Article { AID = articleCount, SourceName = (string)article["source"]["id"], Author = (string)article["author"], Title = (string)article["title"], Description = (string)article["description"], URL = (string)article["url"], URlToImage = (string)article["urlToImage"], PublishedDate = (DateTime)article["publishedAt"] }); articleCount++; } } sourceText.Clear(); //grab 100 articles based on user interests foreach (var cat in feed.FollowedTopics) { string categoryRequest = String.Format("https://newsapi.org/v2/top-headlines?country=us&language=us&category={0}&pagesize=100&apiKey=8919f2f78e174c058c8e9745f90524fa", cat.CID.ToString()); request = new HttpRequestMessage(HttpMethod.Get, categoryRequest); result = await client.SendAsync(request); if (result.IsSuccessStatusCode) { var articleJSON = JObject.Parse(result.Content.ReadAsStringAsync().Result); foreach (var article in articleJSON["articles"]) { feed.Articles.Add( new Bookmarked_Article { AID = articleCount, SourceName = (string)article["source"]["name"], Author = (string)article["author"], Title = (string)article["title"], Description = (string)article["description"], URL = (string)article["url"], URlToImage = (string)article["urlToImage"], PublishedDate = (DateTime)article["publishedAt"] }); articleCount++; } } } //save list of articles to cache Session["Articles"] = feed.Articles; //shuffle order of articles feed.Articles = feed.Articles.OrderBy(x => Guid.NewGuid()).ToList(); //aggregate news articles and tweets into content block objects List <ContentBlock> contentBlocks = new List <ContentBlock>(); for (int i = 0; i < userTimeline.Length && i < feed.Articles.Count; i += 6) { try { var _artList = new List <Bookmarked_Article>() { feed.Articles[i], feed.Articles[i + 1] }; var _twList = new List <Tweetinvi.Models.ITweet>() { userTimeline[i], userTimeline[i + 1], userTimeline[i + 2], userTimeline[i + 3], userTimeline[i + 4], userTimeline[i + 5] }; contentBlocks.Add( new ContentBlock { Articles = _artList, Tweets = _twList }); } catch { break; } } feed.MixedFeed = contentBlocks; return(View(feed)); } }