Heltblank

Text

.NET UserControl displaing a Twitter Search (Atom feed)

This sample code shows how to add a Twitter rss, or to be exact atom feed to a .NET page/usercontrol.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using Stormberg.Util;
using System.ServiceModel.Syndication;
using System.Xml;

namespace HeltBlank.social
{
public partial class twitter : System.Web.UI.UserControl
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

//Semicolon seperated string with searchwords; ex "@ravnevand;#ravnevand"
if (TwitterSearch != null)
{
List lst = new List();

string[] split = TwitterSearch.ToString().Split(';');

foreach (string s in split)
{
AddRange(lst, s);
}

if (lst.Count() > 0)
{
rep.DataSource = (from p in lst orderby p.Date descending select p).Take(10);
rep.DataBind();
}
}
else
this.Visible = false;
}

private void AddRange(List lst, string searchword)
{
if (searchword.Trim() != string.Empty)
{
XmlReader reader = XmlReader.Create(@"http://search.twitter.com/search.atom?q=" + HttpUtility.UrlEncode(searchword.Trim()));
SyndicationFeed feed = SyndicationFeed.Load(reader);

if (feed != null && feed.Items.Count() > 0)
{
var sotcPosts = from entry in feed.Items
from a in entry.Authors
select new RssPost
{
Title = entry.Title.Text,
Link = (from p in entry.Links where p.RelationshipType == "alternate" select p.Uri).FirstOrDefault().ToString(),
Image = (from p in entry.Links where p.RelationshipType == "image" select p.Uri).FirstOrDefault().ToString(),
Date = entry.PublishDate.DateTime,
Author = a.Name,
AuthorUri = a.Uri
};

if (sotcPosts.Count() > 0)
{
lst.AddRange(sotcPosts);
}
}
}
}

//Use this in your repeater itemtemplate like this:
//<%# AuthorLink(Eval("Author"), Eval("AuthorUri")) %>

protected string AuthorLink(object author, object url)
{
string a = author.ToString();
string username = a.Substring(0, a.IndexOf(" ")).Trim();
a = a.Replace(username, string.Empty).Replace("(", string.Empty).Replace(")", string.Empty).Trim();

return iRec.Util.XHTML.Link(url.ToString(), string.Empty, a, a);
}

//Use this in your repeater itemtemplate like this:
//<%# TimeAgo((DateTime)Eval("Date")) %>
protected string TimeAgo(DateTime d)
{
TimeSpan ts = DateTime.Now - d;

if(ts.Days > 1)
return "ca " + ts.Days + " days";
if (ts.Days == 1)
return "ca 1 day";

if (ts.Hours > 1)
return "ca " + ts.Hours + " hours";
if (ts.Hours == 1)
return "ca 1 hour";

if (ts.Minutes > 1)
return ts.Minutes + " minutes";
if (ts.Minutes == 1)
return "1 minute";

if (ts.Minutes > 1)
return ts.Seconds + " seconds";

return ts.Seconds + " second";
}

public object TwitterSearch { get; set; }
}


public class RssPost
{
public string Link { get; set; }
public string Title { get; set; }
public string Lead { get; set; }
public string Image { get; set; }
public string Author { get; set; }
public string AuthorUri { get; set; }
public DateTime Date { get; set; }
}
}
Posted on Friday, December 4 2009. Tagged with: twitteratom
Heltblank www. stuff
Previous Next