From 07a357a94f069f282056eef9f0ead919ae457c78 Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Thu, 14 Mar 2013 17:44:54 +0100 Subject: [PATCH] new cap recipe and app cookboob --- AUTHORS | 2 +- weboob/applications/cookboob/__init__.py | 22 +++++ weboob/applications/cookboob/cookboob.py | 111 +++++++++++++++++++++++ weboob/capabilities/recipe.py | 66 ++++++++++++++ 4 files changed, 200 insertions(+), 1 deletion(-) create mode 100644 weboob/applications/cookboob/__init__.py create mode 100644 weboob/applications/cookboob/cookboob.py create mode 100644 weboob/capabilities/recipe.py diff --git a/AUTHORS b/AUTHORS index 243929ab..a998dfdb 100644 --- a/AUTHORS +++ b/AUTHORS @@ -28,7 +28,7 @@ Julien Veyssier * Suboob, Cineoob, QCineoob and Booblyrics developer * CreditMutuel, Geolocip, Ipinfodb, IsoHunt, Kickass, Piratebay, Attilasub, Opensubtitles, Tvsubtitle, Btmon, Imdb, - Seeklyrics and Parolesmusique modules maintainer. + Seeklyrics, Parolesmusique and Parolesmania modules maintainer. Christophe Benz * Bouygues, INA, SFR and Youtube modules maintainer. diff --git a/weboob/applications/cookboob/__init__.py b/weboob/applications/cookboob/__init__.py new file mode 100644 index 00000000..2412ecef --- /dev/null +++ b/weboob/applications/cookboob/__init__.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2013 Julien Veyssier +# +# This file is part of weboob. +# +# weboob is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# weboob is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with weboob. If not, see . + +from .cookboob import Cookboob + +__all__ = ['Cookboob'] diff --git a/weboob/applications/cookboob/cookboob.py b/weboob/applications/cookboob/cookboob.py new file mode 100644 index 00000000..1d986d02 --- /dev/null +++ b/weboob/applications/cookboob/cookboob.py @@ -0,0 +1,111 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2013 Julien Veyssier +# +# This file is part of weboob. +# +# weboob is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# weboob is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with weboob. If not, see . + +from __future__ import with_statement + +import sys + +from weboob.capabilities.recipe import ICapRecipe +from weboob.tools.application.repl import ReplApplication +from weboob.tools.application.formatters.iformatter import IFormatter, PrettyFormatter +from weboob.core import CallErrors + + +__all__ = ['Cookboob'] + + +class RecipeInfoFormatter(IFormatter): + MANDATORY_FIELDS = ('id', 'title', 'preparation_time', 'cooking_time', 'ingredients', 'instructions', 'nb_person') + + def format_obj(self, obj, alias): + result = u'%s%s%s\n' % (self.BOLD, obj.title, self.NC) + result += 'ID: %s\n' % obj.fullid + result += 'Preparation time: %s\n' % obj.preparation_time + result += 'Cooking time: %s\n' % obj.cooking_time + result += 'Amount of people: %s\n' % obj.nb_person + result += '\n%Ingredients%s\n' % (self.BOLD, self.NC) + for i in obj.ingredients: + result += ' * %s'%i + result += '\n\n%Instructions%s\n' % (self.BOLD, self.NC) + for i in obj.instructions: + result += ' * %s'%i + return result + + +class RecipeListFormatter(PrettyFormatter): + MANDATORY_FIELDS = ('id', 'title', 'short_description', 'preparation_time') + + def get_title(self, obj): + return obj.title + + def get_description(self, obj): + result = u'' + if obj.short_description != NotAvailable: + result += 'description: %s '%obj.short_description + if obj.preparation_time != NotAvailable: + result += 'prep time: %smin'%obj.preparation_time + return result + + +class Cookboob(ReplApplication): + APPNAME = 'cookboob' + VERSION = '0.f' + COPYRIGHT = 'Copyright(C) 2013 Julien Veyssier' + DESCRIPTION = "Console application allowing to search for recipes on various websites." + SHORT_DESCRIPTION = "search and consult recipes" + CAPS = ICapRecipe + EXTRA_FORMATTERS = {'recipe_list': RecipeListFormatter, + 'recipe_info': RecipeInfoFormatter + } + COMMANDS_FORMATTERS = {'search': 'recipe_list', + 'info': 'recipe_info' + } + + def complete_info(self, text, line, *ignored): + args = line.split(' ') + if len(args) == 2: + return self._complete_object() + + def do_info(self, id): + """ + info ID + + Get information about a recipe. + """ + + recipe = self.get_object(id, 'get_recipe') + if not recipee: + print >>sys.stderr, 'Recipe not found: %s' % id + return 3 + + self.start_format() + self.format(recipe) + self.flush() + + def do_search(self, pattern): + """ + search [PATTERN] + + Search recipes. + """ + self.change_path([u'search']) + self.start_format(pattern=pattern) + for backend, recipe in self.do('iter_recipes', pattern=pattern): + self.cached_format(recipe) + self.flush() diff --git a/weboob/capabilities/recipe.py b/weboob/capabilities/recipe.py new file mode 100644 index 00000000..3d649c8f --- /dev/null +++ b/weboob/capabilities/recipe.py @@ -0,0 +1,66 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2013 Julien Veyssier +# +# This file is part of weboob. +# +# weboob is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# weboob is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with weboob. If not, see . + + +from .base import IBaseCap, CapBaseObject, StringField, IntField, UserError, Field + + +__all__ = ['Recipe', 'ICapRecipe'] + + +class Recipe(CapBaseObject): + """ + Recipe object. + """ + title = StringField('Title of the recipe') + thumbnail_url = StringField('Direct url to recipe thumbnail') + nb_person = IntField('The recipe was made for this amount of persons') + preparation_time = IntField('Preparation time of the recipe in minutes') + cooking_time = IntField('Cooking time of the recipe in minutes') + ingredients = Field('Ingredient list necessary for the recipe',list) + instructions = Field('Instruction step list of the recipe',list) + + def __init__(self, id, title): + CapBaseObject.__init__(self, id) + self.title = title + + +class ICapRecipe(IBaseCap): + """ + Recipe providers. + """ + def iter_recipes(self, pattern): + """ + Search recipes and iterate on results. + + :param pattern: pattern to search + :type pattern: str + :rtype: iter[:class:`Recipe`] + """ + raise NotImplementedError() + + def get_recipe(self, _id): + """ + Get a recipe object from an ID. + + :param _id: ID of recipe + :type _id: str + :rtype: :class:`Recipe` + """ + raise NotImplementedError()