protected List<TweetModel> RetrieveTweetsFromTwitterFor(TwitterWidgetPart part) { // Build tweet list var tweetList = new List<TweetModel>(); if (!string.IsNullOrEmpty(part.Username)) { string[] hashtagFilter = null; if (!string.IsNullOrEmpty(part.HashTagsFilter)) { hashtagFilter = part.HashTagsFilter.Split(','); } // Fetch data WebClient twitterClient = new WebClient(); string result = twitterClient.DownloadString("http://api.twitter.com/1/statuses/user_timeline.xml?include_entities=1&screen_name=" + part.Username); XElement xmlTweets = XElement.Parse(result); var tweets = from tweet in xmlTweets.Descendants("status") where hashtagFilter == null || (hashtagFilter != null && hashtagFilter.Any(p => tweet.Element("text").Value.Contains(p))) select new TweetModel() { Message = tweet.Element("text").Value, Username = tweet.Element("user").Element("screen_name").Value, Avatar = tweet.Element("user").Element("profile_image_url").Value, Timestamp = ToFriendlyDate(DateTime.ParseExact(tweet.Element("created_at").Value, "ddd MMM dd HH:mm:ss %zzzz yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal)), Id = tweet.Element("id").Value }; tweetList = tweets.Take(part.Count).ToList(); } // Return return tweetList; }
protected List <TweetModel> RetrieveTweetsFromTwitterFor(TwitterWidgetPart part) { // Build tweet list var tweetList = new List <TweetModel>(); if (!string.IsNullOrEmpty(part.Username)) { string[] hashtagFilter = null; if (!string.IsNullOrEmpty(part.HashTagsFilter)) { hashtagFilter = part.HashTagsFilter.Split(','); } // Fetch data WebClient twitterClient = new WebClient(); string result = twitterClient.DownloadString("http://api.twitter.com/1/statuses/user_timeline.xml?include_entities=1&screen_name=" + part.Username); XElement xmlTweets = XElement.Parse(result); var tweets = from tweet in xmlTweets.Descendants("status") where hashtagFilter == null || (hashtagFilter != null && hashtagFilter.Any(p => tweet.Element("text").Value.Contains(p))) select new TweetModel() { Message = tweet.Element("text").Value, Username = tweet.Element("user").Element("screen_name").Value, Avatar = tweet.Element("user").Element("profile_image_url").Value, Timestamp = ToFriendlyDate(DateTime.ParseExact(tweet.Element("created_at").Value, "ddd MMM dd HH:mm:ss %zzzz yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal)), Id = tweet.Element("id").Value }; tweetList = tweets.Take(part.Count).ToList(); } // Return return(tweetList); }
public List <TweetModel> GetTweetsFor(TwitterWidgetPart part) { // Build cache key var cacheKey = CacheKeyPrefix + part.Username; return(CacheManager.Get(cacheKey, ctx => { ctx.Monitor(Clock.When(TimeSpan.FromMinutes(part.CacheMinutes))); return RetrieveTweetsFromTwitterFor(part); })); }
public List<TweetModel> GetTweetsFor(TwitterWidgetPart part) { // Build cache key var cacheKey = CacheKeyPrefix + part.Username; return CacheManager.Get(cacheKey, ctx => { ctx.Monitor(Clock.When(TimeSpan.FromMinutes(part.CacheMinutes))); return RetrieveTweetsFromTwitterFor(part); }); }
protected List <TweetModel> RetrieveTweetsFromTwitterFor(TwitterWidgetPart part) { // Build tweet list var tweetList = new List <TweetModel>(); if (!string.IsNullOrEmpty(part.Username) && !string.IsNullOrEmpty(part.TwitterConsumerKey) && !string.IsNullOrEmpty(part.TwitterConsumerSecret)) { // Obtain bearer token using applicatiuon authroization var bearerToken = ObtainBearerToken(part.TwitterConsumerKey, part.TwitterConsumerSecret); // Call timeline API var timelineUrl = string.Format("https://api.twitter.com/1.1/statuses/user_timeline.json?exclude_replies={0}&screen_name={1}", !part.ShowReplies ? "true" : "false", Uri.EscapeDataString(part.Username)); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(timelineUrl); request.Headers.Add("Authorization", "Bearer " + bearerToken); request.Method = "GET"; request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8"; request.AutomaticDecompression = DecompressionMethods.GZip; WebResponse response = request.GetResponse(); string responseData = new StreamReader(response.GetResponseStream()).ReadToEnd(); var serializer = new JavaScriptSerializer(); var fromJsonArray = serializer.Deserialize <IEnumerable <TwitterJson> >(responseData); string[] hashtagFilter = null; if (!string.IsNullOrEmpty(part.HashTagsFilter)) { hashtagFilter = part.HashTagsFilter.Split(','); } var tweets = from tweet in fromJsonArray where // Hashtags (hashtagFilter == null || (hashtagFilter != null && hashtagFilter.Any(p => tweet.text.Contains(p)))) && // Show replies (part.ShowReplies || String.IsNullOrEmpty(tweet.in_reply_to_screen_name)) select new TweetModel() { Message = tweet.text, Username = tweet.user.screen_name, Avatar = tweet.user.profile_image_url, Timestamp = ToFriendlyDate(DateTime.ParseExact(tweet.created_at, "ddd MMM dd HH:mm:ss %zzzz yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal)), Id = tweet.id_str }; tweetList.AddRange(tweets.Take(part.Count > 0 ? part.Count : 10)); } // Return return(tweetList); }
public IList <Tweet> GetLatestTweetsFor(TwitterWidgetPart part) { List <Tweet> results = new List <Tweet>() { new Tweet { DateStamp = DateTime.Now.AddSeconds(-10), Text = "Tweet number three" }, new Tweet { DateStamp = DateTime.Now.AddMinutes(-10), Text = "Tweet number two" }, new Tweet { DateStamp = DateTime.Now.AddDays(-5), Text = "Tweet number one" } }; return(results); }
protected List<TweetModel> RetrieveTweetsFromTwitterFor(TwitterWidgetPart part) { // Build tweet list var tweetList = new List<TweetModel>(); if (!string.IsNullOrEmpty(part.Username) && !string.IsNullOrEmpty(part.TwitterConsumerKey) && !string.IsNullOrEmpty(part.TwitterConsumerSecret)) { // Obtain bearer token using applicatiuon authroization var bearerToken = ObtainBearerToken(part.TwitterConsumerKey, part.TwitterConsumerSecret); // Call timeline API var timelineUrl = string.Format("https://api.twitter.com/1.1/statuses/user_timeline.json?exclude_replies={0}&screen_name={1}", !part.ShowReplies ? "true" : "false", Uri.EscapeDataString(part.Username)); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(timelineUrl); request.Headers.Add("Authorization", "Bearer " + bearerToken); request.Method = "GET"; request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8"; request.AutomaticDecompression = DecompressionMethods.GZip; WebResponse response = request.GetResponse(); string responseData = new StreamReader(response.GetResponseStream()).ReadToEnd(); var serializer = new JavaScriptSerializer(); var fromJsonArray = serializer.Deserialize<IEnumerable<TwitterJson>>(responseData); string[] hashtagFilter = null; if (!string.IsNullOrEmpty(part.HashTagsFilter)) { hashtagFilter = part.HashTagsFilter.Split(','); } var tweets = from tweet in fromJsonArray where // Hashtags (hashtagFilter == null || (hashtagFilter != null && hashtagFilter.Any(p => tweet.text.Contains(p)))) && // Show replies (part.ShowReplies || String.IsNullOrEmpty(tweet.in_reply_to_screen_name)) select new TweetModel() { Message = tweet.text, Username = tweet.user.screen_name, Avatar = tweet.user.profile_image_url, Timestamp = ToFriendlyDate(DateTime.ParseExact(tweet.created_at, "ddd MMM dd HH:mm:ss %zzzz yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal)), Id = tweet.id_str }; tweetList.AddRange(tweets.Take(part.Count > 0 ? part.Count : 10)); } // Return return tweetList; }