Startsidan  ▸  Texter  ▸  Teknikblogg

Anders Hesselbom

Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.

Link detection in my Twitter script

2009-08-23

I’ll just get on with my ASP3 Twitter reader written in VBScript so that I can leave the past behind me again. The first part is here.

VBScript version 5 has simple and good Regex support. Some features are missing, but it’s good. It is available through a class called RegExp, like so:

Set R=New RegExp

This is the basic concept (to run it, just add these lines and the line above to a vbs-file):

'What do I want to search for? Use the Pattern property.
R.Pattern="a"

'Case insensitive.
R.IgnoreCase=True

'Do the replacement and show the result in a message box.
MsgBox R.Replace("String to search", "(new value)")

'Case insensitive.
R.IgnoreCase=True

'Do the replacement and show the result in a message box.
MsgBox R.Replace("String to search", "(new value)")

To do the detection, I’ll use this script, downloaded from here:

(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?

And here is the working code:

<%
'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

 'Create the Regex object and set the pattern.
 Set R = New RegExp
 R.Pattern = "(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?"

 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)))

  'Link detection!
  Set Matches = R.Execute(Text)
  If Matches.Count > 0 Then
   For I = 0 To Matches.Count - 1
    Text = R.Replace(Text, "<a href=""" & Matches(I) & """>" & Matches(I) & "</a>")
   Next
  End If

  '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
%>

Categories: General

Tags: ASP3, Twitter

Leave a Reply

Your email address will not be published. Required fields are marked *



En kopp kaffe!

Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!

Bjud på en kopp kaffe!

Om...

Kontaktuppgifter, med mera, finns här.

Följ mig

Twitter Instagram
GitHub RSS

Public Service

Folkbildning om public service.

Hem   |   linktr.ee/hesselbom   |   winsoft.se   |   80tal.se   |   Filmtips