Internet Weather Report in 70 characters of bash code.

Share your own howto's etc. Not for support questions!

Internet Weather Report in 70 characters of bash code.

Postby bedtime » 2018-02-19 20:52

Here it is:
Code: Select all
wget -q -O- "hamiltonweather.ca" | awk -F "(<h2>|</h2>)" '{print $2}'


Depending on the weather, you should get:
Code: Select all
Light Rain and Fog • 5.3°C


So that's it, an internet weather report in 70 characters of bash code—thank you for reading—bye! :D

...

Oh, what's that? You don't live in Hamilton? Okay, well, let me help you out a little. :roll:

Here is a template for those wishing to add their own weather site. I've filled it in with my info as a working example:

The Template
Code: Select all
#!/bin/bash

URL="hamiltonweather.ca"

# The text to the left (A) and right (B) of the info you want to extract
A="<h2>"
B="</h2>"

# Replace text *a with text *b. May be left blank.
R1a=" •"
R1b="☀"

R2a="Snow •"
R2b="❆☃☄"

R3a="Cloudy •"
R3b="☁ϟ☂"

wget -q -O- "$URL" | awk -F "($A|$B)" -v vR1a="$R1a" -v vR1b="$R1b" -v vR2a="$R2a" -v vR2b="$R2b" -v vR3a="$R3a" -v vR3b="$R3b" '{sub(vR1a, vR1b); sub(vR2a, vR2b); sub(vR3a, vR3b); print $2}'



How to make the template work

You will have to do a little digging, but I've done about 95% of the work for you (well, okay, I asked for help on another site...so I can't take all the credit :wink:). You need only drop your in your info.

Step 1:

Go to a weather site that is displaying the weather, and note how the weather is described. In my example, I had, "Light Rain and Fog".

Step 2:

Take a look at the html code. Search for your text (Light Rain and Fog, for my site).

Step 3:

Having found it, look at the surrounding tags. Mine were "<h2>" and "</h2>".

Step 4:

Do a search for both those tags, and see if they are anywhere else in the code. If they are, you cannot use them. You must expand beyond the tags until you find unique code that does not exist anywhere else in the html. I got lucky with this site, but I tried several to get to where I am. Also, the characters or tags MUST be on the same line! If they are not, it will NOT work!

Step 5:

Sweep up any hair that you lost in procuring a site with usable html.

Step 6:

What? You notice that there are imperfections which get in the way of displaying the weather the way you want it? My advice is to just try to get used to them.

Alright, I'll help you out. :roll:

This is were the R* variables come in! Use variable 'R1a' as text you want to replace and use variable 'R1b' as the text you want to take its place. You may leave it blank to just get rid of it.


But it STILL doesn't work!

No problem. I have the perfect solution for you...

...

Move to Hamilton.
bedtime
 
Posts: 51
Joined: 2012-12-16 19:34

Re: Internet Weather Report in 70 characters of bash code.

Postby ruffwoof » 2018-02-19 21:32

70 chars!!! What bloat! :)

In 19 ...
Code: Select all
curl wttr.in/~sw19

What you don't live in Wimbledon? Then just change the post/zip code, or name a town. For instance curl wttr.in/~newyork

The actual shell script code I use for a xterm is ...
Code: Select all
#!/bin/sh
# change to your city
CITY=sw19
xterm -T "Weather" -geometry 136x44+0+0 -fa DejaVu:size=9 -e "curl wttr.in/~$CITY;read"
Debian oldstable
Acer Aspire M3201 (2GB), Radeon HD3200, AMD Phenom X4
ruffwoof
 
Posts: 234
Joined: 2016-08-20 21:00

Re: Internet Weather Report in 70 characters of bash code.

Postby bedtime » 2018-02-20 00:31

ruffwoof wrote:70 chars!!! What bloat! :)
In 19 ...
Code: Select all
curl wttr.in/~sw19

I've seen this code out there. It's nice for those who would like to view a large text display, but for those that want a one-liner, perhaps they want it in their dwm status bar, it's not the way to go, unless one could find a way of somehow grepping that particular data.

What you don't live in Wimbledon? Then just change the post/zip code, or name a town. For instance curl wttr.in/~newyork

:lol:

...I just did the code for the Canadian Government of Canada website. It's quite solid and not likely to change for a while. :)

What, you don't live in CANADA? :shock:

Canada:
Code: Select all
#!/bin/sh

#143 = Toronto; just look up your city and enter your number
URL="https://weather.gc.ca/city/pages/on-143_metric_e.html"

A='center">'
B='</p>'
C='>'
D='&deg'

