Ubuntu Forums ubuntu.com - launchpad.net - ubuntu help  

Go Back   Ubuntu Forums > The Ubuntu Forum Community > Forum Archive > Main Support Categories > Desktop Effects & Customization
Register Reset Password Forum Help Forum Council Search Today's Posts Mark Forums Read

Hello, Unregistered You are browsing a READ only archive of the main support categories pre 4/21/2008. You will not be able to post or reply any threads in this section.
Ubuntu 9.10 is out!!!

When downloading Ubuntu 9.10 please consider using bittorrent to get your copy of Ubuntu.

The Ubuntu Developers Summit for Lucid Lynx will be held the week of 16-Nov-2009 till 20-Nov-2009 in Dallas, TX USA. Visit the the Ubuntu wiki for more information about UDS and how to participate remotely.

Desktop Effects & Customization
This section is for all your compositing needs. This includes Beryl, Compiz, xcompmgr, and other fancy apps which take advantage of compositing managers such as kiba-dock and avant window decorator

 
Thread Tools Display Modes
Old January 13th, 2008   #1
lvleph
Has an Ubuntu Drip
 
Join Date: Jul 2006
Beans: 704
Ubuntu 9.04 Jaunty Jackalope
Conky Weather Revisited V2

IMPORTANT: Before using the new script be aware that the options have changed ( 03 Feb 08 ), and your conkyrc will most likely need to be updated.

The original Conky Weather Revisited was too buggy, because of the way Conky handles bash. Still don't know why it is different, but I decided I would write the script in a language I was more familiar with anyway. So I wrote the V2 in perl. I hope that everyone likes what I have done. I will try my best to meet everyone's needs.

Well, I didn't really like the other conky weather scripts that were out there. They just didn't do what I wanted and were too complicated, so I made my own. I hope everyone likes it.

Step 0: Find your city code. You can go to weather.com and put in your city in the search. It will then ask you to select the city that you meant. Select that, once the page has loaded look at the address bar for a number after the last / (it should include letters too).

Step 1: This conky setup uses a weather font, which I have attached.
To install the fonts follow the directions below.

1. Make the following directory as root:
Code:
# sudo mkdir /usr/share/fonts/truetype/myfonts
2. Copy the font(s) into the newly created directory:
Code:
# sudo cp [fonts] /usr/share/fonts/truetype/myfonts
3. Run the following to setup your fonts cache:
Code:
# fc-cache -f -v
Step 2: Save the following script in a file named weather.pl and place it in ~/scripts folder.

Code:
#!/usr/bin/perl

use Switch;
use Encode;
use Text::Wrap;


# This script was written by lvleph and inspired by the original conky weather script written by azhag (azhag@bsd.miki.eu.org)
# Modyfied by LazarusHC to list more details

$code=$ARGV[0]; #zipcode or weather.com city code
$system=$ARGV[1]; #f for imperial c for metric
$what=$ARGV[2]; #what are we looking for?
$file="/tmp/weather.html"; #temp holding weather
$update=3600; #time in seconds to update $file if set to 0 don't use $file

$leadspace="  "; #spacing before each high low
$trailspace="   "; #spacing after each high low.
$fspaces=""; #spacing between condition symbols.
$dspaces="    "; #spacing between each day
$lines="\n\n\n\n"; #each \n represents one line between the days and temps

$Text::Wrap::columns = 58;
$initial_tab=""; #tab before first line in weather output
$subsequent_tab="\t"; #tab before each subsequet line in weather output

$degree= encode_utf8( "\x{00B0}" ); #give me the degree symbol, not everyone has same locale

#ensure user inputs proper system
if($system !=~ "c" || $system !=~ "f"){$what=0;} #this will give usage error 

