Code: Select all
wget -q -O- "hamiltonweather.ca" | awk -F "(<h2>|</h2>)" '{print $2}'
Code: Select all
Light Rain and Fog • 5.3°C
...
Oh, what's that? You don't live in Hamilton? Okay, well, let me help you out a little.
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 ). 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.
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.