Subversion Repositories Jaiku

[/] [JaikuGrowler/] [JaikuGrowler.applescript] - Rev 21

Compare with Previous | Blame | View Log

-- JaikuGrowler 0.4, April 3, 2007
 
--Sets up Growl (growl.info) to notify you every time that new Jaikus (jaiku.com) are emitted from you or your contacts.
 
--For more information, and updates, see:
--   http://ruk.ca/w/index.php/JaikuGrowler
 
--SYSTEM REQUIREMENTS
 
--1. A Jaiku account (available from free from Jaiku.com)
--2. Your Jaiku screen name and personal key (get your key from http://jaiku.com/api/key)
--3. The Applescript addon "XMLLib osax" (free from http://www.satimage.fr/software/en/downloads_osaxen_pop.html)
--    Follow the installation instructions provided with the download for "XMLLib osax"
 
--USER CONFIGURABLE OPTIONS IN THE SCRIPT
 
--None (just run it and you'll be prompted for your screen name and personal key)
 
--THINGS THE SCRIPT DOES TO YOUR SYSTEM
 
--1. Creates a new folder called "Jaiku" under "~/Library/Application Support" (changed with v0.3 to the user Library, not the system one)
--2. Registers as a Growl application called "Jaiku Growler"
 
--THINGS YOU SHOULD KNOW
 
--Once it starts running, the script checks your Jaiku RSS feed every 5 minutes for an update.  To *stop* the script,
--you'll probably have to select the application (click on its icon in the Dock) and then hit Command + Period.
 
-- (c) 2005 by Reinvented Inc. (www.reinvented.net)
-- Licensed under the GNU Public License
-- http://www.fsf.org/licensing/licenses/gpl.txt 
 
-- This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
 
-- This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 
-- You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
-- We store the settings under ~/Library/Application Support/Jaiku
-- so if this folder doesn't exist, we create it.
 
set the_workingdir to (path to application support from user domain as string) & "Jaiku"
tell application "Finder"
	if not (the_workingdir exists) then
		make new folder at (path to application support from user domain) with properties {name:"Jaiku"}
	end if
end tell
 
-- The date of last RSS item retrieved is stored in a file called LastJaikuDate.
-- If this file exists, we grab the date, otherwise we set the date to 2000-01-01.
 
set the_cachefile to (path to application support from user domain as string) & "Jaiku:LastJaikuDate"
tell application "Finder"
	if the_cachefile exists then
		set JaikuCache to (open for access file the_cachefile)
		set the_lastdate to (read JaikuCache for (get eof JaikuCache))
		tell me to set the_lastdate to date the_lastdate
		close access JaikuCache
	else
		tell me to set the_lastdate to date "Saturday, January 1, 2000 12:00:00 AM"
	end if
end tell
 
-- The settings for our Jaiku RSS feed (username and key) are stored in JaikuSettings
 
set the_settingsfile to (path to application support from user domain as string) & "Jaiku:JaikuSettings"
tell application "Finder"
	if the_settingsfile exists then
		display dialog "Reading Jaiku settings..." buttons {"¥"} default button 1 giving up after 2
		set JaikuSettings to (open for access file the_settingsfile)
		set the_jaikurss to (read JaikuSettings for (get eof JaikuSettings))
		close access JaikuSettings
	else
		display dialog "JaikuGrowler" & return & return & Â
			"This script will periodically read your person Jaiku RSS feed and display any new Jaikus from you and your contacts." & return & return & Â
			"To proceed you'll need your Jaiku Screen Name and  Personal Key" & return & return & Â
			"You can get your Personal Key at:" & return & return & "http://jaiku.com/api/key" buttons {"Cancel", "Continue"} default button 2
 
		display dialog "Enter your Jaiku Screen Name:" & return default answer ""
		set the_screenname to the text returned of the result as string
 
		display dialog "Enter your Jaiku Personal Key. " & return & return & Â
			"You can get your Personal Key at:" & return & return & Â
			"http://jaiku.com/api/key" default answer ""
		set the_personalkey to the text returned of the result as string
 
		set the_jaikurss to "http://" & the_screenname & ".jaiku.com/contacts/feed/rss?user=" & the_screenname & "&personal_key=" & the_personalkey
 
		set JaikuSettings to (open for access file the_settingsfile with write permission)
		write the_jaikurss to JaikuSettings
		close access JaikuSettings
 
	end if
end tell
 
-- We begin interacting with Growl.  Obviously this means that Growl itself
-- needs to be installed.  You can get it for free from http://growl.info/
 
tell application "GrowlHelperApp"
 
	-- Settings for Growl registration
	set the allNotificationsList to {"Jaiku"}
	set the enabledNotificationsList to {"Jaiku"}
 
	-- Register as a Growl application if we haven't already
	register as application Â
		"Jaiku Growler" all notifications allNotificationsList Â
		default notifications enabledNotificationsList Â
		icon of application "Script Editor"
 
	-- And endless loop -- makes the application go "into the background"
 
	repeat while (1 = 1)
 
		-- This will be set to the date of the latest RSS item, and later written out to a file
		set the_mostrecentdate to ""
 
		set the_doc to XMLOpen the_jaikurss
		set the_root to XMLRoot the_doc
		set the_channel to XMLChild the_root index 1
		set the_items to XMLFind the_channel name "item" with all occurrences
 
		--Loop through each of the items in the RSS feed
		repeat with the_item in the_items
 
			set the_title to XMLFind the_item name "title"
			set the_title_text to XMLGetText the_title
 
			set the_guid to XMLFind the_item name "guid"
			set the_guid to XMLGetText the_guid
 
			set the_pubdate to XMLFind the_item name "pubDate"
			set the_pubdate to XMLGetText the_pubdate
 
			--Strip the timezone out of the pubDate -- seems to be required by Applescript by I'm not sure.
			set ODel to AppleScript's text item delimiters
			set AppleScript's text item delimiters to " "
			set the_dateparts to (text items 1 thru 5 of (the_pubdate as string) as list) as string
			set AppleScript's text item delimiters to ODel
 
			tell me to set the_date to date the_dateparts
 
			--If the the_mostrecentdate is empty, then set it to the date of the first (i.e. most recent) item
			if the_mostrecentdate is equal to "" then
				set the_mostrecentdate to the_dateparts
			end if
 
			--If the date of the item we're dealing with is after the date we cached (i.e. it's a new item) then process it
			if the_date is greater than the_lastdate then
 
				set the_user to XMLFind the_item name "user"
				set the_firstname to XMLGetAttribute the_user name "first_name"
				set the_lastname to XMLGetAttribute the_user name "last_name"
				set the_avatar to XMLGetAttribute the_user name "avatar"
				set the_link to XMLGetAttribute the_user name "url"
 
				set the_timesince to XMLFind the_item name "timesince"
				set the_timesince to XMLGetText the_timesince
 
				--Grab and cache the avatar image (Growl only deals with local images, so we need a local copy)
				set ODel to AppleScript's text item delimiters
				set AppleScript's text item delimiters to "/"
				set theImageName to (text item -1 of (the_avatar as string) as list) as string
				set AppleScript's text item delimiters to ODel
 
				set desktopPath to (path to application support from user domain) as string
				set desktopPath to desktopPath & "Jaiku:"
				set targetPath to (POSIX path of ((desktopPath & theImageName) as string))
				set targetPathOut to "'" & targetPath & "'" as string
				set targetPathFile to "file://" & targetPath as string
 
				--Grab the image with cURL
				try
					set theCurlCommand to ("curl -o " & targetPathOut & " " & the_avatar)
					do shell script theCurlCommand
				on error
					set DLStatus to "File could not be downloaded!"
				end try
 
				notify with name Â
					"Jaiku" title the_firstname Â
					description the_title_text Â
					application name Â
					"Jaiku Growler" image from location targetPathFile
			end if
		end repeat
 
		--Update the cache file with the most recent item's date
 
		set JaikuCache to (open for access file the_cachefile with write permission)
		write the_mostrecentdate to JaikuCache
		close access JaikuCache
 
		--The latest RSS item becomes the most recent date
		tell me to set the_lastdate to date the_mostrecentdate
 
		--Wait for 5 minutes (5 x 60 seconds = 300 seconds) before repeating
		delay 300
 
	end repeat
 
end tell
 
 
 

Compare with Previous | Blame | View Log