switch($what){ #determine what user wants
	case "c" { #if current conditions
		&file_op; #save weather to $file
		while(<FILE>){ #cycle through file
			if (/<em>Current conditions/ .. /<h3>/){ #found current conditions
				($cn2) = /<h3>(\b.+\b)<\/h3>/; #save current conditions
				if($cn2){print "$cn2\n"; exit;}			
			}
		}
	}
	case "w" { #if list
		&file_op; #save weather to file
		while(<FILE>){ #cycle through file
			if (/<dt>Feels Like:<\/dt>/ .. /<dd>/){ #found feels like temp
				($tmf) = /<dd>(-?\d+)/; #sav temp								
			}
			if (/<dt>Humidity:<\/dt>/ .. /<dd>/){ #found current humidity
				($hmt) = /<dd>(\d+\%)/; #save current humidity					
			}
			if (/<dt>Wind:<\/dt>/ .. /<dd>/){ #found wind conditions
				($wnd) = /<dd>(\b.+\b)<\/dd>/; #save wind conditions
				#do we have current conditions?
				if($tmf && $hmt && $wnd){
					print "Feels like:  $tmf$degree\n";
					print "Humidity:    $hmt\n"; 
					print "Wind:        $wnd\n"; exit;}
			}
		}
	}	
	case "cp" { #if current conditions symbol
		&file_op; #save weather to $file
		while(<FILE>){ #cycle through file
			if (/<em>Current conditions/ .. /<h3>/){ #found current conditions
				($cnd) = /<h3>(\b.+\b)<\/h3>/; #save current conditions
				#do we have current conditions? Then translate into symbol
				if($cnd){cond_symb($cnd); print "$ctext\n"; exit;}
			}
		}
	}	
	case "t" { #if current temp
		&file_op; #save weather to $file
		while(<FILE>){ #cycle through file
			if (/<div id="forecast-temperature">/ .. /<h3>/){ #found current temp
				($tmp) = /<h3>(-?\d+)/; #save current temp
				#do we have current temp? Then print
				if($tmp){print "$1$degree\n"; exit;}
			}
		}
	}
	case /[1-5]d$/ { #display the days up to specified day 
		&file_op; #save weather to $file
		my $day=(split "t", $what)[0]; #how many days are we looking for
		my $count=0; 
		while(<FILE>){
			if(/<th>(\b.+\b)<\/th>/ && ++$count<=$day){ #look for the conditions upto specified day
				$days[$count-1]=$1; #save day
				&day_space($days);
			}
			elsif($count>=$day){print "$dtext\n"; exit;} #don't keep lopking if everything has been found
		}
	}
	case /[1-5]dp$/ { #display the conditions from today through day $days
		&file_op; #save weather to $file
		my $day=(split "p", $what)[0]; #how many days are we looking for
		my $flag=0; #set flag for when we find start of conditions
		my $count=0; 
		while(<FILE>){
			if(/^<tr class="titles">\s*$/){$flag=1;} #found the start of conditions
			elsif($flag && /(\b.+\b)<\/td>/ && ++$count<=$day){ #look for the conditions upto specified day
				$cnd[$count-1]=$1; #save conditions
				&cond_symb ($cnd[$count-1]); #translate conditions to symbol
				#exit;
			}
			elsif($count>=$day){print "$ctext\n"; exit;} #don't keep looking if everything has been found
		}
	}
	case /[1-5]t$/ { #display the temps from today through day $days
		&file_op; #save weather to $file
		my $count=0;
		my $day=(split "t", $what)[0]; #how many days are we looking for
		while(<FILE>){
			#get the high temp
			(my $high) = /<td><strong>High: (-?\d+)&deg;<\/strong><span>Low: \-?\d+&deg;<\/span><\/td>/;
			#get the low temp
			(my $low) = /<td><strong>High: \-?\d+&deg;<\/strong><span>Low: (-?\d+)&deg;<\/span><\/td>/;
			#print the high and low temp for the specified day
			if($high=~/\d+/ && $low=~/\d+/ && ++$count<=$day){print "$leadspace$high$degree/$low$degree$trailspace";}
			elsif($count>=$day){print "\n"; exit;} #don't keep looking if everything has been found
		}
	}
	case /[1-5]dt$/ {
		&file_op; #save weather to $file
		my $count1 = my $count2=0;
		my $day=(split "dt", $what)[0]; #how many days are we looking for
		my $flag=1; #print days once
		while(<FILE>){
			#get the high temp
			(my $high) = /<td><strong>High: (-?\d+)&deg;<\/strong><span>Low: \-?\d+&deg;<\/span><\/td>/;
			#get the low temp
			(my $low) = /<td><strong>High: \-?\d+&deg;<\/strong><span>Low: (-?\d+)&deg;<\/span><\/td>/;
			#print the high and low temp for the specified day
			if(/<th>(\b.+\b)<\/th>/ && ++$count1<=$day){ #look for the conditions upto specified day
				$days[$count1-1]=$1; #save day
				&day_space($days);
			}
			elsif($high=~/\d+/ && $low=~/\d+/ && ++$count2<=$day){$ttext.=$leadspace.$high.$degree."/".$low.$degree.$trailspace;}
			elsif($count1>=$day && $count2>=$day){print "$dtext\n$lines$ttext\n"; exit;} #don't keep lopking if everything has been found
		}
	}
	case /[1-7]w$/ { #display the weather forecast in words from today through day $days
		&file_op; #save weather to $file
		my $num=(split "w", $what)[0]; #how many are we looking for
		my $count=0; #initialize count
		while(<FILE>){ #cycle through file
			#get the weather
			(my $when) = /<li><strong>(\b.+\b\:)<\/strong>/;
			(my $weather) = /<\/strong>(.+)<\/li>/;
			$weather=$when.$weather;
			#print weather
			if($when && ++$count<=$num){
				#print "$when";
				print wrap($initial_tab, $subsequent_tab, $weather);
				print "\n";
			}
			elsif($count>=$num){exit;} #don't keep looking if everything has been found
		}
	}
	case /[1-5]p$/ { #if conditions of specified day
		&file_op; #save weather to $file
		my $day=(split "p", $what)[0]; #what day are we looking for
		my $flag=0; #set flag for when we find start of conditions
		my $count=0; 
		while(<FILE>){
			if(/^<tr class="titles">\s*$/){$flag=1;} #found the start of conditions
			elsif($flag && /(\b.+\b)<\/td>/ && ++$count==$day){ #look for the conditions for specified day
				$cnd=$1; #save conditions
				&cond_symb ($cnd); #translate conditions to symbol
			}
			elsif($count>=$day){print "$ctext\n"; exit;} #don't keep looking if everything has been found
		}
	}
	case /[1-5]$/ { #if temp of specified day 
		&file_op; #save weather to $file
		while(<FILE>){
			#get the high temp
			($high) = /<td><strong>High: (-?\d+)&deg;<\/strong><span>Low: \-?\d+&deg;<\/span><\/td>/;
			#get the low temp
			($low) = /<td><strong>High: \-?\d+&deg;<\/strong><span>Low: (-?\d+)&deg;<\/span><\/td>/;
			#print the high and low temp for the specified day
			if($high && $low && ++$count==$what){print "$high$degree/$low$degree\n";}
		}
	}
	else { #didn't give proper options
		&usage; #print usage error
	}
}

