Consuming Twitter Search ATOM With Linq -- And Watch Out For Caching!

March 18 2009

So you want to know what is happening right now. Twitter Search is the way. Constructing a query and parsing the ATOM is pretty easy.

Recently hit a gotcha where the network my app was running was caching the results.  The way to override was to create a WebClient and set the cache policy explicitly.  You can see how that works by setting the RequestCacheLevel to NoCacheNoStore. Then, call OpenRead() on the WebClient.

After that, I use Linq to parse the results. One gotcha is the need to explicitly add the namespace when querying the results.  (If there is a better way, let me know!)

Here's the code.

string xml;
string term = "mix09"; 
using (WebClient webclient = new WebClient()) { RequestCachePolicy policy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); webclient.CachePolicy = policy; Stream stream = webclient.OpenRead
(string.Format("http://search.twitter.com/search.atom?q={0}", term)); StreamReader sr = new StreamReader(stream); xml = sr.ReadToEnd(); sr.Close(); } XDocument doc = XDocument.Parse(xml); XNamespace xmlns = "http://www.w3.org/2005/Atom"; var tweet = from entry in doc.Descendants(xmlns + "entry") select new { Content = entry.Element(xmlns + "title").Value, Date = entry.Element(xmlns + "published").Value, Author = entry.Element(xmlns + "author").Element(xmlns + "uri").Value.Replace("http://twitter.com/", ""), Image = entry.Elements(xmlns + "link").ElementAt(1).Attribute("href").Value }; foreach (var c in tweet) {
//do something here...
}

Note this code is used in Flotzam!

Comments (2) -

3/3/2011 3:21:49 PM #

Richard

Great post.

Richard

3/3/2011 3:27:30 PM #

freelance translation jobs

Twitter is also use as powerful advertising strategy to help promote your sales and products. Twitter is also use to find new partners to help spread their business in the field of marketing.

freelance translation jobs

Add comment

Enter your name, handle, alias, or email.

We'll incarnate your avatar from the services below.



biuquote
Loading