Retrieving recent tweets using LINQ
- by brian_ritchie
There are a few different APIs for accessing Twitter from .NET. In this example, I'll use linq2twitter. Other APIs can be found on Twitter's development site.
First off, we'll use the LINQ provider to pull in the recent tweets.
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
1: public static Status[] GetLatestTweets(string screenName, int numTweets)
2: {
3: try
4: {
5: var twitterCtx = new LinqToTwitter.TwitterContext();
6: var list = from tweet in twitterCtx.Status
7: where tweet.Type == StatusType.User &&
8: tweet.ScreenName == screenName
9: orderby tweet.CreatedAt descending
10: select tweet;
11: // using Take() on array because it was failing against the provider
12: var recentTweets = list.ToArray().Take(numTweets).ToArray();
13: return recentTweets;
14: }
15: catch
16: {
17: return new Status[0];
18: }
19: }
Once they have been retrieved, they would be placed inside an MVC model.
Next, the tweets need to be formatted for display. I've defined an extension method to aid with date formatting:
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
1: public static class DateTimeExtension
2: {
3: public static string ToAgo(this DateTime date2)
4: {
5: DateTime date1 = DateTime.Now;
6: if (DateTime.Compare(date1, date2) >= 0)
7: {
8: TimeSpan ts = date1.Subtract(date2);
9: if (ts.TotalDays >= 1)
10: return string.Format("{0} days", (int)ts.TotalDays);
11: else if (ts.Hours > 2)
12: return string.Format("{0} hours", ts.Hours);
13: else if (ts.Hours > 0)
14: return string.Format("{0} hours, {1} minutes",
15: ts.Hours, ts.Minutes);
16: else if (ts.Minutes > 5)
17: return string.Format("{0} minutes", ts.Minutes);
18: else if (ts.Minutes > 0)
19: return string.Format("{0} mintutes, {1} seconds",
20: ts.Minutes, ts.Seconds);
21: else
22: return string.Format("{0} seconds", ts.Seconds);
23: }
24: else
25: return "Not valid";
26: }
27: }
Finally, here is the piece of the view used to render the tweets.
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
1: <ul class="tweets">
2: <%
3: foreach (var tweet in Model.Tweets)
4: {
5: %>
6: <li class="tweets">
7: <span class="tweetTime"><%=tweet.CreatedAt.ToAgo() %> ago</span>:
8: <%=tweet.Text%>
9: </li>
10: <%} %>
11: </ul>