Coder Social home page Coder Social logo

weather's Introduction

Weather.js

Build Status npm GitHub license

About

Weather.js was designed to be a conclusive JavaScript weather library built around the OpenWeatherMap API (no affiliation). Since other providers format their output differently, currently OpenWeatherMap is the only source provider.

Weather.js is still in early development so expect changes and please contribute! Among the features I hope to incorporate:

  • historical weather information
  • API key usage (but there is a beta version!)
  • more data sources
  • more conversions!

Note: As stated above there are plans to add more providers in the future. If you have suggestions for other providers that you'd like to see please create a new issue with info about the provider and a link to the provider's API.

Weather.js was originally created by Noah Smith and is currently maintained by PallasStreams.

Install

Weather.js works in the browser and Node.js. Take your pick, For use in the browser, download the most recent version on GitHub. For use in Node, just install using your NPM package manager of choice. Currently Node has an old version of the library available but it will be updated soon.

npm install -g weather.js

Testing

To run the JavaScript unit tests run:

npm run test

Usage

At the moment you can access the current weather conditions and the forcast for any city. By default it will use the closes match as returned by OpenWeatherMap.

// API Key methods
var apiKey = '12345';
Weather.setApiKey( apiKey );
var tempApiKey = Weather.getApiKey();

// Language methods
var language = "de"; // set the language to German - libraries default language is "en" (English)
Weather.setLanguage( language );
var tempLanguage = Weather.getLanguage();

var cityId = '4393217';

// Get current weather for a given city
Weather.getCurrent( 'Kansas City', function( current ) {
    console.log(
        [ 'Currently:', current.temperature(), 'and', current.conditions() ].join( ' ' );
    );
} );

// Get the current weather for a given city using the city id
Weather.getCurrentByCityId( cityId, function( current ) {
    console.log(
        [ 'Currently:', current.temperature(), 'and', current.conditions() ].join( ' ' );
    );
} );

// Get the current weather for a given city using the latitude and longitude
var lat = 39.100,
    long = -94.579;
Weather.getCurrentByLatLong( lat, long, function( current ) {
    console.log(
        [ 'Currently:', current.temperature(), 'and', current.conditions() ].join( ' ' );
    );
} );

// Get the forecast for a given city
Weather.getForecast( 'Kansas City', function( forecast ) {
    console.log( 'Forecast High in Kelvin: ' + forecast.high() );
    console.log( 'Forecast High in Fahrenheit' + Weather.kelvinToFahrenheit( forecast.high() ) );
    console.log( 'Forecast High in Celsius' + Weather.kelvinToCelsius( forecast.high() ) );
} );

// Get the forecast for a given city using the city id
Weather.getForecastByCityId( cityId, function( forecast ) {
    console.log( 'Forecast High in Kelvin: ' + forecast.high() );
    console.log( 'Forecast High in Fahrenheit' + Weather.kelvinToFahrenheit( forecast.high() ) );
    console.log( 'Forecast High in Celsius' + Weather.kelvinToCelsius( forecast.high() ) );
} );

// Get the forecast for a given city using the latitude and longitude
var lat = 39.100,
    long = -94.579;
Weather.getForecastByLatLong( lat, long, function( forecast ) {
    console.log( 'Forecast High in Kelvin: ' + forecast.high() );
    console.log( 'Forecast High in Fahrenheit' + Weather.kelvinToFahrenheit( forecast.high() ) );
    console.log( 'Forecast High in Celsius' + Weather.kelvinToCelsius( forecast.high() ) );
} );

weather's People

Contributors

codewithtyler avatar danielcgold avatar dependabot[bot] avatar ed1nh0 avatar jmoalves avatar noazark avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

weather's Issues

Is this library maintained anymore - Old API and no result

I've been trying to use this, but no matter what city I put in, it's constantly 55.4 degrees. I even tried using your Kansas City example. I notice in the source code you're still calling the 2.1 API.

