Drafts Action Directory

Dark Sky Forecast

Posted by agiletortoise, Last update 29 days ago

Example action demonstrates communication with the DarkSky.net API. To use this example you will need a Dark Sky API key, which can be acquired by signing up for a free account at the Dark Sky API site.

The first time this action is run, you will be prompt to add a Dark Sky API key, which will be remembered in Credentials for future use.

This example calls the Dark Sky Forecast API for the current location, and displays a prompt with some key information from the forecast data. Additional data is available, this is only meant as an example of the possibilities - see API documentation for additional details.

The action also offers a link to open the full forecast at the Dark Sky website.

Install

Steps

  • script

    // set personal API key credential
    let credential = Credential.create("Dark Sky API Key", "Enter your Dark Sky API key, which can be copied from the darksky.net developer console page after creating a free account.");
    
    credential.addTextField("key", "API Key");
    credential.authorize();
    let apiKey = credential.getValue("key");
    
    // method to fetch forecast data
    let getForecast = (latitude, longitude) => {
    	// build URL to Dark Sky API forecast endpoint
    	let endpoint = `https://api.darksky.net/forecast/${apiKey}/${latitude},${longitude}`;
    
    	// create and make HTTP request
    	let http = HTTP.create();
    	let response = http.request({
    		"url": endpoint,
    		"method": "GET"
    	});
    
    	// check for a success 200 response
    	if (response.statusCode != 200) {
    		context.fail();
    		console.log("Dark Sky Error: " + response.error);
    		return null;
    	}
    
    	// parse response JSON to object
    	let data = JSON.parse(response.responseText);
    	if (!data) {
    		context.fail();
    		console.log("Dark Sky Error: Unable to parse response");
    		return null;
    	}
    
    	// return the forecast data object
    	return data;	
    }
    
    let lat = draft.processTemplate("[[latitude]]")
    let lon = draft.processTemplate("[[longitude]]");
    let darkSkyURL = `https://darksky.net/forecast/${lat},${lon}`;
    
    let data = getForecast(lat, lon);
    if (data) {
    	// See details on all information in response at API docs
    	// https://darksky.net/dev/docs
    	let d = [];
    	d.push(`Temperature: ${data.currently.temperature}`);
    	d.push(`Summary: ${data.currently.summary}`);
    	d.push(`Expected: ${data.minutely.summary}`);
    	d.push(`Today's High: ${data.daily.data[0].temperatureHigh}`);
    	d.push(`Today's Low: ${data.daily.data[0].temperatureLow}`);
    
    	let p = Prompt.create();
    	p.title = "Dark Sky Forecast";
    	p.message = d.join("\n");
    
    	p.addButton("Open at DarkSky.net");
    	if (p.show()) {
    		app.openURL(darkSkyURL, true);
    	}
    }
    else {
    	alert("Unable to fetch Dark Sky forecast information. Check action log for details");
    }

Options

  • After Success Default
    Notification Info
    Log Level Info

Comments

Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.