#print "\n"; # need endline to make things look nice

close FILE;

sub file_op { #do file operations
	if(-e $file ){ #does the file exist and is not empty?
		my $size=`stat -c %s $file`;
		if($size >= 1000){
			my $date=`date -u +%s`; #get current date in seconds
			my $created=`stat -c %Y $file`; #get creation date of file in seconds
			$age=$date - $created; #determine age of file
		}
		else{
			$age=$update+1;
		}
	}
	else{ #if file doesn't exist make it and set to update the file
		`touch $file`;
		$age=$update+1;
	}

	if ($age>=$update){ #only get a new file every hour
		#obtain the weather forecast and store it in $file
		`wget -O - http://weather.yahoo.com/forecast/"$code"_"$system".html > $file`;
	}
	open(FILE, $file) or die "Could not open file $file: $!\n";
}

sub usage { #if correct options haven't been passed usage error
		print "Usage error weather.pl <citycode> <system> <option>\n";
		print "weather.pl <citycode> <system> <option>\n";
		print "\t<citycode> - weather.com city code\n";
		print "\t<system> - c for metric or f for imperial\n";
		print "\t<option> - Only one option can be entered at a time\n";
		print "\t\tc displays current conditions\n";
		print "\t\tw displays list of current conditions\n";
		print "\t\tcp displays current conditions symbol\n";
		print "\t\tt displays current temp in chosen system\n";
		print "\t\t[1-5]d displays the days up to specified day\n"; 
		print "\t\t[1-5]dp displays condition symbol for days up to specified day\n";
		print "\t\t[1-5]t displays high/low temp in chosen system up to specified day\n";
		print "\t\t[1-5]dt displays days and then high/low temp in chosen system up to specified day\n";
		print "\t\t[1-7]w displays the weather in words up number specified\n";
		print "\t\t[1-5]p displays conditions for specified day\n";
		print "\t\t[1-5] displays high/low temp in chosen system for specified day\n";
}