Is this project still being maintained? Worth posting bug reports, or move on?

Set Language

Would be nice to be able to set the language.

http://api.openweathermap.org/data/2.5/weather?q=London,uk&lang=de

Using https in API URL

Hi there,

My site uses https but the weather.js sends api request to the api in http and the browser complaints about it.

ex: http://api.openweathermap.org/data ...

Is it possible that I can configure or do something to change http to https in the api URLs that the weather.js uses?

Thanks.

Weather Info Not Correct

Hi,

I am requesting Weather for "Braga, PT", the information on the response, coordinates, id etc all correct, except the weather information like temperature and type of weather

I get like 15ºc light rains when in the site openweathermap.org I get 8ºC clear sky. What is the problem?

Add 'licensed' tool to help verify the licenses of dependencies.

GitHub has a tool called licensed which helps us to verify that the node modules we are using are appropriately licensed for what we are using them for. It also helps to verify that the license a node module claims to be under matches the license it uses.

I'm wanting to contribute to the library, how does this impact me?

  • You may need to locally install licensed and run licensed cache to update the dependency cache if you install a new production dependency.
    • If licensed cache is unable to determine the dependency, you may need to modify the cache file yourself to put the correct license.
  • You should still verify the dependency, licensed in a tool to help, but is not a substitute for human review of dependencies

How do we support adding 'licensed'?

We will need to create a PR containing the following changes:

  • A workflow to check licenses on pull requests and pushes to the main branch
  • A licensed.yml file used to configure licensed
  • A number of files into the .licenses directory which contain our dependencies and their appropriate licenses

Sources

The information found above was provided by thboop in this PR made to the action/stale repo. His PR can be used as a reference to perform this work.

More robust conversion

Issues #1 and #2 both deal with conversion. It may be best to solve both of these with a simple conversion library, either by suggesting an existing solution, or creating one.

Update dependencies

I ran the command npm outdated and noticed we have several dependencies that out of date. Below is the results of the command:

Package                Current   Wanted  Latest  Location
grunt                    0.4.5    0.4.5   1.0.1  grunt
grunt-cli               0.1.13   0.1.13   1.2.0  grunt-cli
grunt-contrib-copy       0.8.2    0.8.2   1.0.0  grunt-contrib-copy
grunt-contrib-jshint    0.12.0   0.12.0   1.0.0  grunt-contrib-jshint
grunt-contrib-uglify    0.11.1   0.11.1   2.0.0  grunt-contrib-uglify
grunt-contrib-watch      0.6.1    0.6.1   1.0.0  grunt-contrib-watch
grunt-karma             0.12.2   0.12.2   2.0.0  grunt-karma
karma                  0.13.22  0.13.22   1.1.2  karma
karma-chrome-launcher    0.2.3    0.2.3   1.0.1  karma-chrome-launcher
karma-mocha              0.2.2    0.2.2   1.1.1  karma-mocha
mocha                    2.5.3    2.5.3   3.0.2  mocha

Most of these we should be able to update with no changes to the code. I'll try and knock this out tomorrow night.

Upgrade to use the latest version of the OWM API

OpenWeatherMap is currently on version 2.5 of their API. Weather.js is currently using version 2.1 of the API. We should upgrade to use the latest version. To do this the user will have to provide an API key to Weather.js.

Keeping in mind that the plan is to eventually allow the user to pull from other sources like wunderground @noazark how would you like the user to provide the API key? I know we've talked about using the adapter pattern to do that, but how would that pattern affect this particular issue?

Weather.getCurrent not working

Weather.getCurrent doesn't work. When changing it to a different city (e.g. London) it stays the same

