pages.py 3.05 KB
Newer Older
Julien Veyssier's avatar
Julien Veyssier committed
1 2 3 4
# -*- coding: utf-8 -*-

# Copyright(C) 2013 Julien Veyssier
#
5
# This file is part of a weboob module.
Julien Veyssier's avatar
Julien Veyssier committed
6
#
7
# This weboob module is free software: you can redistribute it and/or modify
Julien Veyssier's avatar
Julien Veyssier committed
8 9 10 11
# 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.
#
12
# This weboob module is distributed in the hope that it will be useful,
Julien Veyssier's avatar
Julien Veyssier committed
13 14 15 16 17
# 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
18
# along with this weboob module. If not, see <http://www.gnu.org/licenses/>.
Julien Veyssier's avatar
Julien Veyssier committed
19 20


21 22
from weboob.browser.pages import HTMLPage, JsonPage, pagination
from weboob.browser.elements import ItemElement, DictElement, method
23 24
from weboob.capabilities.recipe import Recipe, Comment
from weboob.capabilities.base import NotAvailable
25 26
from weboob.capabilities.image import BaseImage, Thumbnail
from weboob.browser.filters.standard import Env, Format, Join, Eval
27
from weboob.browser.filters.json import Dict
28 29


30 31
class HomePage(HTMLPage):
    pass
32 33


34
class ResultsPage(JsonPage):
35 36
    @pagination
    @method
37 38 39
    class iter_recipes(DictElement):

        item_xpath = 'recipes'
40 41

        def next_page(self):
42
            return Dict('links/next/href', default=None)(self.page.doc)
43 44 45 46

        class item(ItemElement):
            klass = Recipe

47 48 49
            obj_id = Dict('recipeID')
            obj_title = Dict('title')
            obj_short_description = Dict('description')
50 51


52
class RecipePage(JsonPage):
53 54 55 56 57
    @method
    class get_recipe(ItemElement):
        klass = Recipe

        obj_id = Env('_id')
58 59 60 61
        obj_title = Dict('title')
        obj_short_description = Dict('description')
        obj_preparation_time = Dict('prepMinutes')
        obj_cooking_time = Dict('cookMinutes')
62 63

        def obj_nb_person(self):
64
            nb_pers = u'%s' % Dict('servings', default='')(self)
65 66 67 68
            return [nb_pers] if nb_pers else NotAvailable

        def obj_ingredients(self):
            ingredients = []
69 70 71 72 73
            for el in Dict('ingredients')(self):
                ing = Format('%s (%s gramm)',
                             Dict('displayValue'),
                             Dict('grams'))(el)
                ingredients.append(ing)
74 75
            return ingredients

76 77 78
        def obj_instructions(self):
            ins = [Dict('displayValue')(el) for el in Dict('directions')(self)]
            return Join('\n * ', ins, addBefore=' * ', addAfter='\n')(self)
79

80 81 82 83 84
        class obj_picture(ItemElement):
            klass = BaseImage

            obj_url = Dict('photo/photoDetailUrl')
            obj_thumbnail = Eval(Thumbnail, obj_url)
85 86

    @method
87 88
    class get_comments(DictElement):
        item_xpath = 'topReviews'
89 90 91 92

        class item(ItemElement):
            klass = Comment

93 94 95 96
            obj_author = Dict('submitter/name')
            obj_rate = Dict('rating')
            obj_text = Dict('text')
            obj_id = Dict('reviewID')