sub cond_symb { #translates conditions into symbol in weather font
	if ($_ =~ "Partly Cloudy"){$_="c";}
	elsif ($_ =~ "Fair" || $_ =~ "Sun" || $_ =~ "Clear"){$_="A";}
	elsif ($_ =~ "Cloud" || $_ =~ "Fog"){$_="e";}
	elsif ($_ =~ "Storm" || $_ =~ "Thunder" || $_ =~ "T-"){$_="i";}
	elsif ($_ =~ "Snow" || $_ =~ "Flurries" || $_ =~ "Wintry"){$_="k";}
	elsif ($_ =~ "Rain" || "Drizzle"){$_="h";}
	elsif ($_ =~ "Shower"){$_="g";}
	$ctext.=$_.$fspaces;
}

sub day_space { #Adds spaces for aligment
	if ($_ =~ "Today"){$_="  Today ";}
	elsif ($_ =~ "Tonight"){$_="Tonight";}
	elsif ($_ =~ "Tomorrow"){$_="Tomorrow";}
	elsif ($_ =~ "Thu"){$_="   Thu  ";}
	elsif ($_ =~ "Fri"){$_="   Fri  ";}
	elsif ($_ =~ "Sat"){$_="   Sat  ";}
	elsif ($_ =~ "Sun"){$_="   Sun  ";}
	elsif ($_ =~ "Mon"){$_="   Mon  ";}
	elsif ($_ =~ "Tue"){$_="   Tue  ";}
	elsif ($_ =~ "Wed"){$_="   Wed  ";}
	$dtext.=$_.$dspaces;
}
The usage of the weather.pl script above is as follows (also see: Step 4)
Code:
weather.pl <citycode> <system> <option>
        <citycode> - weather.com city code
        <system> - c for metric or f for imperial
        <option> - Only one option can be entered at a time
                c displays current conditions
                w displays list of current conditions
                cp displays current conditions symbol
                t displays current temp in chosen system
                [1-5]d displays the days up to specified day
                [1-5]dp displays condition symbol for days up to specified day
                [1-5]t displays high/low temp in chosen system up to specified day
                [1-5]dt displays days and then high/low temp in chosen system up to specified day
                [1-7]w displays the weather in words up number specified
                [1-5]p displays conditions for specified day
                [1-5] displays high/low temp in chosen system for specified day
[1-5] means the range of numbers from 1 to 5. So if you want 5 days of conditions for Richmond VA USA in Fahrenheit use
Code:
weather.pl USVA0652 f 5dp
Step 3: Make weather.pl executable.
Code:
sudo chmod a+x ~/scripts/weather.pl
The script will make a file containing the html code from weather.com. The default is to create a new file every hour. You can adjust how often the file is updated through the $update variable in the script.

Step 4: Use the following code for your conky file, changing "USVA0652" for your city code or ZIP code if you live in the USA, Replace "f" with "c" if you want metric degrees.

Code:
# UBUNTU-CONKY.

# Update interval in seconds
update_interval 1.0

# Create own window instead of using desktop (required in nautilus)
own_window yes
own_window_type override #try also 'normal' or 'desktop' 
own_window_transparent yes
own_window_hints undecorated,below,sticky,skip_taskbar,skip_pager

# Use double buffering (reduces flicker, may not work for everyone)
double_buffer yes

# Minimum size of text area
#minimum_size 250 5
maximum_width 250

draw_shades no
draw_outline no # amplifies text if yes
draw_borders no
draw_graph_borders no

use_xft yes
xftalpha 0.9
xftfont Candera:size=6
uppercase no
override_utf8_locale yes
use_spacer no

stippled_borders 3
border_margin 9
border_width 10

# Gap between borders of screen and text
gap_x 10
gap_y 10

# Default colors and also border colors, grey90 == #e5e5e5
default_color grey
color1 orange

# Text alignment, other possible values are commented
#alignment top_left
alignment top_right
#alignment bottom_left
#alignment bottom_right

# stuff after 'TEXT' will be formatted on screen

