Skip to content

Instantly share code, notes, and snippets.

@miloslavnosek
Last active May 1, 2023 13:51
Show Gist options
  • Save miloslavnosek/6adccf4ec649f54f175d42bdde37e487 to your computer and use it in GitHub Desktop.
Save miloslavnosek/6adccf4ec649f54f175d42bdde37e487 to your computer and use it in GitHub Desktop.
Scrape hourly weather for given day and location
#!/bin/sh
declare -A locations=(
["berlin"]=2-2950159
)
default_location=${locations["berlin"]}
if [[ $1 != '' ]]; then location=${locations[$1]}; else location=${default_location}; fi
if [[ $2 != '' ]]; then day_from_now=$2; else day_from_now=0; fi
curl -Ls "https://www.yr.no/en/forecast/hourly-table/${location}?i=${day_from_now}" | html2text | sed -n '/Updated/,$p' | sed -n '/Editor/q;p' | head -n -2

This is a shell script that scrapes hourly weather tables from yr.

$ weather.sh
Updated 22:27.How often is the weather forecast updated?
Forecast as PDFForecast as SVG
***** Monday 1 May *****
                      Display for hour-by-hour forecast.
Time Weather         Temp. Precip. mm Wind speed m/s    Wind desc.
00   [clear sky]     5°    0          2 m/s from east2  Light breeze from east
01   [clear sky]     4°    0          3 m/s from east3  Light breeze from east
02   [clear sky]     4°    0          2 m/s from east2  Light breeze from east
03   [clear sky]     4°    0          3 m/s from east3  Light breeze from east
04   [clear sky]     4°    0          3 m/s from south  Light breeze from
                                      east3             south east
05   [clear sky]     3°    0          3 m/s from south  Light breeze from
                                      east3             south east
06   [clear sky]     3°    0          3 m/s from south  Light breeze from
                                      east3             south east
07   [partly cloudy] 5°    0          4 m/s from south  Gentle breeze from
                                      east4             south east
08   [partly cloudy] 7°    0          3 m/s from south  Light breeze from
                                      east3             south east
09   [partly cloudy] 9°    0          3 m/s from south  Light breeze from
                                      east3             south east
10   [partly cloudy] 11°   0          4 m/s from south  Gentle breeze from
                                      east4             south east
11   [partly cloudy] 13°   0          4 m/s from south  Gentle breeze from
                                      east4             south east
12   [partly cloudy] 14°   0          4 m/s from south  Gentle breeze from
                                      east4             south east
13   [partly cloudy] 15°   0          4 m/s from south  Gentle breeze from
                                      east4             south east
14   [partly cloudy] 16°   0          4 m/s from south  Gentle breeze from
                                      east4             south east
15   [partly cloudy] 17°   0          4 m/s from south  Gentle breeze from
                                      east4             south east
16   [cloudy]        17°   0          4 m/s from south  Gentle breeze from
                                      east4             south east
17   [cloudy]        16°   0          3 m/s from south  Light breeze from
                                      east3             south east
18   [cloudy]        16°   0          3 m/s from south  Light breeze from
                                      east3             south east
19   [cloudy]        15°   0          2 m/s from south  Light breeze from
                                      east2             south east
20   [cloudy]        13°   0          2 m/s from south  Light breeze from
                                      east2             south east
21   [cloudy]        14°   0          2 m/s from north2 Light breeze from
                                                        north
22   [cloudy]        13°   0          1 m/s from east1  Light air from east
23   [cloudy]        11°   0          2 m/s from south  Light breeze from
                                      east2             south east
Sunrise
05:35
Sunset
20:32
Next day
***** Footer *****

Setup:

- Get IDs of your locations by using search in aforementioned website and copying them from URL and add them to the associative array LOCATIONS. You can have as many locations as you want :).
- On line 7 replace "berlin" with whatever default location you want in case no location is passed to the script.

Usage:

Get info from default location for current day:
./weather.sh

Get info from set location for today (the 0 is not needed when scraping current day):
./weather.sh berlin 0

Get info from set location for day after today:
./weather.sh berlin 1

Dependencies:

html2text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment