This script has been written for conky. It shows actual weather and weather for tomorrow.
Properties: the weather information are downloaded from two sources. Actual weather is downloaded from SHMU and the forecast is from METEO.AZET.SK.
The forecast is refreshed every day at 2:00 PM.
Usage: the script know three parameters: -a for actual weather, -f for forecast and -help for showing the help menu.
Example of usage:
$ weather -a BA -a PO -f KE
Bratislava 21°C Oblačno 6m/s SZ
Prešov 23°C Oblačno 4m/s SZ
Pondelok 24. 05. polooblačno až oblačno, prehánky 22°C
The script:
#!/bin/bash
#Help, this is displayed when parameter -help is used, or when bad parameter is given
usage(){
echo Wheater forecast for current day and for tomorrow.
echo USAGE: weather [option] [city]
echo OPTIONS:
echo " -a Actual weather"
echo " -f Weather forecast for tomorrow"
echo " -help Displays this help"
echo ""
echo Known cities: KE - Kosice, PO - Presov, BA - Bratislava
echo " -for actual weather it is possible to use other common cities"
echo ""
echo KRISKO 2010
}
#This prints actual weather
actual(){
#print out following options
ARG=( Mesto Teplota Oblačnosť Rýchlosť vetra )
#location given as the parameter
location=$1
#from web page, take the forecast for city specified in $location
ARRAY="`wget -q http://www.shmu.sk/sk/?page=1 -O /tmp/weather.html;cat /tmp/weather.html | grep $location | head -n 1 | sed -e 's/<strong>/^/g' | sed -e 's/<p>//g' | sed -e 's/<\/strong>/^/g' | sed -e 's/<\/p>/ /g' | sed -e 's/°/°/';rm /tmp/weather.html`"
#print out each entry from ARG
for ENTRY in ${ARG[*]}; do
printf "$ARRAY" | tr [^] ["\n"] | grep -A 1 "$ENTRY" | tail -n 1 | tr ["\n"] [' ']
done
echo ""
}
#This prints weather for tomorrow
forecast(){
idx="mesto_predpoved_pravy_data_"
#convert city shortcut to full name (this is because syntax from web page)
#there are few more cities you can use, see the http://meteo.azet.sk/predpovede/europa/slovenska_republika/ page
case "$1" in
KE) location=kosice;;
BA) location=bratislava;;
PO) location=presov;;
* ) echo Unknown city; usage;;
esac
#from web page, take the forecast for city specified in $location
TMRW="`wget -q http://meteo.sk/predpoved-pocasia/europa/slovenska-republika/$location -O /tmp/weathertmrw.html;cat /tmp/weathertmrw.html | grep -A 6 mesto_predpoved_pravy_data_a | head -n 6 | sed -e 's/°/°/' | sed -e 's/<\/span//g' | sed -e 's/ //' | sed -e 's/title="/\>/' | sed -e 's/" src/\>/';rm /tmp/weathertmrw.html`"
#print out following options a=day, b=date, c,d=forecast, e=temp
ARG=( $idx"a" $idx"b" $idx"c" $idx"d" $idx"e" )
#print out each entry from ARG
for ENTRY in ${ARG[*]}; do
printf "$TMRW" | tr ['>'] ["\n"] | grep -A 1 "$ENTRY" | tail -n 1 | tr ["\n"] [' ']
done
echo ""
}
if [ $# -eq 0 ];then
usage
fi
#allowed options are -a, -f, -h in each case proper function is called with parameter (if specified)
while getopts "a:f:h:" option; do
case "$option" in
a) actual $OPTARG;;
f) forecast $OPTARG;;
h) usage;;
[?]) echo "Bad option.";usage;;
esac
done
EDIT: link for forecast updatedEDIT: due to changes on the page SHMU is the script usage limited until the page goes back to normal.
Script in perl:
#!/usr/bin/perl
#pragma; this specifies that all variables have to be declared
use strict;
#define global variables
use vars qw/ %opt /;
#Help, this is displayed when parameter -help is used, or when bad parameter is given
sub usage{
printf "Wheater forecast for current day and for tomorrow.\n";
printf "USAGE: weather [option] [city]\n\n";
printf "OPTIONS:\n";
printf " -a Actual weather\n";
printf " -f Weather forecast for tomorrow\n";
printf " -h Displays this help\n\n";
printf "Known cities: KE - Kosice, PO - Presov, BA - Bratislava\n";
printf " -for actual weather it is possible to use other common cities\n\n";
printf "KRISKO 2010\n";
}
#This prints actual weather
sub actual{
#print out following options
my @ARG=('Mesto', 'Teplota', 'Oblačnosť', 'Rýchlosť', 'vetra');
#location given as the parameter
my $location=$_[0];
#from web page, take the forecast for city specified in $location
my $ARRAY=`wget -q http://www.shmu.sk/sk/?page=1 -O /tmp/weather.html;cat /tmp/weather.html | grep $location | head -n 1 | sed -e 's/<strong>/^/g' | sed -e 's/<p>//g' | sed -e 's/<\\/strong>/^/g' | sed -e 's/<\\/p>/ /g' | sed -e 's/°/°/';rm /tmp/weather.html`;
#print out each entry from ARG
foreach (@ARG){
my $OUT=`echo "$ARRAY" | tr [^] ["\n"]i | grep -A 1 "$_" | tail -n 1 | tr ["\n"] [' ']`;
printf "$OUT";
}
printf "\n";
}
#This prints weather for tomorrow
sub forecast{
#convert city shortcut to full name (this is because syntax from web page)
#there are few more cities you can use, see the http://meteo.azet.sk/predpovede/europa/slovenska_republika/ page
my $location;
switch: {
$_[0] =~ "KE" && do { $location="kosice"; last switch; };
$_[0] =~ "PO" && do { $location="presov"; last switch; };
$_[0] =~ "BA" && do { $location="bratislava"; last switch; };
printf "Unknown city\n"; usage();
}
#from web page, take the forecast for city specified in $location
my $TMRW=`wget -q http://meteo.sk/predpoved-pocasia/europa/slovenska-republika/$location -O /tmp/weathertmrw.html; cat /tmp/weathertmrw.html | grep -A 6 mesto_predpoved_pravy_data_a | head -n 6 | sed -e 's/°/°/' | sed -e 's/<\\/span//g' | sed -e 's/ //' | sed -e 's/title="/\\>/' | sed -e 's/" src/\\>/' | sed -e 3d | sed -e 's/>=/\\n/' | sed -e 4d; rm /tmp/weathertmrw.html`;
#my $TMRW=`wget -q http://meteo.sk/predpoved-pocasia/europa/slovenska-republika/$location -O /tmp/weathertmrw.html;cat /tmp/weathertmrw.html | grep -A 6 mesto_predpoved_pravy_data_a | head -n 6 | sed -e 's/°/°/' | sed -e 's/<\\/span//g' | sed -e 's/ \;//' | sed -e 's/title="/\\>/' | sed -e 's/" src/\\>/'`;
#print out following options a=day, b=date, c,d=forecast, e=temp
my $idx="mesto_predpoved_pravy_data_";
my @ARG=("${idx}a", "${idx}b", "${idx}c", "${idx}d", "${idx}e");
#print out each entry from ARG
foreach (@ARG){
my $OUT=`echo "$TMRW" | tr ['>'] ["\n"] | grep -A 1 "$_" | tail -n 1 | tr ["\n"] [' ']`;
printf "$OUT";
}
print "\n";
}
my $ARGC = $#ARGV + 1;
if ($ARGC == 0){
usage();
}
use Getopt::Std;
my $opt_string = 'a:f:h:';
getopts( "$opt_string", \%opt ) or usage();
usage() if $opt{h};
actual($opt{a}) if $opt{a};
forecast($opt{f});
exit 0;Script in python:#!/usr/bin/python
# coding: utf8
#-------------------------------------------
# Author: KrisKo
# Year: 2011
# Description: script for showing weather forecast
#-------------------------------------------
#for processing bash commands
import commands
import sys
#Help, this is displayed when parameter -help is used, or when bad parameter is given
def usage():
print " Wheater forecast for current day and for tomorrow."
print " USAGE: weather [option] [city]"
print " OPTIONS:"
print " -a Actual weather"
print " -f Weather forecast for tomorrow"
print " -help Displays this help"
print ""
print " Known cities: KE - Kosice, PO - Presov, BA - Bratislava"
print " -for actual weather it is possible to use other common cities"
print ""
print " KRISKO 2010"
exit (1)
#This prints actual weather
def actual():
#from web page, take the forecast for city specified in location
ARRAY=commands.getoutput("wget -q http://www.shmu.sk/sk/?page=1 -O /tmp/weather.html;cat /tmp/weather.html | grep " + sys.argv[2] + "| head -n 1 | sed -e 's/<strong>/^/g' | sed -e 's/<p>//g' | sed -e 's/<\/strong>/^/g' | sed -e 's/<\/p>/ /g' | sed -e 's/°/°/';rm /tmp/weather.html")
#print out following options
ARG = [ 'Mesto', 'Teplota', 'Oblačnosť', 'Rýchlosť', 'vetra' ]
for ENTRY in ARG[:]:
FILTER=commands.getoutput("printf" + ARRAY + " | tr [^] [\"\n\"] | grep -A 1 " + ENTRY + " | tail -n 1 ")
sys.stdout.write(FILTER + " ")
print ""
#This prints weather for tomorrow
def forecast():
#convert city shortcut to full name (this is because syntax from web page)
#there are few more cities you can use, see the http://meteo.azet.sk/predpovede/europa/slovenska_republika/ page
if (sys.argv[2] == "KE"): location="kosice"
elif (sys.argv[2] == "BA"): location="bratislava"
elif (sys.argv[2] == "PO"): location="presov"
else:
print "Unknown city"
usage()
#from web page, take the forecast for city specified in $location
TMRW=commands.getoutput("wget -q http://meteo.sk/predpoved-pocasia/europa/slovenska-republika/" + location + " -O /tmp/weathertmrw.html;cat /tmp/weathertmrw.html | grep -A 6 mesto_predpoved_pravy_data_a | head -n 6 | sed -e 's/°/°/' | sed -e 's/<\/span//g' | sed -e 's/ //g' | sed -e 's/title=\"/\>/' | sed -e 's/\" src/\>/' | cut -d \">\" -f 2; rm /tmp/weathertmrw.html")
#print out following options a=day, b=date, c,d=forecast, e=temp
idx="mesto_predpoved_pravy_data_"
ARG=( idx + "a", idx + "b", idx + "c", idx + "d", idx + "e" )
#print out each entry from ARG
FILTER=commands.getoutput("printf \"" + TMRW + "\" | tr [\"\n\"] [' '] | sed -e 's/ / /g'")# | grep -A 1 " + ENTRY + " | tail -n 1")
sys.stdout.write(FILTER + " ")
print ""
def main():
#allowed options are -a, -f, -h in each case proper function is called with parameter (if specified)
if (len(sys.argv) > 1):
if( sys.argv[1] == '-a' ) and ( len(sys.argv) > 2 ):
actual()
elif( sys.argv[1] == '-f' ) and ( len(sys.argv) > 2 ):
forecast()
elif( sys.argv[1] == '-h' ) or ( sys.argv[1] == '-help' ) or ( sys.argv[1] == '--help' ):
usage()
else:
print "Unknown or bad option"
usage()
else:
print "You need to provide a parameter!"
usage()
main()
### Known Bugs ###
#
# actual forecast for bratislava doesn't work
#
### ### ### ### ###
No comments:
Post a Comment