20/07/07

AppleScript Météo-France pour GeekTool et Growl

AppleScript (météo pour GeekTool et Growl)

Dans ce précédent billet : AppleScript Météo-France pour GeekTool, je m'initiais doucement à curl[1], bon, il est vrai à la réflexion qu'il n'y a rien de sorcier ; le plus complexe, à mon sens, étant de parser la source HTML pour y récupérer les informations pertinentes.

J'ai aussi essayé de corriger les défauts que j'énonçais à la fin du précédent billet, aussi, maintenant, le script se base sur les pages Prévisions ville de Meteo France[2], ce qui rend la configuration et l'utilisation du script beaucoup plus aisée.

Capture d'écran

capture d'écran notifications Growl, AppleScript (météo pour GeekTool)

Fonctionnement et utilisation

Le parsage de la source HTML est exclusivement effectué avec des AppleScript's text item delimiters, c'est sûrement sommaire ; mais, comme l'illustre la capture d'écran, la quantité d'information récupérée est somme toute assez satisfaisante.

Le script pourra être exécuté à intervalle régulier grâce à GeekTool, un rafraîchissement de 600 secondes est un minimum acceptable. Pour mémoire, la commande nécessaire à GeekTool :

osascript "/path/to/weather.scpt"

Le script ne prévoit aucune sortie sous forme de texte si tout s'est bien passé, voir le précédent billet pour quelques explications à ce sujet ; debug à true permet l'affichage de quelques informations sous cette forme :

Créteil (94)
20/07/07 18:09:05 : begin of weather.scpt
20/07/07 18:09:06 : end of weather.scpt

Ville et, date de début et de fin de script.

Si debug est à false[3], seules d'éventuelles erreurs seraient retournées.

weather.scpt

(*    
     weather.scpt
     HP <http://blogosx.jupiterii.com>
  
     updated Jul. 20, 2007
 *)
 
property LIEUID : "FR94028"
property myfolder : "/Users/path/to/folder"
 
property scriptName : "weather.scpt"
property empty : ""
 
--debug
property debug : true
 
on run
	growlregister(scriptName, "GeekTool")
	
	if debug then set begin to timestamp("begin of " & scriptName) & return
	
	set page to "http://www.meteofrance.com/FR/mameteo/prevVille.jsp?LIEUID=" & LIEUID
	set html to curl4content(page)
	
	if first word of html is not "curlbug" then
		try
			(* parse the result of curl4content *)
			
			--get img
			set img to string2get("<img src=\"/img/picto/gd/mf/", html, 2)
			set img to string2get("\"", img, 1)
			set wURL to "http://" & (string2get("/", page, 3)) & "/img/picto/cte/grd/" & img
			
			--ex: 'vendredi à 20h:Belles éclaircies' 
			set prev to string2get("<a href=\"#\" onMouseOver=\"showAlt('", html, 2)
			set prev to string2get("'", prev, 1)
			set prev to replaceString(prev, ":", " : ")
			
			--temp
			set mini to first word of (string2get("mini ", html, 2)) & "°"
			set maxi to first word of (string2get("maxi ", html, 2)) & "°"
			
			--add mini & maxi to prev
			set prev to prev & return & "mini : " & mini & " / maxi : " & maxi
			
			--title
			set title to string2get("<title>", html, 2)
			set title to string2get("</title>", title, 1)
			set title to string2get(":", title, 2)
			set title to (title & space & "(" & (item 3 of LIEUID) & (item 4 of LIEUID) & ")")
			
			--remove old pic and curl the new one (no string returned if property debug is false)
			rm2curl(myfolder, wURL)
			
			--notif
			makenotif(title, prev, LIEUID)
			--debug
			if debug then title & return & (begin) & timestamp("end of " & scriptName)
		on error bug
			beep
			--error displayed with some useful information
			return timestamp(((POSIX path of (path to me)) as string) & space & bug)
		end try
	end if
end run
 
--functions
 
on curl4content(page)
	try
		return do shell script "curl" & space & page & space & "| iconv -f 'iso-8859-1' -t 'macroman'"
	on error bug
		beep
		return "curlbug:" & return & bug
	end try
end curl4content
 
on replaceString(theText, oldString, newString)
	set text item delimiters to oldString
	set tempList to every text item of theText
	set text item delimiters to newString
	set theText to the tempList as string
	set text item delimiters to empty
	return theText
end replaceString
 
on string2get(delim, istring, pos)
	set text item delimiters to delim
	set temp to every text item of istring
	set text item delimiters to empty
	return ((item pos) of temp)
end string2get
 
on rm2curl(myfolder, wURL)
	global begin, scriptName, LIEUID
	try
		--remove old pic (try because no verification before)
		do shell script "rm " & quoted form of ((myfolder) & "/weather-" & LIEUID & ".gif")
	end try
	do shell script "curl " & quoted form of wURL & " >" & quoted form of ((myfolder) & "/weather-" & LIEUID & ".gif")
end rm2curl
 
on timestamp(output)
	return (do shell script "date '+%d/%m/%y'") & space & (time string of (current date)) & " : " & output
end timestamp
 
-- functions Growl related
 
on growlrunornot()
	tell application "System Events"
		set isRunning to ¬
			(count of (every process whose name is "GrowlHelperApp")) > 0
	end tell
end growlrunornot
 
on growlregister(myApp, appicon)
	if growlrunornot() then
		tell application "GrowlHelperApp"
			set the allNotificationsList to {"Notification"}
			set the enabledNotificationsList to {"Notification"}
			register as application myApp all notifications allNotificationsList ¬
				default notifications enabledNotificationsList ¬
				icon of application appicon
		end tell
	end if
end growlregister
 
on makenotif(mytitle, prev, LIEUID)
	global scriptName, myfolder
	
	if growlrunornot() then
		tell application "GrowlHelperApp"
			-- Send a Notification
			notify with name ¬
				"Notification" title ¬
				mytitle description ¬
				prev application name ¬
				scriptName image from location ¬
				"file://" & ((myfolder) & "/weather-" & LIEUID & ".gif")
		end tell
	end if
end makenotif

La propriété LIEUID s'obtient très facilement en visitant le site de Meteo France. Il faut renseigner myfolder pour choisir où (chemin) curl sauvegardera l'image… et c'est tout.

Notes

[1] cURL - Tutorial

[2] exemple : Prévisions ville: Créteil

[3] ce qui correspond à une utilisation courante

RSS commentaires

  1. G
  2. W
  3. Y
  4. G
  5. N
  6. B

Pour valider le formulaire, saisissez les 3 premiers caractères de la série

  • caca
  • confused
  • cry
  • dizzy
  • happy
  • lol
  • neutral
  • omg
  • sad
  • smile
  • tongue
  • wink