You are not logged in.

#1 2008-02-04 23:14:06

noalwin
Member
From: Spain
Registered: 2007-06-08
Posts: 115

Openbox Weather Pipe Menu

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:

obweatherpipemenuun8.th.jpg

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

#2 2008-02-05 00:49:13

finferflu
Forum Fellow
From: Manchester, UK
Registered: 2007-06-21
Posts: 1,899
Website

Re: Openbox Weather Pipe Menu

I don't use OpenBox, but this script looks quite cool. I love these kind of things big_smile

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

#3 2008-02-14 20:13:50

syme
Member
Registered: 2007-02-27
Posts: 21

Re: Openbox Weather Pipe Menu

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

#4 2008-03-19 18:48:05

Reasons
Member
From: Washington
Registered: 2007-11-04
Posts: 572

Re: Openbox Weather Pipe Menu

If anyone has the script could they post it here, both links are giving me a 404.
Thanks.

Offline

#5 2008-03-19 19:00:26

noalwin
Member
From: Spain
Registered: 2007-06-08
Posts: 115

Re: Openbox Weather Pipe Menu

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

#6 2008-03-19 19:00:31

.:B:.
Forum Fellow
Registered: 2006-11-26
Posts: 5,819
Website

Re: Openbox Weather Pipe Menu

Man this is one awesome script big_smile. I will throw out the script that conky is parsing for me now and implement yours tongue.

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

#7 2008-03-19 19:06:29

Reasons
Member
From: Washington
Registered: 2007-11-04
Posts: 572

Re: Openbox Weather Pipe Menu

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

#8 2008-03-19 19:12:14

.:B:.
Forum Fellow
Registered: 2006-11-26
Posts: 5,819
Website

Re: Openbox Weather Pipe Menu

Sorry guys I was a bit confused about the initial author tongue.

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

#9 2008-03-19 19:20:42

Reasons
Member
From: Washington
Registered: 2007-11-04
Posts: 572

Re: Openbox Weather Pipe Menu

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

Last edited by Reasons (2008-03-19 19:26:29)

Offline

#10 2008-03-19 19:27:06

.:B:.
Forum Fellow
Registered: 2006-11-26
Posts: 5,819
Website

Re: Openbox Weather Pipe Menu

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

[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

#11 2008-03-19 19:35:56

noalwin
Member
From: Spain
Registered: 2007-06-08
Posts: 115

Re: Openbox Weather Pipe Menu

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

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

#12 2008-03-19 19:37:29

noalwin
Member
From: Spain
Registered: 2007-06-08
Posts: 115

Re: Openbox Weather Pipe Menu

B wrote:

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

[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

#13 2008-03-19 19:40:46

Reasons
Member
From: Washington
Registered: 2007-11-04
Posts: 572

Re: Openbox Weather Pipe Menu

noalwin wrote:
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. wink

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

#14 2008-03-19 20:19:19

noalwin
Member
From: Spain
Registered: 2007-06-08
Posts: 115

Re: Openbox Weather Pipe Menu

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

obweatherpipezm5.th.png

EDIT3: Added the storm link

Last edited by noalwin (2008-03-19 21:50:51)

Offline

#15 2008-03-19 20:21:33

noalwin
Member
From: Spain
Registered: 2007-06-08
Posts: 115

Re: Openbox Weather Pipe Menu

Reasons wrote:

EDIT2 - You also have the Fahrenheit set to Celsius. wink

Fixed big_smile

Offline

#16 2008-03-19 20:35:27

Reasons
Member
From: Washington
Registered: 2007-11-04
Posts: 572

Re: Openbox Weather Pipe Menu

noalwin wrote:

Keep in mind that the extended day forecast will not work.

Why not?

Offline

#17 2008-03-19 20:47:18

noalwin
Member
From: Spain
Registered: 2007-06-08
Posts: 115

Re: Openbox Weather Pipe Menu

Reasons wrote:
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

#18 2008-03-19 21:52:44

noalwin
Member
From: Spain
Registered: 2007-06-08
Posts: 115

Re: Openbox Weather Pipe Menu

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

#19 2008-03-19 22:13:05

Reasons
Member
From: Washington
Registered: 2007-11-04
Posts: 572

Re: Openbox Weather Pipe Menu

IT works but it is really slow and makes my CPU jump, is that just me?

Offline

#20 2008-03-19 22:26:20

noalwin
Member
From: Spain
Registered: 2007-06-08
Posts: 115

Re: Openbox Weather Pipe Menu

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

#21 2008-03-19 22:30:55

Reasons
Member
From: Washington
Registered: 2007-11-04
Posts: 572

Re: Openbox Weather Pipe Menu

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

#22 2008-03-19 22:39:29

noalwin
Member
From: Spain
Registered: 2007-06-08
Posts: 115

Re: Openbox Weather Pipe Menu

Reasons wrote:

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

#23 2008-03-19 22:42:10

Reasons
Member
From: Washington
Registered: 2007-11-04
Posts: 572

Re: Openbox Weather Pipe Menu

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

#24 2008-03-19 23:02:45

Reasons
Member
From: Washington
Registered: 2007-11-04
Posts: 572

Re: Openbox Weather Pipe Menu

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

#25 2008-03-19 23:05:25

noalwin
Member
From: Spain
Registered: 2007-06-08
Posts: 115

Re: Openbox Weather Pipe Menu

Reasons wrote:

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.

Well, use google script then. It's innacurate?

It's very strange the difference between the two weatherget executions.

Offline

Board footer

Powered by FluxBB