Dynamically update Twitter part 2

10. June 2009

Following my previous article on Dynamically updating Twitter with Twitterizer and TinyURL Edward Lewis suggested I use bit.ly to shrink the URL. So here is the code to do that:

Public Shared Function MakeBitlyUrl(ByVal Url As String) As String
Try
If Url.Length <= 30 Then
Return Url
End If

If Not Url.ToLower().StartsWith("http") AndAlso Not Url.ToLower().StartsWith("ftp") Then
Url = "http://" & Url
End If

Dim request As Net.HttpWebRequest = WebRequest.Create("http://api.bit.ly/shorten?version=2.0.1&longUrl=" + HttpUtility.UrlEncode(Url) + "&login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07")
Dim Response As WebResponse = request.GetResponse
Dim res = request.GetResponse()
Dim sr As StreamReader
sr = New StreamReader(Response.GetResponseStream)
Dim text As String = sr.ReadToEnd
sr.Close()

Dim indexOfBefore As Integer = text.IndexOf("shortUrl"": """) + 12
Dim indexOfAfter As Integer = text.IndexOf("""", indexOfBefore)
Dim shortURL As String = text.Substring(indexOfBefore, indexOfAfter - indexOfBefore)

Return shortURL
Catch generatedExceptionName As Exception
Return Url
End Try
End Function

Thanks also to Using bit.ly URL Shortening From ASP.NET (REST/JSON) for a great code example.

.Net ,

Dynamically update Twitter

2. June 2009

Further to my previous post Twitter API and .NET I have been working on a way to dynamically update my Twitter status from my .NET web application. To get this to work I have used Twitterizer and TinyURL.

Twitterizer makes it very easy to implement. I also wanted to make sure I could also shrink the urls so I used TinyURL for this.

To start the vb class you will need the following:

Imports Twitterizer.Framework

Public Class TwitterUtils

Private Shared twitter_username As String = "YourTwitterUserName"
Private Shared twitter_password As String = "YourTwitterPassword"

The VB code for shrinking the url with TinyURL:

Public Shared Function MakeTinyUrl(ByVal Url As String) As String
Try
If Url.Length <= 30 Then
Return Url
End If
If Not Url.ToLower().StartsWith("http") AndAlso Not Url.ToLower().StartsWith("ftp") Then
Url = "http://" & Url
End If

Dim request As Net.HttpWebRequest = WebRequest.Create("http://tinyurl.com/api-create.php?url=" & Url)
Dim Response As WebResponse = request.GetResponse
Dim res = request.GetResponse()

Dim sr As StreamReader
sr = New StreamReader(Response.GetResponseStream)
Dim text As String = sr.ReadToEnd
sr.Close()
Return text
Catch generatedExceptionName As Exception
Return Url
End Try
End Function

To update my status on Twitter I used the following code:

Public Shared Sub updateStatus(ByVal post As String)
Dim RegexObj As Regex = New Regex("(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?")
Dim t As New Twitter(twitter_username, twitter_password)
t.Status.Update(RegexObj.Replace(post, TwitterUtils.MakeTinyUrl(Regex.Match(post, "(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?").Groups(0).ToString)))
End Sub

As you can see I used a regular expression to replace the url in the text with a TinyURL.

You may want to have a textbox and a seperate url field. To submit these you could use the following code:

Public Shared Sub updateStatus(ByVal post As String, Optional ByVal url As String = "")
Dim new_status As String = String.Empty
If Not url Is Nothing Then
If post.Length > 114 Then
post = post.Substring(0, 114)
End If
new_status += post & " " & MakeTinyUrl(url)
Else
new_status += post
End If

Dim t As New Twitter(twitter_username, twitter_password)
t.Status.Update(new_status)
End Sub

Twitterizer makes it simple to update your status using .NET. I have only used it for updating my status but you can use it for Direct Messages and Replies. Why not download it and start using it in your own web applications.

If you are on Twitter leave your username here for others to follow.

.Net ,

Twitter api and .NET

2. May 2009

I have been working on implementing the Twitter api into a .Net website. After some searching I discovered Twitterizer. It is a .Net dll that you can include in your project. This has made it easy for me to update my status dynamically. The problem I did find was that the URL posted was not being shrunk.

So using tinyurl I was able to include some code to convert the url to a tinyurl and post it. I found this article Shortening URLs Using TinyURL API in .NET and using the code provided I was able to shorten the url for posting.

I am currently demoing this to a client and will keep you updated on my progress.

.Net ,

Valid XHTML Dynamic List Instead Of Using A Repeater

30. January 2009

Repeater controls are great for displaying lists. But these the lists displayed are not valid XHTML. To make sure my pages are valid I have had to use a workaround.

For my events list I created a shared function, in case I need to reuse it.

Public Shared Function getOrderedEventsList (ByVal ds As DataSet) As String
'take dataset and return an ordered list string that is valid XHTML
Dim strResult As String = String.Empty
strResult += "<ol>"
Dim dr As DataRow
For Each dr In ds.Tables(0).Rows
strResult += "<li>" & dr("EventTitle") & "</li>"
Next
strResult += "</ol>"
Return strResult
End Function

Using an empty div and from the code behind I am filling it using InnerHtml.

divName.InnerHtml = getOrderedEventsList(dataset)

You can use this for any dynamic list instead of a repeater. And your code will be valid XHTML.

.Net ,

Accessible Forms In .Net

7. January 2009

For an accessible web form one of the requirements is that input fields require explicit descriptions. The "for" attribute in the label element must be associated to the input id.

With Visual Studio this can be difficult to achieve. When using master pages Visual Studio renames the ids of the of the input fields to ensure they are unique within the master page. This means that the label tags are no longer associated with input fields.

To resolve this issue give the labels an id and runat="server" then in the Page Load put

labelid.Attributes.Add("for", textboxid.ClientID)

This fixes the accessibility issue of input fields having labels associated with them.

.Net, Accessibility ,

Web Hosting

1. January 2009

If you are looking for reliable and affordable we hosting for your small business website then I recommend 123-reg. I recommend them to my UK customers because they provide excellent service at a very affordable price. I have also used them myself for websites I have created.

For .Net hosting solutions I recommend Webhost4life. They are based in America but they provide everything you need for your .Net based website solutions. Following the link to Webhost4life gives me referrer bonus. So please be kind and click it and sign up with them.

.Net, General ,



Marketing & SEO Blogs - Blog Top SitesBlog Flux Directory Marketing / SEO Top Blogs Internet Marketing & SEO (Link Building) - TOP.ORG Add to Technorati Favorites TopOfBlogs