Code Monkey home page Code Monkey logo

openweathermap-api-csharp's Introduction

OpenWeatherMap-API - C# Library

Overview

This library takes what openweathermap api returns in JSON, and converts it to C# objects for easy interaction with in C# projects. It supports (most/all) of the returned data the API returns in JSON. The JSON api returned properties are sometimes present, and sometimes not. I have done my best to detect properties that are prone to not being presented, and assigning those values null. You should check certain properties for null vals before assigning under the assumption they are good values. It is also possible I missed values, and the C# library may nullref. This project was originally created to be used in a Twitch chat bot, but has been released for others to use.

Returned Data

  • Clouds
    • All - Level of cloudiness (percentage of cloud cover?)
  • Coordinates
    • Longitude - Query location longitude
    • Latitude - Query location latitude
  • Main
    • CelsiusCurrent - Returns converted Kelvin values of current temperature in Centigrade
    • FahrenheitCurrent - Returns converted Kelvin values of current temperature in Fahrenheight
    • KelvinCurrent - Returns raw openweather API values for temperature in Kelvin
    • CelsiusMinimum - Returns converted Kelvin values of minimum temperature in Centigrade
    • CelsiusMaximum - Returns converted Kelvin values of maximum temperature in Centigrade
    • FahrenheitMinimum - Returns converted Kelvin values of minimum temperature in Fahrenheight
    • FahrenheitMaximum - Returns converted Kelvin values of maximum temperature in Fahrenheight
    • KelvinMinimum - Returns raw openweather API values for minimum temperature in Kelvin
    • KelvinMaximum - Returns raw openweather API values for maximum temperatures in Kelvin
    • SeaLevel - Returns atmospheric pressure on sea level, hPa, raw from openweather API
    • GroundLevel - Returns atmospheric pressure on ground level, hPa, raw from openweather API
  • Rain
    • 3h - Returns rain related data for the last 3 hours at query location (if available).
  • Snow
    • 3h - Returns snow related data for the last 3 hours at query location (if available).
  • Sys
    • Type - System related parameter, avoid usage
    • ID - Openweather API city identification int
    • Message - System related parameter, avoid usage
    • Country - Country code of given query location
    • Sunrise - Returns DateTime for sunrise converted from openweather API returned unix time.
    • Sunset - Returns DateTime for sunset converted from openweather API returned unix time.
  • Weather
    • ID - System related parameter, avoid usage
    • Main - Main description of the weather (IE rain, snow, etc.)
    • Description - Description of main parameter (heavy intensity rain, etc)
    • Icon - Weather icon ID
  • Wind
    • SpeedMetersPerSecond - Gives wind speed in raw values returned by openweathermap api, in meters per second
    • SpeedFeetPerSecond - Gives wind speed in converted values in feet per second
    • Direction - Returns DirectionEnum with details of direction of wind on basis of degree
    • Degree - Returns raw 360-oriented degree returned by openweathermap api
    • Gust - Returns speed of wind gusts in meters per second

Added Functionality

  • Additional Main values: By default the openweathermap API returns just Kelvin temperatures. I have created properties in the Main class that returns equivalent Celsius and Fahrenheight
  • DirectionEnum - Added direction enum in Wind class that is set in constructor on the basis of degree double
  • directionEnumToString(DirectionEnum dir) - Returns string value of wind direction on the basis of passed in DirectionEnum

Installing

Install the NuGet package https://www.nuget.org/packages/OpenWeatherAPI/

Example Usage

var openWeatherAPI = new OpenWeatherAPI.OpenWeatherApiClient("my open weather api key");
// Use async version wherever possible
var query = await openWeatherAPI.QueryAsync("city/location query");
// or non-async version if needed for legacy code
var query = openWeatherAPI.Query("city/location query");

Console.WriteLine(string.Format("The temperature in {0}, {1} is currently {2} °F", query.Name,query.Sys.Country, query.Main.Temperature.FahrenheitCurrent));

Sample Project

This repository also has a sample project. Find it here: https://github.com/swiftyspiffy/OpenWeatherMap-API-CSharp/tree/master/OpenWeatherAPI%20Example

Credits and Libraries Utilized

  • Newtonsoft.Json - JSON parsing class.

License

The MIT License (MIT)

Copyright (c) 2015 swiftyspiffy

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

openweathermap-api-csharp's People

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

openweathermap-api-csharp's Issues

Kevin is showing the Fahrenheit

I am honestly hoping it is just a bug in your software. I put the temp to faren. But it was saying it was 300 out. I put it to kelvin and it says the temp of outside. I live in ks and it is pretty hot out. So my thermometer told me 87. So when i discovered that faren is not faren and kelvin displays faren. I was hoping its just a bug in your software.

Add possibility to use https endpoint

I'm using this library for a cross-platform scenario running, amongst others, on iOS. These platforms force you to use secure endpoints. It seems there are also https endpoints available for Open Weather Map so it would be great to have the ability to use https.

can't use Weather method.

You can only use:

ValidRequest

Coordinates

WeatherList (is it supposed to be this? It returns nothing)

Base

Main

Visibility

Wind

Rain

Snow

Clouds

Sys

ID

Name

Cod

I looked into the package and it looks like everything needed is there for Weather but I can't use it.

Temperature value ignores decimal separator

I tried to use this library, but it seems, it ignores the decimal separator, making the dobule result incredibly high value.

This example will represent, what I mean.
Budapest weather is 274.19 K, but the lib's result is 27419 K (misses the decimal point).
image

Please fix this issue, thanks.

System.NullReferenceException: Object Reference not set to an instance of an object

System.NullReferenceException: Object Reference not set to an instance of an object at OpenWeatherAPI.Sys.cs line 21

Line 21:
if (sysData.SelectToken("type") != null)
Type = int.Parse(sysData.SelectToken("type").ToString());

Notes:
Commenting lines 19-31 on Sys.cs and Line 61 in query.cs seems to correct the problem.
sys.cs /*
public Sys(JToken sysData)
{
if (sysData.SelectToken("type") != null)
Type = int.Parse(sysData.SelectToken("type").ToString());
if (sysData.SelectToken("id") != null)
ID = int.Parse(sysData.SelectToken("id").ToString());
Message = double.Parse(sysData.SelectToken("message").ToString());
Country = sysData.SelectToken("country").ToString();
Sunrise = convertUnixToDateTime(double.Parse(sysData.SelectToken("sunrise").ToString()));
Sunset = convertUnixToDateTime(double.Parse(sysData.SelectToken("sunset").ToString()));
}
*/
query.cs //sys = new Sys(jsonData.SelectToken("sys"));

Add timezone field to `QueryResponse`

The response from OpenWeatherMaps includes a field that tells you something about the timezone. This is unfortunately not included in this library

Decimal Seeparator

Somehow this code seems to use the users decimal separator (in my case a comma) and thus ignoreing the . (US & as far as i know part of the JSON's standard).

Result is wind speeds are 1000 fold of what they are in the data retrieved

"speed": 11.32 thus results in the demo printing 1132 m/s/

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.