Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to OpenWeatherMap API #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,26 @@

Made for [Übersicht](http://tracesof.net/uebersicht/)

Uses the [freegeoip.net](http://freegeoip.net/ "freegeoip.net") api to obtain your location, and the [Yahoo Weather api](https://developer.yahoo.com/weather// "Yahoo Weather api") to obtain the weather information.

## In Action

### Original Icons
![Original Icons]
(https://raw.githubusercontent.com/felixhageloh/weather-widget/master/originalIcons.png)

### Yahoo Icons
![Yahoo Icons]
(https://raw.githubusercontent.com/felixhageloh/weather-widget/master/yahooIcons.png)
Uses the [positionstack.com](https://positionstack.com/documentation), [ipstack.com](https://ipstack.com/documentation) to obtain your location, and the [OpenWeather API](https://home.openweathermap.org/users/sign_up) to obtain the weather information.
All APIs require registration to get an API key, so please configure those in the `index.coffe` script.

## Options

You can find all options `index.coffee` at the top of the file:

1. Default city and region. You can replace `<city>` with your city, and `<region>` with your region (state). This location is used in case the automatic location lookup fails, or if you switch off auto location (see below). Example:
1. Default city and region. Set `city` and `region` if you switch off `auto` location (see below). Example:

```
options =
city : 'Cupertino'
region: 'CA'
city : 'Cupertino'
region : 'CA'
```

2. Temperature units. Use 'f' for Fahrenheit and 'c' for Celsius.
2. Temperature `units`. Use `imperial` for Fahrenheit and `metric` for Celsius.

3. Automatic location lookup. Set `staticLocation` to `true` to disable auto location and instead always use the default city and region.
3. Automatic location lookup. Set `useLocation` to `static` to disable auto location and instead always use the default city and region.

4. API keys `posApiKey`, `geoipApiKey` and `weatherApiKey` are required to obtain responses from the PositionStack, IpStack and OpenWeather APIs.

## Appearance

Expand All @@ -39,6 +32,9 @@ To tweak the appearance, just follow the directions inside `index.coffee`. You c
Automatic location detection and switch to Yahoo api by @nickroberts
https://github.com/nickroberts

Icons by Erik Flowers
Ported to Dark Sky and ipstack API after Yahoo retired their API
https://github.com/Titousensei

Original icons by Erik Flowers
http://erikflowers.github.io/weather-icons/

39 changes: 0 additions & 39 deletions weather.widget/README.md

This file was deleted.

90 changes: 56 additions & 34 deletions weather.widget/get-weather
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// usage:
// node get-weather.js [city] [region] [units] [lang] [geoipkey] [weatherapikey]
// node get-weather [city] [region] [units] [lang] [posapikey] [geoipapikey] [weatherapikey]

process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 1;

var http = require('http'),
https = require('https'),
Expand All @@ -13,40 +13,64 @@ var options = {
units: process.argv[4].toLowerCase(),
static: process.argv[5] === 'static',
lang: process.argv[6],
geoipApiKey: process.argv[7],
forecastApiKey: process.argv[8]
posApiKey: process.argv[7],
geoipApiKey: process.argv[8],
weatherApiKey: process.argv[9]
};

var weatherRetryCounter = 0;

getLocation(function(location) {
if (location.city) {
options.region = location.region_name;
options.city = location.city;
options.latitude = location.latitude;
options.longitude = location.longitude;
getPosition(function(position) {
if (position.data
&& position.data.length > 0) {
var pos = position.data[0];
options.latitude = pos.latitude;
options.longitude = pos.longitude;
}

getWeather(options, function(data) {
retryGetWeatherOrPrintResults(data);
getLocation(function(location) {
if (location.city) {
options.region = location.region_name;
options.city = location.city;
options.latitude = location.latitude;
options.longitude = location.longitude;
}

getWeather(options, function(data) {
retryGetWeatherOrPrintResults(data);
});
});
});

function getPosition(callback) {
if (!options.static) return callback({data:[]});

var url = 'http://api.positionstack.com/v1/forward';
var params = {
query: options.city + ',' + options.region,
access_key: options.posApiKey,
output: 'json'
};

var request = getJSON(url, params, callback);

setTimeout(function(){
request.abort();
}, 3000)
}

function getLocation(callback) {
if (options.static) return callback({});

var url = "http://api.ipstack.com/check";
var url = 'http://api.ipstack.com/check';
var params = {
access_key : options.geoipApiKey,
language : "en",
output: "json"
access_key: options.geoipApiKey,
language: 'en',
output: 'json'
};

var request = getJSON(url, params, callback);

request.on('error', function() {
callback({})
});
setTimeout(function() {
request.abort();
}, 3000);
Expand Down Expand Up @@ -74,25 +98,18 @@ function retryGetWeatherOrPrintResults(data) {
}

function getWeather(options, callback) {
var geo = options.latitude + "," + options.longitude;
var exclude = "minutely,hourly,alerts,flags";

var url = "https://api.darksky.net/forecast/" + options.forecastApiKey + "/" + geo;
var url = 'https://api.openweathermap.org/data/2.5/onecall';
var params = {
units : options.units,
lang: options.lang,
exclude: exclude
appid: options.weatherApiKey,
lat: options.latitude,
lon: options.longitude,
units: options.units,
lang: options.lang
};

var request = getJSON(url, params, function(data) {
getJSON(url, params, function(data) {
callback(data);
});

request.on('error', function(e) {
callback({
error: e.message
});
});
}

function getJSON(url, params, callback) {
Expand All @@ -107,7 +124,7 @@ function getJSON(url, params, callback) {
if (querystring)
url = url + '?' + querystring;

var json = "",
var json = '',
result;

return protocol.get(url, function(res) {
Expand All @@ -124,5 +141,10 @@ function getJSON(url, params, callback) {
}
callback(result);
});
res.on('error', function(e){
callback({
error: e.message
});
})
});
}
Loading