[cuisineaz] pep8fication

This commit is contained in:
Julien Veyssier 2013-03-16 00:51:45 +01:00
commit f0abb61123
4 changed files with 61 additions and 49 deletions

View file

@ -17,7 +17,7 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>. # along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.capabilities.recipe import ICapRecipe,Recipe from weboob.capabilities.recipe import ICapRecipe, Recipe
from weboob.tools.backend import BaseBackend from weboob.tools.backend import BaseBackend
from .browser import CuisineazBrowser from .browser import CuisineazBrowser
@ -46,16 +46,16 @@ class CuisineazBackend(BaseBackend, ICapRecipe):
def fill_recipe(self, recipe, fields): def fill_recipe(self, recipe, fields):
if 'nb_person' in fields or 'instructions' in fields: if 'nb_person' in fields or 'instructions' in fields:
rec = self.get_recipe(recipe.id) rec = self.get_recipe(recipe.id)
recipe.picture_url = rec.picture_url recipe.picture_url = rec.picture_url
recipe.instructions = rec.instructions recipe.instructions = rec.instructions
recipe.ingredients = rec.ingredients recipe.ingredients = rec.ingredients
recipe.comments = rec.comments recipe.comments = rec.comments
recipe.nb_person = rec.nb_person recipe.nb_person = rec.nb_person
recipe.cooking_time = rec.cooking_time recipe.cooking_time = rec.cooking_time
recipe.preparation_time = rec.preparation_time recipe.preparation_time = rec.preparation_time
return recipe return recipe
OBJECTS = { OBJECTS = {
Recipe:fill_recipe, Recipe: fill_recipe,
} }

View file

@ -25,6 +25,7 @@ from .pages import RecipePage, ResultsPage
__all__ = ['CuisineazBrowser'] __all__ = ['CuisineazBrowser']
class CuisineazBrowser(BaseBrowser): class CuisineazBrowser(BaseBrowser):
DOMAIN = 'www.cuisineaz.com' DOMAIN = 'www.cuisineaz.com'
PROTOCOL = 'http' PROTOCOL = 'http'
@ -33,10 +34,11 @@ class CuisineazBrowser(BaseBrowser):
PAGES = { PAGES = {
'http://www.cuisineaz.com/recettes/recherche_v2.aspx\?recherche=.*': ResultsPage, 'http://www.cuisineaz.com/recettes/recherche_v2.aspx\?recherche=.*': ResultsPage,
'http://www.cuisineaz.com/recettes/.*[0-9]*.aspx': RecipePage, 'http://www.cuisineaz.com/recettes/.*[0-9]*.aspx': RecipePage,
} }
def iter_recipes(self, pattern): def iter_recipes(self, pattern):
self.location('http://www.cuisineaz.com/recettes/recherche_v2.aspx?recherche=%s' % (pattern.replace(' ','-'))) self.location('http://www.cuisineaz.com/recettes/recherche_v2.aspx?recherche=%s' % (
pattern.replace(' ', '-')))
assert self.is_on_page(ResultsPage) assert self.is_on_page(ResultsPage)
return self.page.iter_recipes() return self.page.iter_recipes()

View file