TEXT
${color1}$hr
${voffset -3}$alignc CURRENT CONDITIONS 
${voffset -7}$hr $color
${execi 3600 perl ~/scripts/weather.pl USVA0652 f w}
${voffset -30}$alignr${offset -50}${execi 3600 perl ~/scripts/weather.pl USVA0652 f t}
${voffset -15}$alignr${offset -60}${font weather:size=45}${execi 3600 perl ~/scripts/weather.pl USVA0652 f cp}$font
${voffset -10}${color1}$hr
${voffset -3}$alignc 5 DAY FORECAST 
${voffset -7}$hr $color
${font weather:size=45}${execi 3600 perl ~/scripts/weather.pl USVA0652 f 5dp}${font}$color
${voffset -55}${execi 3600 perl ~/scripts/weather.pl USVA0652 f 5dt}
${execi 3600 perl ~/scripts/weather.pl USVA0652 f 7w}
This conkyrc should work without any changes to the current script. If you change things the formatting will have to be changed most likely. There are several different variables in the weather.pl script that are used in formatting. Spaces between each day's temps are controlled with the variables $leadingspace and $trailingspace (lines 17 and 18 ). There is also a variable for the spacing between each condition symbol, $fspaces (line 19). The spaces between each day can be controlled with the $dspaces variable (line 20). The number of lines between the days and temps, using the [1-5]dt option, can be controlled using the $lines variable (line 21), each /n represents one line.

The width for the list of weather for each day can be controlled using the $Text::Wrap::columns variable (line 23). The number represent the number of columns. The tab for each days initial line can be controlled with the $initial_tab variable (line 24), where /t represents a tab. Following line tabs can be controlled with $subsequent_tab variable.

NOTE 1: If the degree symbol is not printing correctly, it is most likely due to the font you are using.

NOTE 2: This script is very dependent on the html tagging used by weather.com, so when they change that tagging this script won't work any more. However, I plan on staying on top of things like that. Just post here if you have any issues.

NOTE 3: Please, give me feed back and let me know what you think would be a useful addition. I would like this script to be the end all of conky weather scripts. My main philosophy is to keep everything simple. I don't want to have outside dependencies, if I can help it.

Note 4:You should have
Code:
use_xft yes
set in your conkyrc.

Note 5:The text weather given by the option [1-7]w is only available in the imperial system.

UPDATE: 21 January 2008 Added the ability to print multi-day forecast with just two script runs; one for conditions and one for high/low. Also, added UTF-8 for the degree symbol.
UPDATE: 22 January 2008 Added the ability to put spaces between condition symbols.
UPDATE: 03 February 2008 Added the ability to see weather report for next 5 days, combined the days and temps into one script call (can still use the single calls), added list of current weather, added leading and trailing space for temps, and added wintry mix.
UPDATE: 12 February 2008 Added Fog as cloudy and Drizzle as Rain.

Thanks LazarusHC for your additions.
Attached Thumbnails
Click image for larger version

Name:	weather.png
Views:	7095
Size:	57.3 KB
ID:	58590  
Attached Files
File Type: gz weather.tar.gz (12.8 KB, 4370 views)

Last edited by lvleph; February 23rd, 2008 at 10:29 PM..
lvleph is offline   Reply With Quote
Old January 13th, 2008   #2
Bruce M.
Chocolate Ubuntu Mocha Blend
 
Bruce M.'s Avatar
 
Join Date: Sep 2007
Location: 34°35'S - 58°21'W
My beans are hidden!
Re: Conky Weather Revisited V2

Hi lvleph

I'm here, got your heads up, will check this out later, now I'm going to watch a movie with my wife.

Thanks
Bruce
Bruce M. is offline   Reply With Quote
Old January 14th, 2008   #3
Bruce M.
Chocolate Ubuntu Mocha Blend
 
Bruce M.'s Avatar
 
Join Date: Sep 2007
Location: 34°35'S - 58°21'W
My beans are hidden!
Re: Conky Weather Revisited V2

Hi lvleph

I have my first weather.pl conky working.
Looks good too, but a little heavy on CPU usage.

I made some "format" changes, weather symbols are smaller and then spaced to fit over the high/lows better:

Code:
${color cyan}$hr$color
${voffset -3}${alignc}${color orange}${font :size=10}Buenos Aires 5 Day Forcast$font$color

${voffset -5}${font weather:size=30}${alignc}${offset -45}${execi 3600 perl ~/scripts/lvlephw2.pl ARBA0009 c 1p}${offset 25}${color orange}${execi 3600 perl ~/scripts/lvlephw2.pl ARBA0009 c 2p}${offset 25}${color cyan}${execi 3600 perl ~/scripts/lvlephw2.pl ARBA0009 c 3p}${offset 24}${color green}${execi 3600 perl ~/scripts/lvlephw2.pl ARBA0009 c 4p}${offset 23}${color red}${execi 3600 perl ~/scripts/lvlephw2.pl ARBA0009 c 5p}${font}$color
${alignc}${offset -15}${execi 3600 perl ~/scripts/lvlephw2.pl ARBA0009 c 1}${offset 10}${execi 3600 perl ~/scripts/lvlephw2.pl ARBA0009 c 2}${offset 10}${execi 3600 perl ~/scripts/lvlephw2.pl ARBA0009 c 3}${offset 10}${execi 3600 perl ~/scripts/lvlephw2.pl ARBA0009 c 4}${offset 10}${execi 3600 perl ~/scripts/lvlephw2.pl ARBA0009 c 5}
${color cyan}$hr$color
Attached Thumbnails
Click image for larger version

