Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Internet Weather Report in 70 characters of bash code.

Share your HowTo, Documentation, Tips and Tricks. Not for support questions!.
Post Reply
Message
Author
bedtime
Posts: 146
Joined: 2012-12-16 19:34
Has thanked: 1 time
Been thanked: 6 times

Internet Weather Report in 70 characters of bash code.

#1 Post by bedtime »

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.

ruffwoof
Posts: 298
Joined: 2016-08-20 21:00

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

#2 Post by ruffwoof »

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"

bedtime
Posts: 146
Joined: 2012-12-16 19:34
Has thanked: 1 time
Been thanked: 6 times

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

#3 Post by bedtime »

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

User avatar
sunrat
Administrator
Administrator
Posts: 6412
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 116 times
Been thanked: 462 times

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

#4 Post by sunrat »

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
Hallvor
Global Moderator
Global Moderator
Posts: 2029
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 139 times
Been thanked: 206 times

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

#5 Post by Hallvor »

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:
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

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

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

#6 Post by debiman »

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

bedtime
Posts: 146
Joined: 2012-12-16 19:34
Has thanked: 1 time
Been thanked: 6 times

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

#7 Post by bedtime »

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.

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

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

#8 Post by debiman »

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...

Post Reply