Anders Hesselbom
Programmerare, skeptiker, sekulärhumanist, antirasist. Författare till bok om C64 och senbliven lantis. Röstar pirat.
2009-08-16
I had a business case to display a Twitter feed on an ASP3 (VBScript based) page. The code contains a few sticky passages, but all in all it went very well many thanks to the enormous COM toolkit that Microsoft provides. This is how it can be done:
<%
'Create and configure the DOM Document.
Set D = Server.CreateObject("MSXML2.DOMDocument.3.0")
D.async = False
D.setProperty "ServerHTTPRequest", True
D.validateOnParse = True
D.preserveWhiteSpace = False
'Download XML data (pointing to the user's RSS feed).
Rss="http://twitter.com/statuses/user_timeline/33177615.rss"
If Not D.Load(Rss) Then
'Download failed.
Response.write "<p>We can not connect to Twitter right now.</p>"
Else
'Downloading went well. Extract the tweets.
Set TweetList = D.getElementsByTagName("item")
'Keep track on tweet count - I want to display five of them.
TweetCount = 0
For Each TweetNode In TweetList
TweetCount = TweetCount + 1
'Extract the items from the tweet node.
Dt = TweetNode.childNodes(2).text
Text = TweetNode.childNodes(1).text
'If I want it, the feed contains the URL to the tweet.
Url = TweetNode.childNodes(4).text
'This is one of the ugly bits - I don't parse the date, I just grab the part that I want.
Dt = Server.HtmlEncode(Mid(Dt,1,22))
'The tweet starts with my username followed by a colon. I don't want that either.
Text = Server.HtmlEncode(Mid(Text,13,Len(Text)))
'Display it!
Response.Write "<p><b>" & Dt & "</b><br />" & Text & "</p>"
'And just quit when five tweets are displayed.
If TweetCount>=5 Then
Exit For
End If
Next
'Release reference to tweet list.
Set TweetList = Nothing
End If
'Release reference to DOM object.
Set D = nothing
%>
This code does not take care of URL detection and such things. I will look into that too (here).
Categories: General
If this is your first comment, it will be moderated before it is shown.
Leave a Reply