Name:	Weather-pl.png
Views:	3151
Size:	9.8 KB
ID:	56450  

Last edited by Bruce M.; January 14th, 2008 at 01:25 PM..
Bruce M. is offline   Reply With Quote
Old January 14th, 2008   #4
lvleph
Has an Ubuntu Drip
 
Join Date: Jul 2006
Beans: 704
Ubuntu 9.04 Jaunty Jackalope
Re: Conky Weather Revisited V2

I am going to add an additional feature that will make less cpu intensive for multiday forecasts. But it may take some time, because school just started back up, and I am not use to my teaching schedule and my class schedule yet.
lvleph is offline   Reply With Quote
Old January 14th, 2008   #5
Bruce M.
Chocolate Ubuntu Mocha Blend
 
Bruce M.'s Avatar
 
Join Date: Sep 2007
Location: 34°35'S - 58°21'W
My beans are hidden!
Re: Conky Weather Revisited V2

Quote:
Originally Posted by lvleph View Post
I am going to add an additional feature that will make less cpu intensive for multiday forecasts. But it may take some time, because school just started back up, and I am not use to my teaching schedule and my class schedule yet.
That would be GREAT!
School? January? It's summ... OH! Up North, how soon we forget.

I'm a patient man.
Bruce

Code:
Are we there yet?
Bruce M. is offline   Reply With Quote
Old January 15th, 2008   #6
cdtech
Chocolate Ubuntu Mocha Blend
 
cdtech's Avatar
 
Join Date: Jan 2007
Location: ~/PA/USA
Beans: 1,984
Ubuntu 8.04 Hardy Heron
Re: Conky Weather Revisited V2

Hey Bruce, where did you get your fonts? I only have the black and white. I like the color you have.
Did you redo those yourself?

Been following this thread, seems to work good so far. I'm using the first script and still play'n around with it myself, doesn't seem to be buggie with my PC.

Never mind Bruce, I just got it (FONTS) lol, you can change the color of the fonts. I was thinking image.....duh!

Last edited by cdtech; January 15th, 2008 at 03:41 AM..
cdtech is offline   Reply With Quote
Old January 15th, 2008   #7
lvleph
Has an Ubuntu Drip
 
Join Date: Jul 2006
Beans: 704
Ubuntu 9.04 Jaunty Jackalope
Re: Conky Weather Revisited V2

For some reason the first script had to be run through another script. This was because conky couldn't handle the functions in the bash script. I could have always taken them out, but it was harder to read, ugly, and harder to make changes. This version should be a bit faster than the old version. When I finish the new update one can use just two scripts for 5 days versus 10 scripts for 5 days. This, I hope will help with slower computers. But the formatting of one's weather section in conky will have to be very specific. Looks like even in conky sometimes you have to sacrifice beauty for performance.
lvleph is offline   Reply With Quote
Old January 15th, 2008   #8
lvleph
Has an Ubuntu Drip
 
Join Date: Jul 2006
Beans: 704
Ubuntu 9.04 Jaunty Jackalope
Re: Conky Weather Revisited V2

Anyone want to beta test the new version? There are very specific setup criterion for this version. It requires specific font sizes and positioning. I may be able to mess with that a bit, so that the user can input those, but right now there is no way of adjusting besides trial and error in your .conkyrc Anyway, to test it make a new file weather_test.pl in ~/scripts. Now make it executable. If the output is not positioned correctly inside conky mess with the offset. If the temps don't line up correctly you will have to add or subtract spaces from the print on line 76 and most likely adjust the offset in conky.
Code:
chmod a+x ~/scripts/weather_test.pl
Then copy the following code into that file
PHP Code:
#!/usr/bin/perl

use Switch;

# This script was written by lvleph and inspired by the original conky weather script written by azhag (azhag@bsd.miki.eu.org)

