You are not logged in.
Important: Here there is an updated version of the script. And here there is a version that uses the the yahoo api instead google api.
Hi, I have playing today with python and I have done my first script: an OpenBox pipe menu that shows the weather in your location:
To add it to your menus, simply add
<menu id="pipe-weather" label="Weather" execute="python ~/.config/openbox/scripts/gweather.py YOURCITY" />
The script
#!/usr/bin/python
import sys
import urllib
from string import maketrans
#from xml.sax import make_parser, handler
from xml.sax import handler, parseString
class ElementProcesser(handler.ContentHandler):
def startElement(self, name, attrs):
if name == "city":
print "<separator label='" + attrs["data"] + "' />"
elif name == "current_conditions":
print "<separator label='Current condidtions' />"
elif name == "condition":
print "<item label='Weather: " + attrs["data"] + "' />"
elif name == "humidity":
print "<item label='" + attrs["data"] + "' />"
elif name == "wind_condition":
print "<item label='" + attrs["data"] + "' />"
elif name == "day_of_week":
print "<separator label='" + self.getDayOfWeek(attrs["data"]) + "' />"
#Celsius
#elif name == "temp_c":
#print "<item label='Temperature " + attrs["data"] + " C' />"
#elif name == "low":
#print "<item label='Minimun " + attrs["data"] + " C' />"
#elif name == "high":
#print "<item label='Maximun " + attrs["data"] + " C' />"
#Fahrenheit
elif name == "temp_f":
print "<item label='Temperature " + attrs["data"] + " F' />"
elif name == "low":
print "<item label='Minimun " + attrs["data"] + " F' />"
elif name == "high":
print "<item label='Maximun " + attrs["data"] + " F' />"
def endElement(self, name):
if name == "current_conditions":
print "<separator label='Forecast' />"
def startDocument(self):
print '<openbox_pipe_menu>'
def endDocument(self):
print '</openbox_pipe_menu>'
def getDayOfWeek(self,day):
#English
if day == "Mon":
return "Monday"
elif day == "Tue":
return "Tuesday"
elif day == "Wed":
return "Wednesday"
elif day == "Thu":
return "Thursday"
elif day == "Sat":
return "Saturday"
elif day == "Sun":
return "Sunday"
else:
return day
# You should use your local version of google to have the messages in your language and metric system
f = urllib.urlopen("http://www.google.com/ig/api?weather="+sys.argv[1])
xml = f.read()
f.close()
#Avoid problems with non english characters
trans=maketrans("\xe1\xe9\xed\xf3\xfa","aeiou")
xml = xml.translate(trans)
#parser.parse("http://www.google.es/ig/api?weather="+sys.argv[1])
parseString(xml,ElementProcesser())
And another version based on weatherget: http://bbs.archlinux.org/viewtopic.php? … 56#p344156
Last edited by noalwin (2009-07-17 20:54:02)
Offline
I don't use OpenBox, but this script looks quite cool. I love these kind of things
Good job!
Have you Syued today?
Free music for free people! | Earthlings
"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- A. de Saint-Exupery
Offline
very nice!
you should submit this to the pipemenu section of the openbox site, there aren't very many scripts there.
I'm sure it would get more attention there.
[EDIT] linky http://icculus.org/openbox/index.php/Openbox:Pipemenus
Last edited by syme (2008-02-14 20:15:18)
Don't fight it, it's gonna happen.
Offline
If anyone has the script could they post it here, both links are giving me a 404.
Thanks.
Offline
I added the script to the original message and deleted the link to the pastebin.
Last edited by noalwin (2008-03-19 19:03:42)
Offline
Man this is one awesome script . I will throw out the script that conky is parsing for me now and implement yours .
Do you have some hosting to put the script on noalwin? If not i'll upload it to my webspace and link to it. I assume the Arch pastebin doesn't keep it forever.
Last edited by B (2008-03-19 19:15:45)
Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy
Offline
I'm actually getting a small home server today or tomorrow. Just for torrentflux as I know how to get that but I might throw in a small arch section.
Offline
Sorry guys I was a bit confused about the initial author .
Last edited by B (2008-03-19 19:16:55)
Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy
Offline
How hard would it be to make it use weather.com instead of igoogle weather? They're 7F degrees different.
EDIT - Now if I can get a working gmail one I'm set.
EDIT2 - You also have the Fahrenheit set to Celsius.
Last edited by Reasons (2008-03-19 19:26:29)
Offline
Noalwin if I'm correct I just comment out the Celsius part to get Celsius right? I don't know how much interest there would be in localised versions . If anyone wants Dutch, just ask.
Also, is there a way to make it parse the degree sign (°)? Scripts borks about non-ASCII if I put it in .
[stijn@lysithea scripts]$ python gweather.py sint-niklaas
File "gweather.py", line 27
SyntaxError: Non-ASCII character '\xc2' in file gweather.py on line 27, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
Last edited by B (2008-03-19 19:30:33)
Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy
Offline
How hard would it be to make it use weather.com instead of igoogle weather? They're 7F degrees different.
EDIT - Now if I can get a working gmail one I'm set.
EDIT2 - You also have the Fahrenheit set to Celsius.
The script works for me
About weather.com, I think that there were some done but I can't find them. My city isn't there but it was on google, so I did my own script.
Anyway, if you what do do your own script using weathe.com, weatherget will help you
Offline
Noalwin if I'm correct I just comment out the Celsius part to get Celsius right? I don't know how much interest there would be in localised versions . If anyone wants Dutch, just ask.
Also, is there a way to make it parse the degree sign (°)? Scripts borks about non-ASCII if I put it in .
[stijn@lysithea scripts]$ python gweather.py sint-niklaas File "gweather.py", line 27 SyntaxError: Non-ASCII character '\xc2' in file gweather.py on line 27, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
Sure, but my python knowledge is too small, so I don't know the way to write º.
EDIT: I have been testing and I think that openbox can't render non-ascii characters
Last edited by noalwin (2008-03-19 20:39:38)
Offline
Reasons wrote:How hard would it be to make it use weather.com instead of igoogle weather? They're 7F degrees different.
EDIT - Now if I can get a working gmail one I'm set.
EDIT2 - You also have the Fahrenheit set to Celsius.The script works for me
About weather.com, I think that there were some done but I can't find them. My city isn't there but it was on google, so I did my own script.
Anyway, if you what do do your own script using weathe.com, weatherget will help you
I mean that this part
#Fahrenheit
elif name == "temp_c":
should be
#Fahrenheit
elif name == "temp_f":
Offline
You can use weatherget to get the info from weather.com and use it as a pipe menu. You will need a custom weatherrc file
For example for New York you can use:
<menu id="pipe-weather-myplace" label="Tiempo (Pa)" execute="weatherget -m -s USNY0996 -f /home/user/mycustomweathergetrc" />
And the config file
#
# ~/.weathergetrc
#
# NOTE: this must follow python syntax which means you cannot alter the indentions or you will experience tracebacks!
#
#### TERMINAL COLORS ###
# For colored output use term_code('<TERM-KEY>'). To get a current list of all available keys, run weatherget --term-keys.
# To print the text 'example' underlined, bold, and blue:
# print term_code('underline', 'bold', 'fg_blue')+'example'+term_code('default')
# This feature is not supported on Windows machines.
# Refer to the 'TERMINAL COLORS' section of the manpage for more information.
#
# current conditions
def current():
print '<openbox_pipe_menu>'
print ' <separator label="'+des_name+'"/>'
print ' <item label="Temperature: '+temp+' '+temp_units+'" />'
print ' <item label="Feels Like: '+feels_like+' '+temp_units+'" />'
print ' <item label="Conditions: '+cloud_type+'" />'
print ' <item label="Wind: '+wind_speed+' '+speed_units+', '+wind_gust+' '+speed_units+' gusts, '+wind_dir+'" />'
print ' <item label="Visibility: '+visibility+' '+dist_units+'" />'
print ' <item label="Humidity: '+humidity+' %'+'" />'
print ' <item label="Barometer: '+barom_psr+' '+pressure_units+', '+barom_state+'" />'
print ' <item label="Dewpoint: '+dewpoint+' '+temp_units+'" />'
print ' <item label="UV: '+uv_state+', '+uv_index+'" />'
print ' <item label="Sunrise: '+sunrise+'" />'
print ' <item label="Sunset: '+sunset+'" />'
print ' <item label="Moon: '+moon_state+'" />'
print ' <item label="Time Zone: '+time_zone+' GMT'+'" />'
if is_storm:
print ' <item label="'+storm_watch+'">'
print '<action name="Execute"><command>xdg-open "'+storm_link.replace('&','&')+'"</command></action>'
print '</item>'
if not EXTENDED_DAY:
print '</openbox_pipe_menu>'
# print ''
# extended day forecast
def extended():
print ' <separator label="'+weekday+'"/>'
print ' <item label="High: '+high_temp+' '+temp_units+'" />'
print ' <item label="Low: '+low_temp+' '+temp_units+'" />'
print ' <item label="Conditions: '+cloud_type+'" />'
print ' <item label="Precipitation: '+precipitation+' %'+'" />'
print ' <item label="Wind: '+wind_speed+' '+speed_units+', '+wind_gust+' '+speed_units+' gusts, '+wind_dir+'" />'
print ' <item label="Humidity: '+humidity+' %'+'" />'
print ' <item label="Sunrise: '+sunrise+'" />'
print ' <item label="Sunset: '+sunset+'" />'
if num==(int(NUMDAYS)-1):
print '</openbox_pipe_menu>'
#print ''
Keep in mind that the extended day forecast will not work.
EDIT: extended day forecast should work now
EDIT2: Because we all like screenshots...
EDIT3: Added the storm link
Last edited by noalwin (2008-03-19 21:50:51)
Offline
EDIT2 - You also have the Fahrenheit set to Celsius.
Fixed
Offline
Keep in mind that the extended day forecast will not work.
Why not?
Offline
noalwin wrote:Keep in mind that the extended day forecast will not work.
Why not?
Because it prints in this way:
current
extended
entended
extended
where current is the current weather (printed by current()) and extended is the forecast (printed by extended())
The entire output has to be between <openbox_pipe_menu> tags and that can't be done in confg file, it will require to do a small change to weatherget's code.
Edit: I'm wrong, I fixed the configuration, now it works with extended day forecast
Last edited by noalwin (2008-03-19 21:07:24)
Offline
Ok, after add the extended day forecast I added the storm warning. Clickig the storm warning should open the browser with the alert page. To choose the browser I use xdg-open (xdg-utils package)
Offline
IT works but it is really slow and makes my CPU jump, is that just me?
Offline
weather.com script:
real 0m1.498s
user 0m1.077s
sys 0m0.133s
google script:
real 0m0.543s
user 0m0.157s
sys 0m0.053s
So, yes weatherget-based script it's very slow (1.077s) if we compare it with the google one (0.157s)
Keep in mind that the weatherget is more complex and configurable.
By the way, it seems that in my case it spends 0.5s fetching the information from internet
Last edited by noalwin (2008-03-19 22:27:15)
Offline
It takes about five seconds on my computer. That is just me counting though and watching the music time. Of course my internet maxes out at 160kbs too.
With the time command I've gotten anything from 2.6 to 9.2
Last edited by Reasons (2008-03-19 22:40:39)
Offline
It takes about five seconds on my computer. That is just me counting though and watching the music time. Of course my internet maxes out at 160kbs too.
I have 6144kbps down / 320kbps up
You can measure the scripts using:
time ./google-weather.py SomeCity
time weatherget -m -s SomeCityCode -f ~/weathergetconfigg -e 2
Last edited by noalwin (2008-03-19 22:39:48)
Offline
Okay, my times are
Googlereal
0m0.479s
user 0m0.057s
sys 0m0.017s
Weatherget
real 0m5.869s
user 0m3.973s
sys 0m3.436s
Weatherget again
real 0m13.430s
user 0m8.783s
sys 0m6.520s
Which really is a big difference.
Last edited by Reasons (2008-03-19 22:43:34)
Offline
Just because this is slightly related, I got the gmail menu to work, but every time it opens, it gets put in /tmp/.gmail-cache and then won't work again until that is deleted. Can that be fixed so it doesn't have to? It's nice to have because it makes loading less.
Last edited by Reasons (2008-04-08 05:24:45)
Offline
Okay, my times are
Googlereal
0m0.479s
user 0m0.057s
sys 0m0.017sWeatherget
real 0m5.869s
user 0m3.973s
sys 0m3.436sWeatherget again
real 0m13.430s
user 0m8.783s
sys 0m6.520sWhich really is a big difference.
Well, use google script then. It's innacurate?
It's very strange the difference between the two weatherget executions.
Offline