@ -30,43 +30,46 @@ class ResultsPage(BasePage):
""" Page which contains results as a list of recipies """ Page which contains results as a list of recipies
""" """
def iter_recipes(self): def iter_recipes(self):
for div in self.parser.select(self.document.getroot(),'div.rechRecette'): for div in self.parser.select(self.document.getroot(), 'div.rechRecette'):
thumbnail_url = NotAvailable thumbnail_url = NotAvailable
short_description = NotAvailable short_description = NotAvailable
imgs = self.parser.select(div,'img.photo') imgs = self.parser.select(div, 'img.photo')
if len(imgs) > 0: if len(imgs) > 0:
thumbnail_url = unicode(imgs[0].attrib.get('src','')) thumbnail_url = unicode(imgs[0].attrib.get('src', ''))
link = self.parser.select(div,'a.rechRecetTitle',1) link = self.parser.select(div, 'a.rechRecetTitle', 1)
title = unicode(link.text) title = unicode(link.text)
id = unicode(link.attrib.get('href','').split('/')[-1].replace('.aspx','')) id = unicode(link.attrib.get('href', '').split(
'/')[-1].replace('.aspx', ''))
short_description = u'' short_description = u''
ldivprix = self.parser.select(div,'div.prix') ldivprix = self.parser.select(div, 'div.prix')
if len(ldivprix) > 0: if len(ldivprix) > 0:
divprix = ldivprix[0] divprix = ldivprix[0]
nbprixneg = 0 nbprixneg = 0
spanprix = self.parser.select(divprix,'span') spanprix = self.parser.select(divprix, 'span')
if len(spanprix) > 0: if len(spanprix) > 0:
nbprixneg = unicode(spanprix[0].text).count(u'') nbprixneg = unicode(spanprix[0].text).count(u'')
nbprixtot = unicode(divprix.text_content()).count(u'') nbprixtot = unicode(divprix.text_content()).count(u'')
short_description += u'Cost: %s/%s ; '%(nbprixtot - nbprixneg, nbprixtot) short_description += u'Cost: %s/%s ; ' % (
nbprixtot - nbprixneg, nbprixtot)
short_description += unicode(' '.join(self.parser.select(div,'div.rechResume',1).text_content().split()).strip()).replace(u'','') short_description += unicode(' '.join(self.parser.select(
div, 'div.rechResume', 1).text_content().split()).strip()).replace(u'', '')
short_description += u' ' short_description += u' '
short_description += unicode(' '.join(self.parser.select(div,'div.rechIngredients',1).text_content().split()).strip()) short_description += unicode(' '.join(self.parser.select(
div, 'div.rechIngredients', 1).text_content().split()).strip())
recipe = Recipe(id,title) recipe = Recipe(id, title)
recipe.thumbnail_url = thumbnail_url recipe.thumbnail_url = thumbnail_url
recipe.short_description= short_description recipe.short_description = short_description
recipe.instructions = NotLoaded recipe.instructions = NotLoaded
recipe.ingredients = NotLoaded recipe.ingredients = NotLoaded
recipe.nb_person = NotLoaded recipe.nb_person = NotLoaded
recipe.cooking_time = NotLoaded recipe.cooking_time = NotLoaded
recipe.preparation_time = NotLoaded recipe.preparation_time = NotLoaded
yield recipe yield recipe
class RecipePage(BasePage): class RecipePage(BasePage):
""" Page which contains a recipe """ Page which contains a recipe
@ -81,42 +84,49 @@ class RecipePage(BasePage):
instructions = NotAvailable instructions = NotAvailable
comments = [] comments = []
title = unicode(self.parser.select(self.document.getroot(),'div#ficheRecette h1.fn.recetteH1',1).text) title = unicode(self.parser.select(
main = self.parser.select(self.document.getroot(),'div#ficheRecette',1) self.document.getroot(), 'div#ficheRecette h1.fn.recetteH1', 1).text)
imgillu = self.parser.select(main,'div#recetteLeft img.photo') main = self.parser.select(
self.document.getroot(), 'div#ficheRecette', 1)
imgillu = self.parser.select(main, 'div#recetteLeft img.photo')
if len(imgillu) > 0: if len(imgillu) > 0:
picture_url = unicode(imgillu[0].attrib.get('src','')) picture_url = unicode(imgillu[0].attrib.get('src', ''))
l_spanprep = self.parser.select(main,'span.preptime') l_spanprep = self.parser.select(main, 'span.preptime')
if len(l_spanprep) > 0: if len(l_spanprep) > 0:
preparation_time = int(l_spanprep[0].text.split()[0]) preparation_time = int(l_spanprep[0].text.split()[0])
l_cooktime = self.parser.select(main,'span.cooktime') l_cooktime = self.parser.select(main, 'span.cooktime')
if len(l_cooktime) > 0: if len(l_cooktime) > 0:
cooking_time = int(l_cooktime[0].text.split()[0]) cooking_time = int(l_cooktime[0].text.split()[0])
l_nbpers = self.parser.select(main,'td#recipeQuantity span') l_nbpers = self.parser.select(main, 'td#recipeQuantity span')
if len(l_nbpers) > 0: if len(l_nbpers) > 0:
nb_person = int(l_nbpers[0].text.split()[0]) nb_person = int(l_nbpers[0].text.split()[0])
ingredients = [] ingredients = []
l_ing = self.parser.select(main,'div#ingredients li.ingredient') l_ing = self.parser.select(main, 'div#ingredients li.ingredient')
for ing in l_ing: for ing in l_ing:
ingtxt = unicode(ing.text_content().strip()) ingtxt = unicode(ing.text_content().strip())
if ingtxt != '': if ingtxt != '':
ingredients.append(ingtxt) ingredients.append(ingtxt)
instructions = u'' instructions = u''
l_divinst = self.parser.select(main,'div#preparation span.instructions div') l_divinst = self.parser.select(
main, 'div#preparation span.instructions div')
for inst in l_divinst: for inst in l_divinst:
instructions += '%s: '%inst.text instructions += '%s: ' % inst.text
instructions += '%s\n'%inst.getnext().text instructions += '%s\n' % inst.getnext().text
for divcom in self.parser.select(self.document.getroot(),'div.comment'): for divcom in self.parser.select(self.document.getroot(), 'div.comment'):
author = unicode(self.parser.select(divcom,'div.commentAuthor span',1).text) author = unicode(self.parser.select(
date = unicode(self.parser.select(divcom,'div.commentDate',1).text) divcom, 'div.commentAuthor span', 1).text)
comtxt = unicode(self.parser.select(divcom,'p',1).text_content().strip()) date = unicode(self.parser.select(
comments.append('author: %s, date: %s, text: %s'%(author, date, comtxt)) divcom, 'div.commentDate', 1).text)
comtxt = unicode(self.parser.select(
divcom, 'p', 1).text_content().strip())
comments.append('author: %s, date: %s, text: %s' % (
author, date, comtxt))
recipe = Recipe(id,title) recipe = Recipe(id, title)
recipe.preparation_time = preparation_time recipe.preparation_time = preparation_time
recipe.cooking_time = cooking_time recipe.cooking_time = cooking_time
recipe.nb_person = nb_person recipe.nb_person = nb_person

View file

@ -19,6 +19,7 @@
from weboob.tools.test import BackendTest from weboob.tools.test import BackendTest
class CuisineazTest(BackendTest): class CuisineazTest(BackendTest):
BACKEND = 'cuisineaz' BACKEND = 'cuisineaz'
@ -29,4 +30,3 @@ class CuisineazTest(BackendTest):
assert full_recipe.instructions assert full_recipe.instructions
assert full_recipe.ingredients assert full_recipe.ingredients
assert full_recipe.title assert full_recipe.title