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