public class Twitter { private static XDocument xDoc = null; public Int32 TweetCount { get; set; } public String TwitterProfileName { get; set; } public List<Tweet> Tweets { get { XDocument xml = LoadTweetXML(false); if (xml == null) throw new Exception(String.Format("Failed to get tweets from {0}", TwitterProfileName)); var query = from e in xml.Descendants("item") select new Tweet { Id = e.Element("guid").Value, Title = e.Element("title").Value .Replace(String.Format("{0}: ", TwitterProfileName), String.Empty), Link = e.Element("link").Value, Content = e.Element("description").Value, Published = Convert.ToDateTime((e.Descendants("pubDate").First().Value)), }; return query.ToList(); } } public List<Tweet> TweetMentions { get { XDocument xml = LoadTweetXML(true); if (xml == null) throw new Exception(String.Format("Failed to get tweets mentions from {0}", TwitterProfileName)); XNamespace ns = "http://www.w3.org/2005/Atom"; var query = from item in xml.Descendants(ns + "entry").Take(TweetCount) select new Tweet { Id = item.Element(ns + "id").Value, Published = DateTime.Parse(item.Element(ns + "published").Value), Title = item.Element(ns + "title").Value, Content = item.Element(ns + "content").Value, Link = (from XElement x in item.Descendants(ns + "link") where x.Attribute("type").Value == "text/html" select x.Attribute("href").Value).First(), Image = (from XElement x in item.Descendants(ns + "link") where x.Attribute("type").Value == "image/png" select x.Attribute("href").Value).First(), Author = new Author() { Name = item.Element(ns + "author").Element(ns + "name").Value, Uri = item.Element(ns + "author").Element(ns + "uri").Value } }; return query.ToList(); } } public Twitter(string screenName, int numTweets = 10) { TwitterProfileName = screenName; TweetCount = numTweets; } private XDocument LoadTweetXML(bool isMentions) { try { string url; if (isMentions) url = String.Format( "http://search.twitter.com/search.atom?lang=en&q={0}", System.Web.HttpUtility.UrlEncode(TwitterProfileName)); else url = String.Format( "http://api.twitter.com/statuses/user_timeline.rss?screen_name={0}&count={1}", TwitterProfileName, TweetCount); xDoc = XDocument.Load(url); return xDoc; } catch { return null; } } public class Tweet { public string Id { get; set; } public DateTime Published { get; set; } public string Title { get; set; } public string Content { get; set; } public string Link { get; set; } public Author Author { get; set; } public string Image { get; set; } } public class Author { public string Name { get; set; } public string Uri { get; set; } } }
SELECT * FROM Joseph.Thoughts WHERE Topics LIKE 'Pseudo' AND PostDateTime LIKE 'Random' DESC
Tuesday, July 10, 2012
Extracting Twitter Posts and Twitter Mentions
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment