monkinetic weblog

Steve Ivy's Weblog - Since 1999 - XII Ed.

RSSPuller Script

RSSPuller script:


global rssFile
set rssFile to alias "Sandbox:Users:SteveIvy:TheHub:rssSources"
global tmpFile

--return rssFile

set tmpFilePath to "FireStorm:tmp:rssTmpFile"

tell application "BrainForest"
    open {rssFile}
    
    set rsf to the frontmost document
    
    set rssSources to (the tree items of rsf whose done is true) as list
    
    repeat with sourceNode in rssSources
        -- all sources should be actions so we can check them on or off
        if the action of sourceNode is false then set the action of sourceNode to true
        
        -- clean up
        
        if (count of the tree items of sourceNode) is not 0 then
            repeat (the count of the tree items of sourceNode) times
                if the (count of (the tree items of (tree item 1 of sourceNode) as list)) is 0 then
                    tell rsf to delete tree item 1 of sourceNode
                else
                    -- BIG TIME CRASHER HERE
                    -- this BREAKS the MAC
                    -- if we do this without deleting subnodes first
                    
                    -- delete subnodes first
                    repeat (the count of the tree items of (tree item 1 of sourceNode)) times
                        tell rsf to delete tree item 1 of (tree item 1 of sourceNode)
                    end repeat
                    -- then delete the node
                    tell rsf to delete tree item 1 of sourceNode
                end if
            end repeat
        end if
        
        set the_url to the note of sourceNode
        
        -- fetch the rss source
        with timeout of 30 seconds
            tell application "URL Access Scripting"
                --return "" & the_url & ""
                download the_url to file tmpFilePath replacing yes
            end tell
        end timeout
        tell application "URL Access Scripting" to quit
        
        set xmlStr to ""
        -- read in xml contents
        open for access file tmpFilePath
        set xmlStr to (read file tmpFilePath) --to (get eof file tmpFilePath))
        close access file tmpFilePath
        
        
        set xmlData to parse XML (xmlStr)
        
        --return xmlData
        set rssCh to ""
        
        repeat with anElement in the XML contents of xmlData
            if the XML tag of anElement is "channel" then
                set rssCh to anElement
                exit repeat
            end if
        end repeat
        
        if rssCh is not "" then
            set rssCh to the XML contents of rssCh
            
            set the start date of sourceNode to the current date
            set the due date of sourceNode to the current date
            
            repeat with anElement in rssCh
                if the XML tag of anElement is "item" then
                    set rssItemData to (the XML contents of anElement)
                    repeat with itemBit in rssItemData
                        if the XML tag of itemBit is "title" then set theTitle to the XML contents of itemBit as string
                        if the XML tag of itemBit is "link" then set theLink to the XML contents of itemBit as string
                        if the XML tag of itemBit is "description" then set theDesc to the XML contents of itemBit as string
                    end repeat
                    
                    --now we insert it into brainforest
                    tell rsf to set newNode to make new tree item at end of sourceNode with properties {title:theTitle & " - " & theDesc}
                    tell rsf to set linkNode to make new tree item at end of newNode with properties {title:theLink}
                    set the expanded of newNode to false
                end if
            end repeat
        else
            error "puke"
        end if
        
        set theTitle to ""
        set theLink to ""
        set theDesc to ""
        
        set the expanded of sourceNode to false
    end repeat
    activate
    say "RSS Sources updated"
end tell

My name is Steve Ivy and I write about technology, the open web, social software, and general nerdity on monkinetic.com. You should follow me on Twitter or subscribe to this blog if you like what you're reading. I spend my days hacking Movable Type, python, Django, and various other efforts at Wallrazer. This is my personal site.