1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
public static List<Tweet> GetSearchResults(string searchString)
{
using (WebClient web = new WebClient())
{
//More parameters then this. Most important is the page paramater. You can &page=1 to xxx.
//few parameters in the url : &rpp=100 for 100answer | %23 for hashtag | lang=en for english msg......
// exemple: http://search.twitter.com/search.atom?lang=en&rpp=100&q=%23AnHashTag-filter:retweets
// -filter:retweets delete duplicates
string url = string.Format("http://search.twitter.com/search.atom?lang=en&q=%23{0}", searchString); //HttpUtility.UrlEncode(searchString)
WebClient client = new WebClient();
#region AsABrowser
//Pretend to be Google Chrome
//Pretending to be a browser instead of an app seems to make twitter respond faster
client.Headers.Add("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
client.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3");
client.Headers.Add("Accept-Language: en-US,en;q=0.8");
client.Headers.Add("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16");
#endregion
#region TweetFormat
XDocument doc = XDocument.Load(url);
XNamespace ns = "http://www.w3.org/2005/Atom";
List<Tweet> tweets = (from item in doc.Descendants(ns + "entry")
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
}
}).ToList();
return tweets;
#endregion
}
} |
Partager