$code=$ARGV[0]; #zipcode or weather.com city code
$system=$ARGV[1]; #f for imperial c for metric
$what=$ARGV[2]; #what are we looking for?
$file="/tmp/weather.html"#temp holding weather
$update=3600#time in seconds to update }le if set to 0 don't use }le

#ensure user inputs proper system
if($system !=~ "c" || $system !=~ "f"){$what=0;} #this will give usage error 

if(-e $file){ #does the file exist?
    
$date=`date -u +%s`; #get current date in seconds
    
$created=`stat -c %Y $file`; #get creation date of file in seconds
    
$age=$date $created#determine age of file
}
else{ 
#if file doesn't exist make it and set to update the file
    
`touch $file`;
    
$age=$update+1;
}

if (
$age>=$update){ #only get a new file every hour
    #obtain the weather forecast and store it in $file
    
`wget -O - http://weather.yahoo.com/forecast/"$code"_"$system".html > $file`;
}

open(FILE"$file") or die "Cannot open file $file: $1\n";

$count=0#set count so we can match proper day
$days=(split /d/, $what)[0]; #get number of days

switch($what){ #determine what user wants
    
case "cp" #if current conditions
        
while(<FILE>){ #cycle through file
            
if (/<em>Current conditions/ .. /<h3>/){ #found current conditions
                
($cnd) = /<h3>(b.+b)</h3>/; #save current conditions
                #do we have current conditions? Then translate into symbol
                
if($cnd){cond_symb($cnd); exit;}
            }
        }
    }    
    case 
"c" #if current temp
        
while(<FILE>){ #cycle through file
            
if (/<div id="forecast-temperature">/ .. /<h3>/){ #found current temp
                
($tmp) = /<h3>(-?d+)/; #save current temp
                #do we have current temp? Then print
                
if($tmp){print "$1°\n"; exit;}
            }
        }
    }
    case /[
1-5]dp/ { #display the conditions from today through day $days
        
$day=(split "p"$what)[0]; #how many days are we looking for
        
$flag=0#set flag for when we find start of conditions
        
$count=0
        while(<
FILE>){
            if(/^<
tr class="titles">s*$/){$flag=1;} #found the start of conditions
            
elsif($flag && /(b.+b)</td>/ && ++$count<=$day){ #look for the conditions upto specified day
                
$cnd[$count-1]=$1#save conditions
                
&cond_symb ($cnd[$count-1]); #translate conditions to symbol
                #exit;
            
}
        }
    }
    case /[
1-5]d/ { #display the temps from today through day $days
        
while(<FILE>){
            
#get the high temp
            
$day=(split "p"$what)[0]; #how many days are we looking for
            
($high) = /<td><strong>High: (-?d+)&deg;</strong><span>Low: -?d+&deg;</span></td>/;
            
#get the low temp
            
($low) = /<td><strong>High: -?d+&deg;</strong><span>Low: (-?d+)&deg;</span></td>/;
            
#print the high and low temp for the specified day
            
if($high && $low && ++$count<=$day){print "$high°/$low°     ";}
        }
    }
    case /[
1-5]p/ { #if conditions of specified day
        
$day=(split "p"$what)[0]; #what day are we looking for
        
$flag=0#set flag for when we find start of conditions
        
$count=0
        while(<
FILE>){
            if(/^<
tr class="titles">s*$/){$flag=1;} #found the start of conditions
            
elsif($flag && /(b.+b)</td>/ && ++$count==$day){ #look for the conditions for specified day
                
$cnd=$1#save conditions
                
&cond_symb ($cnd); #translate conditions to symbol
                #exit;
            
}
        }
    }
    case /[
1-5]/ { #if temp of specified day 
        
while(<FILE>){
            
#get the high temp
            
($high) = /<td><strong>High: (-?d+)&deg;</strong><span>Low: -?d+&deg;</span></td>/;
            
#get the low temp
            
($low) = /<td><strong>High: -?d+&deg;</strong><span>Low: (-?d+)&deg;</span></td>/;
            
#print the high and low temp for the specified day
            
if($high && $low && ++$count==$what){print "$high°/$low°";}
        }
    }
    else { 
#didn't give proper options
        
print "Usage error weather.pl <citycode> <system> <option>
weather.pl <citycode> <system> <option>
\t<citycode> - weather.com city code
\t<system> - c for metric or f for imperial
\t<option> - Only one option can be entered at a time
\t\tcp displays current conditions
\t\t1p displays today's conditions
\t\t2p displays tomorrow's conditions
\t\t3p displays 3rd day's conditions
\t\t4p displays 4th day's conditions
\t\t5p displays 5th day's conditions
\t\tc displays current temp in chosen system
\t\t1 displays today's high/low temp in chosen system
\t\t2 displays tomorrow's high/low temp in chosen system
\t\t3 displays 3rd day's high/low temp in chosen system
\t\t4 displays 4th day's high/low temp in chosen system
\t\t5 displays 5th day's high/low temp in chosen system"
    
}
}

