SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2009-03-16 17:45:27

ottomar
Member
From: France, Vesoul
Registered: 2009-02-20
Posts: 44
Website

weather pipemenu .fr parse error ?

well ! I'll try to be clear, net and accurate in the Shakespeare language !

try this in your urlbar : http://www.google.fr/ig/api?weather=vesoul
you get a clear, net and accurate xml with my weather-forecasts, in french & C°

see my pipemenu command :

python ~/.config/openbox/scripts/gweather.py vesoul

see now my ~/.config/openbox/scripts/gweather.py

#!/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='Actuel' />"
        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.fr/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())

correct, no ?
but I get sometimes no pipemenu or sometimes this alert
k0t9bzj11d86lk533t-weather-parse.png

the only way for me to get a forecast in C° is to connext to http://www.google.nl/ig/api?weather= (Netherlands)
h9u8elhnanh5jmhld0-weather-nl.png

I do love my some nl-friends, I know that Onbewolk means 'nuageux' but where is the problem ?
thanks for your help

Offline

Help fund CrunchBang, donate to the project!

#2 2009-03-25 22:08:41

ADcomp
Python Script Master
From: Belgium
Registered: 2008-12-22
Posts: 600
Website

Re: weather pipemenu .fr parse error ?

salut,

* Il y'a un prob dans le format on dirait .. Il commence bien la sortie, mais il bug sur la fin ..

david@acer-desktop:~$ python weather.py vesoul
<openbox_pipe_menu>
<separator label='Vesoul, Franche-Comte' />
<separator label='Actuel' />
<item label='Weather: Nuageux dans l'ensemble' />
<item label='Temperature 4 C' />
<item label='Humidite : 100%' />
Traceback (most recent call last):
..
..
    raise exception
xml.sax._exceptions.SAXParseException: <unknown>:1:597: not well-formed (invalid token)

et google me dit :

xml.sax._exceptions.SAXParseException: <unknown>:x:y: not well-formed ...

Despite the best efforts of the preParsers there is badly formed XML within the document; possibly a malformed tag, or character entity.
ACTION: Check the document source at line x, column y.

après un petit test , c'est le " à " dans cette ligne :

<wind_condition data="Vent : O à 23 km/h"/>

Solution , remplace cette ligne dans ton script :

trans=maketrans("\xe1\xe9\xed\xf3\xfa","aeiou")

par celle-ci ( simple ajout de la conversion 'à' -> 'a' )

trans=maketrans("\xe1\xe9\xed\xf3\xfa\xe0","aeioua")

Bien à toi , @pluche


David [aka] ADcomp

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.

Debian Logo