Categories
Module 6 ReCode

Morning Weather Forecast

📌 Assignment, due Thu Dec 8th

Get the “notify” app running that we built in class using pm2. Let the app run in the background throughout your day and experiment with different scenarios.

✍️ Process

For this assignment, I decided to create an app that sends a morning notification for the day’s weather forecast. There have been too many days over the past few weeks where I have been completely mis-dressed for the weather and have gotten soaked by rain. It’s time to put an end to this madness!

I began this project by playing around more with cron and Pushover and making sure I had a solid grasp of how they were working. I knew I wanted there to be an image displayed along with the weather notification so I tested serving files from URL’s to the Pushover notification (which does not work). I tried out another npm Pushover library called pushover-notifications to see if that was any different (which it was not). The documentation says it uses fs.readFileSync() to send the files to the Pushover API. After poking around on Github I found that this only works with files locally available on the server. I played around a bit with the solutions mentioned in StackOverflow but was not able to get any of them working. So, I decided to put images on the back-burner and move forward with the weather data side of things.

A code block I tried from Stack Overflow, I could not really understand what the code meant and just decided to move on.
The error I got when using an image URL

I chose this weather API to use after looking at a few others. They have a very easy to use API Explorer that lets you easily customize your fetch URL and see the results instantly. They also already have a built in system for delivering an icon to represent the weather status (saving me from having to use another image API for that later on).

I recycled the code from Module 2’s fetch demo to make API calls. Apparently, Node just recently added support for fetch back in February, so every time you run the program you get a beta notification printed in the console. But, I got the API set up very quickly and was able to print the data into the console.

API Results

Now, it was just a matter of putting it all together. I wrote code to create two Strings for the title and body with the relevant data for temperature highs and lows and precipitation amount.

The first notification I got to work – the copy has changed since then and you can see the old app icon being used as the image.

At this point, I decided it was time to go back and tackle the image problem I was facing earlier. I found an npm library called image-downloader that can grab and image from a url and download it into a directory on the server. This was exactly what I needed and I followed the library’s documentation to grab the weather icon from the API link and download it into a /icons folder on the server. The nice thing about these icons is they are only 1 or 2 KB in size, meaning they download very quickly and I don’t have to worry about it taking up too much space on my server (if it downloads a new icon every morning).

The code that downloads the image from the API into the /icons folder
Getting icons working!

One more thing to note is that I stuck with the new Pushover library I discovered. I found it to be more user friendly when writing the notifications and was also able to set custom sounds and a URL to go along with the notification. If the user wants more detailed weather information, they can open the notification and click the link.

✨ Conclusion

Looking back, I am pleased with my work on this assignment. I feel much more confident in finding and using npm libraries. This was also a good chance to get more practice with API’s. Here is what I would like to change going forward:

  1. Location based weather notification – Currently, the API only gives weather based on my dorm’s zip code (10010). This value is coded into the API call (stored in the .env file for privacy). I wonder if there is some way for users to submit this information when they sign up for the notifications? I don’t really know how this could be fixed.
  2. Clothing suggestions – Inspired by Billy’s weather app, it would be fun to have a little phrase to go along with each weather update. Something like “Wear a big coat!” or “Grab your umbrella!” These could be in an array of phrases and use temperature points or rain percentages to choose the phrase.
  3. Dynamic hour weather updates – I don’t wake up at 8am everyday! It’s sometimes earlier and sometimes later. If the notification could be customized to send at a different time everyday, that would be very useful.

Leave a Reply

Your email address will not be published. Required fields are marked *