using Weboob.do()

This commit is contained in:
Romain Bignon 2010-04-29 15:50:11 +02:00
commit d900375a39

View file

@ -18,6 +18,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
""" """
from weboob import CallErrors
from weboob.capabilities.weather import ICapWeather, CityNotFound from weboob.capabilities.weather import ICapWeather, CityNotFound
from weboob.tools.application import ConsoleApplication from weboob.tools.application import ConsoleApplication
@ -37,10 +38,9 @@ class WetBoobs(ConsoleApplication):
print '| ID | Name |' print '| ID | Name |'
print '+--------------------------------+---------------------------------------------+' print '+--------------------------------+---------------------------------------------+'
count = 0 count = 0
for backend in self.weboob.iter_backends(): for backend, city in self.weboob.do('iter_city_search', pattern):
for city in backend.iter_city_search(pattern): print u'| %-31s| %-44s|' % (city.city_id, city.name)
print u'| %-31s| %-44s|' % (city.city_id, city.name) count += 1
count += 1
print "+--------------------------------'---------------------------------------------+" print "+--------------------------------'---------------------------------------------+"
print "| %3d cities listed |" % count print "| %3d cities listed |" % count
print "'------------------------------------------------------------------------------'" print "'------------------------------------------------------------------------------'"
@ -51,14 +51,17 @@ class WetBoobs(ConsoleApplication):
print '| Temperature | Text |' print '| Temperature | Text |'
print '+-------------+----------------------------------------------------------------+' print '+-------------+----------------------------------------------------------------+'
found = 0 found = 0
for backend in self.weboob.iter_backends(): try:
try: for backend, current in self.weboob.do('get_current', city):
current = backend.get_current(city)
print u'| %-12s| %-63s|' % (u'%d °%s' % (current.temp, current.unit), current.text) print u'| %-12s| %-63s|' % (u'%d °%s' % (current.temp, current.unit), current.text)
found = 1 found = 1
except CityNotFound: except CallErrors, e:
if not found: for error in e:
found = -1 if isinstance(error, CityNotFound):
if not found:
found = -1
else:
raise error
if found < 0: if found < 0:
print "| -- | City not found |" print "| -- | City not found |"
print "+-------------'----------------------------------------------------------------+" print "+-------------'----------------------------------------------------------------+"
@ -69,17 +72,20 @@ class WetBoobs(ConsoleApplication):
print '| Date | Min | Max | Text |' print '| Date | Min | Max | Text |'
print '+-------------+-------+-------+------------------------------------------------+' print '+-------------+-------+-------+------------------------------------------------+'
found = 0 found = 0
for backend in self.weboob.iter_backends(): try:
try: for backend, f in self.weboob.do('iter_forecast', city):
for f in backend.iter_forecast(city): found = 1
found = 1 print u'| %-12s|%6s |%6s | %-47s|' % (f.date,
print u'| %-12s|%6s |%6s | %-47s|' % (f.date, u'%d °%s' % (f.low, f.unit),
u'%d °%s' % (f.low, f.unit), u'%d °%s' % (f.high, f.unit),
u'%d °%s' % (f.high, f.unit), f.text)
f.text) except CallErrors, e:
except CityNotFound: for error in e:
if not found: if isinstance(error, CityNotFound):
found = -1 if not found:
found = -1
else:
raise error
if found < 0: if found < 0:
print "| -- --- ---- | -- | -- | City not found |" print "| -- --- ---- | -- | -- | City not found |"
print "+-------------'-------'-------'------------------------------------------------+" print "+-------------'-------'-------'------------------------------------------------+"