<script>
        Weather.getCurrent( "London", function( current ) {
            $( "#current" ).html( "Temperature: " + current.temperature() + "&deg;K or " +
                Weather.kelvinToFahrenheit( current.temperature() ) + "&deg;F or " +
                Weather.kelvinToCelsius( current.temperature() ) + "&deg;C" +
                "<br />Current Conditions: " + current.conditions() );
        });
        Weather.getForecast( "London", function ( forecast ) {
            $( "#forecast" ).html( "High: " + forecast.high() + "&deg;K or " +
                Weather.kelvinToFahrenheit( forecast.high() ) + "&deg;F or " +
                Weather.kelvinToCelsius( forecast.high() ) + "&deg;C<br />Low: " +
                forecast.low() + "&deg;K or " +
                Weather.kelvinToFahrenheit( forecast.low() ) + "&deg;F or " +
                Weather.kelvinToCelsius( forecast.low() ) + "&deg;C" );
        });
</script>`

Using ID instead of city name

According to openweathermap, you should use the city ID instead of name to get a more exact match; I've modified the following functions to allow for this:

Weather.getCurrentById = function( id, callback ) {
    var url = "http://api.openweathermap.org/data/2.5/forecast?id=" + encodeURIComponent( id ) + "&cnt=1";

    if ( Weather.APIKEY ) {
        url = url + "&APPID=" + Weather.APIKEY;
    } else {
        console.log( 'WARNING: You must set an apiKey for openweathermap' );
    }

    return this._getJSON( url, function( data ) {
        callback( new Weather.Current( data ) );
    } );
};

and

Weather.getForecastById = function( id, callback ) {
    var url = "http://api.openweathermap.org/data/2.5/forecast?id=" + encodeURIComponent( id ) + "&cnt=1";

    if ( Weather.APIKEY ) {
        url = url + "&APPID=" + Weather.APIKEY;
    } else {
        console.log( 'WARNING: You must set an apiKey for openweathermap' );
    }

    return this._getJSON( url, function( data ) {
        callback( new Weather.Current( data ) );
    } );
};

How to create a storm forecast alert?

How to create a storm forecast alert that will occur at a certain time, hour or day shift?
It is something that I do not find or do not understand how it is posed. Since the api allows creating triggers and alerts. But I only saw for conditions of temperature, humidity, pressure, etc.

For example, it would be very useful in a practical case of time of the day that you may have to unplug the modem / router so that it does not burn. Or create an automatic mechanism to close the circuit using smart strips (tplink, sonoff, etc.)

forecast
https://samples.openweathermap.org/data/2.5/forecast/hourly?id=524901&lang=zh_cn&appid=439d4b804bc8187953eb36d2a8c26a02

{"temp":276.635,"temp_min":276.635,"temp_max":276.635,"pressure":1019.752,"sea_level":1019.752,"grnd_level":999.387,"humidity":100,"temp_kf":0},"weather":[{"id":804,"main":"Clouds","description":"阴,多云","icon":"04d"}],"clouds":{"all":93},"wind":{"speed":2.99,"deg":253.222},"rain":{},"sys":{"pod":"d"},"dt_txt":"2019-03-29 11:00:00"},{"dt":1553860800,"main":{"temp":276.588,"temp_min":276.588,"temp_max":276.588,"pressure":1019.321,"sea_level":1019.321,"grnd_level":998.972,"humidity":100,"temp_kf":0},"weather":[{"id":804,"main":"Clouds","description":"阴,多云","icon":"04d"}],"clouds":{"all":94},"wind":{"speed":2.64,"deg":255.679},"rain":{},"sys":{"pod":"d"},"dt_txt":"2019-03-29 12:00:00"}

Decimals should be trimmed

Per PR #9 when using forecast.low(), forecast.high(), Weather.kelvinToFahrenheit(), and Weather.kelvinToCelsius() decimals somtimes look like this 25.584000000000003. I haven't looked into it yet to see which function is causing the issue, but it probably wouldn't hurt to trim the decimals on all four methods. We can go to the hundredths place like so 25.58 or we could just round it up to the nearest whole number.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.