sub cond_symb #translates conditions into symbol in weather font
    
if ($_ =~ "Partly Cloudy"){$_="c";}
    
elsif ($_ =~ "Fair" || $_ =~ "Sun" || $_ =~ "Clear"){$_="A";}
    
elsif ($_ =~ "Cloud"){$_="e";}
    
elsif ($_ =~ "Storm" || $_ =~ "Thunder" || $_ =~ "T-"){$_="i";}
    
elsif ($_ =~ "Snow" || $_ =~ "Flurries"){$_="k";}
    
elsif ($_ =~ "Rain"){$_="h";}
    
elsif ($_ =~ "Shower"){$_="g";}
    print 
"$_";
}

print 
"\n"# need endline to make things look nice

close FILE
Place the following in your .conkyrc, don't forget to backup the original.
Code:
${voffset -5}${font weather:size=45}${alignc}${execi 3600 perl ~/scripts/weather_test.pl USVA0652 f 5dp}${font}$color
${font Candera:size=6}${offset 18}${execi 3600 perl ~/scripts/weather_test.pl USVA0652 f 5d}
Don't forget if you change the fonts you will have to play with the formatting in the script, specifically line 76, by adding or removing spaces.

Oh and I found a couple bugs in the original code.
1. If your connection is down when weather first tries to get the forecast it creates a blank file and tries to read from that file for the next hour until it can update it again, even if you restart conky. I will have to write a portion of the code that checks the size of the file.
2. The user can input 5dps and it will just output the 5d output. This is from the way I check cases. This may take some thinking and so it will take longer than problem 1.

Last edited by lvleph; January 15th, 2008 at 10:47 AM..
lvleph is offline   Reply With Quote
Old January 15th, 2008   #9
cdtech
Chocolate Ubuntu Mocha Blend
 
cdtech's Avatar
 
Join Date: Jan 2007
Location: ~/PA/USA
Beans: 1,984
Ubuntu 8.04 Hardy Heron
Re: Conky Weather Revisited V2

I tried the test script but got tons of errors. I will check them out when I get a little more time though (work time). The weather.pl works good so far, the only issue I have is the character format for the degree symbol, I've tried everything to no avail. I would really like to have that.
cdtech is offline   Reply With Quote
Old January 15th, 2008   #10
Bruce M.
Chocolate Ubuntu Mocha Blend
 
Bruce M.'s Avatar
 
Join Date: Sep 2007
Location: 34°35'S - 58°21'W
My beans are hidden!
Thumbs down Re: Conky Weather Revisited V2

Hi cdtech,

I have to do this.
I know I don't have to, but I really do. It's the little devil in me.
So what follows are the thoughts that went through my head as I read each statement.

Quote:
Originally Posted by cdtech View Post
Hey Bruce, where did you get your fonts? I only have the black and white. I like the color you have.
Did you redo those yourself?
What? I downloaded them from here, like anyone else that's trying this script. Did he miss the icon for downloading in the first post? Black and white?
DUH, use the colour command!

Quote:
Originally Posted by cdtech View Post
Been following this thread, seems to work good so far. I'm using the first script and still play'n around with it myself, doesn't seem to be buggie with my PC.
Yup, same here. Works just fine.

Quote:
Originally Posted by cdtech View Post
Never mind Bruce, I just got it (FONTS) lol, you can change the color of the fonts. I was thinking image.....duh!
{ had a really good chuckle here }
------------------------
To continue in the present...
Thanks for the chuckle. It did me good.

Don't you just love it when something is staring you in the face and you can't see it because our mind tries to complicate things.

NO!

Bruce
Bruce M. is offline   Reply With Quote

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 06:33 AM.


vBulletin ©2000 - 2009, Jelsoft Enterprises Ltd. Ubuntu Logo, Ubuntu and Canonical © Canonical Ltd. Tango Icons © Tango Desktop Project. bilberry