Want to Learn Javascript.

Twitter App for BlogEngine.net

9. March 2008 16:57 by scott in   //  Tags: ,   //   Comments (5)

Here is my first thought on things.

I just created a twitter account, I know its a long time.  But since I created my blog with the idea of wanting to get the word out about what I think.  I decided to go searching for a twitter control for the BlogEngine.net.  Which I found one at Codeplex, it is called BlogEngine.NET Twitter Extension.  I went to look at the application and I was satisfied, but I only found that the application just gives the latest feeds of what you published in twitter.  I wanted to give it more extensibility like the JavaScript version of the Twitter badge.

The current Twitter application on codeplex like I said only does the updates without any times, dates or user names.  I decided to upgrade the application to include how long ago the message was sent and a link to the message on twitter.  This took a few hours actually to dive into the old code and produce the new stuff. I hope you like it.

To Download:

Twitter.cs (8.22 kb)

To Install:

Prerequisites
BlogEngine.NET 1.3 (1.2 ?)
How To
1. Download source code. Put the Twitter.cs file in App_Code/Controls folder of your BlogEngine.NET site.
1. Télécharger le code source. Copier le fichier Twitter.cs dans le dossier App_Code/Controls de votre site BlogEngine.NET.
2. Add the following line in the page where the control should appear.
2. Ajouter la ligne suivante dans la page où le contrôle doit s'afficher.
<blog:Twitter ID="Twitter" runat="server" Username="julesss" Password="mypassword" />
Properties
- Username (required) : username of your Twitter account
- Password (required) : password of your Twitter account
- Timeline (optionnal) : updates type to display from Twitter : User, Friends or Public (default : User)
- NumberOfStatuses (optionnal) : number of updates to display (default : 20, which is the maximum recommended by Twitter)

The lines I personally edited is around line 141.

The old code was:

   1:  string innerText = status["text"].InnerText;
 
The New code I inserted is:
 
   1:                      string innerText = "\"" + status["text"].InnerText + "\"" + "<br />"
 + "<a href='http://twitter.com/" + status["user"]["screen_name"].InnerText + "/statuses/" 
+ status["id"].InnerText + "/'>";
   2:                      string TwitterTime = status["created_at"].InnerText;
   3:                      string[] TimeValues = TwitterTime.Split(' ');
   4:                      TwitterTime = TimeValues[1] + " " + TimeValues[2] + " "
 + TimeValues[5] + " " + TimeValues[3] + " GMT";
   5:                      DateTime dt = Convert.ToDateTime(TwitterTime);
   6:                      innerText += GetRelativeDate(dt);
   7:                      innerText += "</a>";

I also inserted a new method that allows for the relative time to be found.

   1:          /// <summary>
   2:          /// gets the relevant time of the current twitter user.
   3:          /// </summary>
   4:          /// <param name="date"></param>
   5:          /// <returns></returns>
   6:          private String GetRelativeDate(DateTime date)
   7:          {
   8:              DateTime now = DateTime.Now;
   9:              TimeSpan span = now - date;
  10:              if (span <= TimeSpan.FromSeconds(60))
  11:              {
  12:                  return span.Seconds + " seconds ago";
  13:              }
  14:              else if (span <= TimeSpan.FromMinutes(60))
  15:              {
  16:                  if (span.Minutes > 1)
  17:                  {
  18:                      return "about " + span.Minutes + " minutes ago";
  19:                  }
  20:                  else
  21:                  {
  22:                      return "about a minute ago";
  23:                  }
  24:              }
  25:              else if (span <= TimeSpan.FromHours(24))
  26:              {
  27:                  if (span.Hours > 1)
  28:                  {
  29:                      return "about " + span.Hours + " hours ago";
  30:                  }
  31:                  else
  32:                  {
  33:                      return "about an hour ago";
  34:                  }
  35:              }
  36:              else
  37:              {
  38:                  if (span.Days > 1)
  39:                  {
  40:                      return "about " + span.Days + "days ago";
  41:                  }
  42:                  else
  43:                  {
  44:                      return "about a day ago";
  45:                  }
  46:              }
  47:          }

I hope you like it as much as I do.  By the way, did I tell you its written all in ASP.NET and C#.  There's another fine example of a GREAT language!

kick it on DotNetKicks.com

Comments (5) -

mikedopp
mikedopp
3/14/2008 12:17:54 PM #

Have you been able to get this to work and where would I put the line:
<blog:Twitter ID="Twitter" runat="server" Username="julesss" Password="mypassword" />  
What I mean by put the line is in what page?
Thanks,
  mikedopp

Scott
Scott
3/14/2008 5:55:08 PM #

As you can see it does work on my page.

If your using Blogengine, most like ou can put the line where I put mine.  I put mine in the themes/"Name of the theme"/site.master page.  I just put it as a new panel on the right column and it works perfectly.

Wayne
Wayne
5/4/2009 2:49:43 PM #

How about an extension that will, once a week, reach out to twitter and compile all the tweets into a single post, and post it to the blog?

If I can't find this, I'm going to write it.  Just checking around now for something already done.  Seen anything?

Scott
Scott
5/5/2009 7:13:14 AM #

@Wayne,  No sorry, I haven't.  Its a good idea though.

Heather Floyd - Web Developer
Heather Floyd - Web Developer
7/22/2009 10:04:54 AM #

Thanks for this. The Twitter widget on my install wasn't working, but this works great.

The only thing I would consider adding would be for the inline links to be converted to clickable (nofollow) links inside the posts.