Need help updating Geektool weather geeklet to use Dark Sky API

Years ago, Brett Terpstra created a great weather and forecast geeklet for Geektool that put the current conditions and forecast on your desktop. It worked great until recently when the data source, Weather Underground, began to shut down its API. At the moment, only the current conditions part of the geeklet is working.

So I was thinking that perhaps I could replace the WU API with the Dark Sky API. The problem is that I don’t know how to parse the JSON file and I don’t know ruby (which is what Brett’s original scripts are written in). So, I turn to you all for help.

Any suggestions on how I could get started or perhaps a different way to “skin the cat”? All I really need is the extended forecast in a text file, so maybe I can get the data from Dark Sky using AppleScript or something else? Any thoughts or ideas are appreciated. Thanks.

If you put the JSON file into this viewer, https://codebeautify.org/jsonviewer, it may help you decipher the data tree.

1 Like

As I dig into this, I realize how out of my depth I am. I need a much greater knowledge of ruby and JSON than I do now in order to execute this. I guess I will wait to see if another solution presents itself.

I use AppleScript to parse weather data in Json format from the WU API. I still get all the weather information, so including the forecasts. I’m in the process of rewriting my program to be able to switch to the Dark Sky API. At this moment I can process the current weather from Dark Sky and display it on the screen of my Mac (using Übersicht). Processing the forecast is much harder to do with the data from Dark Sky than that of WU.

I know of two ways to parse Json and use it in AppleScript.

One is an utility called JSON Helper from http://www.mousedown.net/mouseware/JSONHelper.html
I use JSON Helper to do the most of the work now.

And the other is a command line utility you can install with Brew called JQ.
After installing that you can extract the information with a do shell script command from the Json file.
For more information
https://stedolan.github.io/jq/

If you need code examples for either, let me know.

Thanks FrankV, Any examples you could share would be most appreciated. And thank you for the explanation.

So I tried to use JSON helper, but I’m running into a snag trying to iterate through the days of the week in the Dark Sky API. I’m getting errors about the file already being open and about the repeat loop. Here’s what I have so far:

tell application "Finder"
	set theFile to file "Macintosh HD:Users:domenicobettinelli:Documents:sevendayforecast.txt"
	set theFile to theFile as string
	set theOpenedFile to open for access file theFile with write permission
	end tell
	tell application "JSON Helper"
		set weatherdata to fetch JSON from "https://api.darksky.net/forecast/APIKEY/LATITUDE,LONGITUDE"
	
	set i to 0
	set b to 1
	repeat with i from 0 to 7
	set dailySummary to summary of item b of |data| of daily of weatherdata as string
	set dailyTempLow to temperatureLow of item b of |data| of daily of weatherdata as string
	set dailyTempHigh to temperatureHigh of item b of |data| of daily of weatherdata as string
	if i = 0
	set dailyDay to weekday of (current date)
	set eof of theOpenedFile to 0
	write dailyDay & " : " & dailySummary & " HI: " & dailyTempHigh & "F, LO: " & dailyTempLow & "F" & return starting at eof
	else
	if i = 1 then
	set dailyDay to weekday of (current date) + 1
	write dailyDay & " : " & dailySummary & " HI: " & dailyTempHigh & "F, LO: " & dailyTempLow & "F" & return starting at eof
	endif
	endif
	set i to i + 1
	set b to b + 1
	end repeat
end tell

The end result should be a text file with 8 lines with the day name (ex. Monday), the summary, and highs and lows and that file would be overwritten each time the script runs. Any ideas? Thanks.

I must say that you came very far in one day. Looks like you have experience with these kind of things. It took me much longer to understand how I had to parse the weather information with help of JSON Helper.

I changed your program the following way:

set theFile to "Macintosh HD:Users:domenicobettinelli:Documents:sevendayforecast.txt"


tell application "JSON Helper"
	set weatherdata to fetch JSON from "https://api.darksky.net/forecast/APIKEY/LATITUDE,LONGITUDE"
end tell



set i to 0
set b to 1
repeat with i from 0 to 7
	set dailySummary to summary of item b of |data| of daily of weatherdata as string
	set dailyTempLow to temperatureLow of item b of |data| of daily of weatherdata as string
	set dailyTempHigh to temperatureHigh of item b of |data| of daily of weatherdata as string
	if i = 0 then
		set dailyDay to weekday of (current date)
		set thisText to dailyDay & " : " & dailySummary & " HI: " & dailyTempHigh & "F, LO: " & dailyTempLow & "F" & return as string
		writeToFile(thisText, theFile, false)
	else
		set dailyDay to (weekday of ((current date) + i * days))
		set thisText to dailyDay & " : " & dailySummary & " HI: " & dailyTempHigh & "F, LO: " & dailyTempLow & "F" & return as string
		writeToFile(thisText, theFile, true)
	end if
	set i to i + 1
	set b to b + 1
end repeat


--- Handler that writes text to the file
---  boolAppend : boolean -- True append to existing file. False overwrite existing file

on writeToFile(theText, theFile, boolAppend)
	local theText, theFile, boolAppend, otf
	try
		set theFile to theFile as string
		set otf to open for access file theFile with write permission
		if boolAppend is false then set eof of otf to 0
		write theText to otf as «class utf8» starting at eof
		close access otf
		return true
	on error eMsg number eNum
		try
			close access file otf
		end try
		error "Can't writeToFile: " & eMsg number eNum
	end try
end writeToFile

The resulting weather forecast (for Amsterdam):
Thursday : Mostly cloudy until evening. HI: 64,5F, LO: 50,47F
Friday : Clear throughout the day. HI: 70,33F, LO: 50,17F
Saturday : Mostly cloudy throughout the day. HI: 70,39F, LO: 50,21F
Sunday : Mostly cloudy throughout the day. HI: 61,64F, LO: 43,13F
Monday : Partly cloudy until afternoon. HI: 64,18F, LO: 47,78F
Tuesday : Clear throughout the day. HI: 69,93F, LO: 54,24F
Wednesday : Clear throughout the day. HI: 74,2F, LO: 57,39F
Thursday : Mostly cloudy starting in the evening. HI: 74,51F, LO: 54,55F

Thank you! I’ve been dabbling at Applescript for years (mainly cribbing other people’s code to hack things together) and I do know HTML and CSS so structured documents aren’t completely foreign. I did take some educated guesses at the parsing based on the JSON Helper examples (and a bit of luck). I’ll try this out and see if it works and then report back here.

Okay, yes, it works! I put it in a geeklet with the command cat /Users/domenicobettinelli/Documents/sevendayforecast.txt and this is the result:

I’ll call the Applescript using a timed trigger in Keyboard Maestro.

1 Like

You’re welcome, glad you got it working. I learned programming in AppleScript because of my weather program. It started as a shell script for Geektool someone made a couple of years ago, it stopped working because of a change to the Yahoo Whether API. So I decided to write my own in AppleScript using WU instead. Now I need to change it again, this time to Dark Sky. I also use Keyboard Maestro to regularly run it.

1 Like