First commit, 1.0
This commit is contained in:
parent
c77219c2ea
commit
b09deeab20
3 changed files with 66 additions and 1 deletions
13
README.md
13
README.md
|
@ -1,2 +1,13 @@
|
|||
# can-i-disc-golf-today
|
||||
# Can I Disc Golf Today?
|
||||
A Webtask ready api ready to tell you if the weather looks good for Disc Golfing this weekend.
|
||||
|
||||
[Premade example](https://webtask.it.auth0.com/api/run/wt-sharpshark28-gmail_com-0/discGolfWeekend?webtask_no_cache=1)
|
||||
|
||||
## How do I generate my own?
|
||||
This tool is built on Node/npm/[Webtask](https://webtask.io). Clone this repo locally and within it run:
|
||||
|
||||
```
|
||||
wt create app.js --secret OPEN_WEATHER_KEY={Your Key}
|
||||
```
|
||||
|
||||
Where `{Your Key}` is an API key you generate from [OpenWeatherMap](https://openweathermap.org/).
|
||||
|
|
43
app.js
Normal file
43
app.js
Normal file
|
@ -0,0 +1,43 @@
|
|||
'use latest';
|
||||
require('isomorphic-fetch');
|
||||
|
||||
module.exports = function(ctx, done) {
|
||||
// Params
|
||||
let appid = ctx.secrets.OPEN_WEATHER_KEY;
|
||||
let where = ctx.data.where || 'Austin,US';
|
||||
let tempRange = [ctx.data.min || 50, ctx.data.max || 100]; //Imperial (F)
|
||||
let windMaxSpeed = ctx.data.wind || 50;
|
||||
// URL built from above params
|
||||
let weatherCastURL = `http://api.openweathermap.org/data/2.5/forecast/daily?mode=json&units=imperial&country=US&cnt=7&q=${where}&APPID=${appid}`;
|
||||
// Weekend days offset from today
|
||||
let today = new Date().getDay();
|
||||
let saturday = 6 - today;
|
||||
let sunday = 7 - today;
|
||||
// Function to decide if each day's weather is appropriate for disc golfing
|
||||
let analyze = days => days.map((day, i) => {
|
||||
// Poor weather conditions and relevant user message
|
||||
let notClear = day.weather[0].main !== 'Clear'
|
||||
? `Its calling for ${day.weather[0].description}.` : '';
|
||||
let tooCold = day.temp.day < tempRange[0]
|
||||
? `Brr... Its only ${Math.round(day.temp.day)}F.`: '';
|
||||
let tooHot = day.temp.day >= tempRange[1]
|
||||
? `Phew! Its going to be ${Math.round(day.temp.day)}F.` : '';
|
||||
let tooWindy = day.speed > windMaxSpeed
|
||||
? `May be too windy with wind speeds of ${day.speed}mph.` : '';
|
||||
let details = notClear || tooHot || tooCold || tooWindy || 'Go Disc Golfing!';
|
||||
// Build object to return for each day
|
||||
return {
|
||||
day: ['Saturday', 'Sunday'][i],
|
||||
greatWeather: details === 'Go Disc Golfing!',
|
||||
details
|
||||
};
|
||||
});
|
||||
|
||||
// Fetch weather
|
||||
fetch(weatherCastURL)
|
||||
.then(r => r.json())
|
||||
.then(r => [r.list[saturday], r.list[sunday]]) // We only care about the weekends
|
||||
.then(r => analyze(r)) // Map over each day to determine if they're good for disc golf
|
||||
.then(r => done(null, r)) // Return content to the user
|
||||
.catch(e => done(null, e.error));
|
||||
};
|
11
package.json
Normal file
11
package.json
Normal file
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"name": "can-i-disc-golf-this-weekend",
|
||||
"version": "1.0.0",
|
||||
"description": "A Webtask ready api ready to tell you if the weather looks good for Disc Golfing this weekend.",
|
||||
"main": "app.js",
|
||||
"author": "Joe Wroten <sharpshark28@gmail.com>",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"isomorphic-fetch": "^2.2.1"
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue