Skip to content

Tut_weather_part_1

Sven Roelse edited this page Aug 13, 2019 · 1 revision

Let's make a weather add-on - Part 1

In this tutorial we'll focus on getting a weather add-on working for PnP.

Questions immediately arise:

  1. Where do we get the weather information from?
  2. How do we retrieve, interpret and store this weather information?
  3. How do we display this information?

Answers follow after some investigation. There are many bots around on IRC which display useful information and some have their source code on GitHub, GitLab etc. I'm currently following the development of jesopo's BitBot and see that she has coded a weather module. Why reinvent the wheel? So, to answer the first question: we can use OpenWeatherMap.org (OWM). Further reading reveals that they have an API and offer a free plan, with up to 60 calls a minute!

I'm reading this: https://openweathermap.org/current

I read that results are by default returned in JSON format. XML and HTML formats are also supported. But, JSON! This means we can use SReject's excellent JSON For mIRC script to do all of the heavy lifting for us! This answers the second question.

Now I am reading this: https://openweathermap.org/appid

In "Tips on how to use API effectively", I notice that you shouldn't query the source for more than once every 10 minutes and it is recommended to store your previous request.

An example URL will look like: http://api.openweathermap.org/data/2.5/weather?q=Vlissingen,nl&units=metric&appid={APIKEY}

And this will be the result:

{
   "coord":{
      "lon":3.57,
      "lat":51.46
   },
   "weather":[
      {
         "id":800,
         "main":"Clear",
         "description":"clear sky",
         "icon":"01d"
      }
   ],
   "base":"stations",
   "main":{
      "temp":17.33,
      "pressure":1017,
      "humidity":55,
      "temp_min":16,
      "temp_max":18.33
   },
   "visibility":10000,
   "wind":{
      "speed":6.7,
      "deg":270
   },
   "clouds":{
      "all":0
   },
   "dt":1565720327,
   "sys":{
      "type":1,
      "id":1553,
      "message":0.0085,
      "country":"NL",
      "sunrise":1565670459,
      "sunset":1565723631
   },
   "timezone":7200,
   "id":2745392,
   "name":"Vlissingen",
   "cod":200
}

Summarize

Gathering ingredients:

  • I need SReject's JSON For mIRC: https://github.com/SReject/JSON-For-Mirc
  • I need the base OWM API URL: http://api.openweathermap.org/data/2.5/weather
  • After the base URL I need to specify the parameters: ?q={city name},{country code}, where city name and country code divided by comma, use ISO 3166 country codes.
  • I want users of the add-on to specify in what format the data is displayed. By default the temperature is listed in Kelvin. Then there's an option &units=metric and &units=imperial to specify temperature in Celsius and Fahrenheit respectively.
  • I need an OWM API key. After you have signed up, have a look here: https://home.openweathermap.org/api_keys Be sure to append &APPID={APIKEY} to the API URL.