Example #1
0
        /// <summary>
        /// Get the latest tweets from twitter for the specified username.
        /// </summary>
        /// <param name="username"></param>
        /// <param name="maxEntries"></param>
        /// <returns></returns>
        public static IList <Tweet> GetFeed(string username, int maxEntries)
        {
            string        url    = string.Format("http://twitter.com/statuses/user_timeline/{0}.rss", HttpUtility.UrlEncode(username));
            IList <Tweet> tweets = null;

            try
            {
                var feed = FeedHelper.LoadUrl(url);
                tweets = new List <Tweet>();

                foreach (var item in feed.Items)
                {
                    var tweet = new Tweet()
                    {
                        Id        = item.Id,
                        User      = item.Contributors.IsNullOrEmpty() ? string.Empty : item.Contributors[0].Name,
                        Text      = item.Title.Text,
                        Content   = item.Title.Text,
                        Published = feed.LastUpdatedTime.DateTime,
                        Link      = item.Links.IsNullOrEmpty() ? string.Empty : item.Links[0].Uri.OriginalString,
                        Author    = item.Authors.IsNullOrEmpty() ? string.Empty : item.Authors[0].Name
                    };
                    tweets.Add(tweet);
                    if (tweets.Count == maxEntries)
                    {
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.Logger.Error("Unable to get tweets for user : " + username, ex);
                tweets = new List <Tweet>();
            }
            return(tweets);
        }