property since_id : 0

property hashtag : 0


on run

--display dialog "running"

set hashtag to my urlencode(text returned of (display dialog "Which hashtag or search term would you like to follow?" default answer "Boosh") as string)

tell application "GrowlHelperApp"

-- Make a list of all the notification types 

-- that this script will ever send:

set the allNotificationsList to ¬

{"gt"}

-- Make a list of the notifications 

-- that will be enabled by default.      

-- Those not enabled by default can be enabled later 

-- in the 'Applications' tab of the growl prefpane.

set the enabledNotificationsList to ¬

{"gt"}

-- Register our script with growl.

-- You can optionally (as here) set a default icon 

-- for this script's notifications.

(register as application ¬

"gt" all notifications allNotificationsList ¬

default notifications enabledNotificationsList ¬

)

end tell

set myR to showlatest()

end run


on idle

set myR to showlatest()

return 60

end idle


on showlatest()

set tcontents to {}

set tnames to {}

set imageurl to {}

set turl to "http://search.twitter.com/search.atom?q=" & hashtag & "&since_id=" & since_id

--display dialog "url" default answer turl

set xmlData to do shell script "curl" & " " & "'" & turl & "'"

tell application "System Events"

set xmlDocument to make new XML data with data xmlData

tell contents of xmlDocument

set rootElement to first XML element

set n to name of rootElement

set idElements to XML elements of XML element n of xmlDocument whose name is "entry"

if ((count of idElements) = 0) then return ""

repeat with idElement in idElements

set the end of tcontents to value of (XML element "content" of idElement)

set the end of tnames to value of (XML element "name" of XML element "author" of idElement)

set links to (XML elements of idElement whose name is "link")

repeat with link in links

if (value of XML attribute "rel" of link) = "image" then set the end of imageurl to (value of XML attribute "href" of link)

end repeat

end repeat

set z to tcontents & return & imageurl

set lastid to value of XML element "id" of item 1 of idElements as text

try

set oldDelims to AppleScript's text item delimiters -- save their current state

set AppleScript's text item delimiters to {":"} -- declare new delimiters

-- do script steps here

set tsince_id to last text item of lastid

--display dialog tsince_id

copy tsince_id to since_id

set AppleScript's text item delimiters to oldDelims -- restore them

on error

set AppleScript's text item delimiters to oldDelims -- restore them in case something went wrong

end try

end tell

repeat with i from (count of tcontents) to 1 by -1

set theFile to my getTwitterImage(item i of tnames, item i of imageurl)

tell application "GrowlHelperApp"

set message to my remove_markup(item i of tcontents)

notify with name "gt" title "gt" description message application name "gt" image from location theFile

end tell

end repeat

end tell

end showlatest


on getTwitterImage(theName, theURL)

set filename to ""

set the okstring to "abcdefghijklmnopqrstuvwxyz"

repeat with thisChar in theName

set x to the offset of thisChar in the okstring

if x is not 0 then

set the filename to (the filename & character x of the okstring) as string

end if

end repeat

set theFile to "/tmp/" & filename & ".jpg" as string

tell application "Finder"

if exists (theFile) as POSIX file then

--the file has already been downloaded

--

else

do shell script "curl " & theURL & ">/tmp/" & filename & ".jpg"

end if

end tell

return theFile

end getTwitterImage



on remove_markup(this_text)

set copy_flag to true

set the clean_text to ""

repeat with this_char in this_text

set this_char to the contents of this_char

if this_char is "<" then

set the copy_flag to false

else if this_char is ">" then

set the copy_flag to true

else if the copy_flag is true then

set the clean_text to the clean_text & this_char as string

end if

end repeat

return the clean_text

end remove_markup


on urlencode(theText)

set theTextEnc to ""

repeat with eachChar in characters of theText

set useChar to eachChar

set eachCharNum to ASCII number of eachChar

if eachCharNum = 32 then

set useChar to "+"

else if (eachCharNum  42) and (eachCharNum  95) and (eachCharNum < 45 or eachCharNum > 46) and (eachCharNum < 48 or eachCharNum > 57) and (eachCharNum < 65 or eachCharNum > 90) and (eachCharNum < 97 or eachCharNum > 122) then

set firstDig to round (eachCharNum / 16) rounding down

set secondDig to eachCharNum mod 16

if firstDig > 9 then

set aNum to firstDig + 55

set firstDig to ASCII character aNum

end if

if secondDig > 9 then

set aNum to secondDig + 55

set secondDig to ASCII character aNum

end if

set numHex to ("%" & (firstDig as string) & (secondDig as string)) as string

set useChar to numHex

end if

set theTextEnc to theTextEnc & useChar as string

end repeat

return theTextEnc

end urlencode