echo $(wget -q -O- "$URL" | awk  -v v1="$A" -v v2="$B" -v v3="$C" -v v4="$D" '$0 ~ v1 ".*" v2 {sub(v2 ".*", ""); sub(".*" v1, ""); print $0; next;} $0 ~ v3 ".*" v4 {sub(v4 ".*", ""); sub(".*" v3, ""); print $0 "°C"; exit}')


I admit it's a monster, but it has to grab data from two separate lines. 'echo' is used as a hack so that awk doesn't add a new line after the first readout. This code grabs the conditions and then immediately leaves to find the temperature, after which it immediately quits. It is all done by opening awk only once and no other programs except wget and echo. I'll have to figure out how to stop awk from adding that line.

Also, this code is different from my code above, and I recommend this code as the former seemed to mess up on other sites including this one.

Save as canada:
Code: Select all
$ chmod +x canada


Run:
Code: Select all
$ ./canada


Result will be akin to:
Code: Select all
Light Rain and Fog 8°C


Looks beautiful in dwm status bar. :D
bedtime
 
Posts: 51
Joined: 2012-12-16 19:34

Re: Internet Weather Report in 70 characters of bash code.

Postby sunrat » 2018-02-20 02:58

8°C! Remind me not to go to Toronto in winter. :D
Code: Select all
$ curl wttr.in/Melbourne
Weather report: Melbourne, Australia

    \  /       Partly cloudy
  _ /"".-.     26 °C         
    \_(   ).   ↖ 15 km/h     
    /(___(__)  10 km         
               0.0 mm
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!
User avatar
sunrat
 
Posts: 2215
Joined: 2006-08-29 09:12
Location: Melbourne, Australia

Re: Internet Weather Report in 70 characters of bash code.

Postby Hallvor » 2018-02-20 08:07

sunrat wrote:8°C! Remind me not to go to Toronto in winter. :D
Code: Select all
$ curl wttr.in/Melbourne
Weather report: Melbourne, Australia

      \  /       Partly cloudy
  _ /"".-.     26 °C         
    \_(   ).   ↖ 15 km/h     
    /(___(__)  10 km         
               0.0 mm


Code: Select all
Weather report: Kristiansand, Norway

 _`/"".-.     Light Snow
   ,\_(   ).   -9--2 °C       
    /(___(__)  ↙ 26 km/h     
      *  *  *  3 km           
     *  *  *   0.1 mm 


8°C is the yearly average here...
:lol:
Intel Core i5 3320-M CPU @ 2.60 GHz, 6 GB RAM, Intel HD 4000 graphics, 240 GB SSD, Debian Stretch (KDE)
User avatar
Hallvor
 
Posts: 798
Joined: 2009-04-16 18:35
Location: Norway

Re: Internet Weather Report in 70 characters of bash code.

Postby debiman » 2018-02-20 08:55

wttr.in is cheating.
i present to you https://github.com/ohnonot/conky-itl-weather
what, you don't live in finland?
you can actually use that for most european and many global locations, too, in 3 languages.

we do have a solid -9 degrees celsius, clear sky & sunny, almost no wind, snow & ice everywhere. it glitters like crazy. what am i doing on the computer, stupid me?

Image
User avatar
debiman
 
Posts: 2126
Joined: 2013-03-12 07:18

Re: Internet Weather Report in 70 characters of bash code.

Postby bedtime » 2018-02-20 17:01

Accuweather is done. :D

This example: Kristiansand, Norway:
Code: Select all
#!/bin/sh

# Find your city and add the link
URL="https://www.accuweather.com/en/no/kristiansand/261324/weather-forecast/261324"

A="txt : '"
B="',"
C="temp_f : '"
D="',"

wget -q -O- "$URL" | mawk  -v v1="$A" -v v2="$B" -v v3="$C" -v v4="$D" '$0 ~ v1 ".*" v2 {sub(v2 ".*", ""); sub(".*" v1, ""); printf $0 " "; next;} $0 ~ v3 ".*" v4 {sub(v4 ".*", ""); sub(".*" v3, ""); print $0 "°F"; exit}'


Output:
Code: Select all
Light snow 30°F


* EDIT *
Fixed line printing isses (used printf).
Last edited by bedtime on 2018-02-20 22:40, edited 1 time in total.
bedtime
 
Posts: 51
Joined: 2012-12-16 19:34

Re: Internet Weather Report in 70 characters of bash code.

Postby debiman » 2018-02-20 20:41

debiman wrote:we do have a solid -9 degrees celsius, clear sky & sunny, almost no wind, snow & ice everywhere. it glitters like crazy. what am i doing on the computer, stupid me?

what a beautiful day that was.
walking on the ice...
User avatar
debiman
 
Posts: 2126
Joined: 2013-03-12 07:18


Return to Docs, Howtos, Tips & Tricks

Who is online

Users browsing this forum: No registered users and 2 guests

fashionable