diff --git a/weboob/applications/cookboob/cookboob.py b/weboob/applications/cookboob/cookboob.py
index 7f91616ba72ea1ed526d4acd94b5ccff3b6321be..5d6e90cc70a18b2ff8f3bfb16944b049ac8f2013 100644
--- a/weboob/applications/cookboob/cookboob.py
+++ b/weboob/applications/cookboob/cookboob.py
@@ -25,6 +25,7 @@
from weboob.capabilities.base import empty
from weboob.tools.application.repl import ReplApplication, defaultcount
from weboob.tools.application.formatters.iformatter import IFormatter, PrettyFormatter
+from weboob.tools.capabilities.recipe import recipe_to_krecipes_xml
__all__ = ['Cookboob']
@@ -129,7 +130,7 @@ def do_export(self, line):
recipe = self.get_object(id, 'get_recipe')
if recipe:
- xmlstring = recipe.toKrecipesXml(backend_name or None)
+ xmlstring = recipe_to_krecipes_xml(recipe, backend_name or None)
if dest == '-':
print(xmlstring)
else:
diff --git a/weboob/applications/qcookboob/recipe.py b/weboob/applications/qcookboob/recipe.py
index dcb7569ba4e1d4081f5e3aff879224cbc421b3a0..c6f09d0076146afb15a59b8e853f72c04e49d731 100644
--- a/weboob/applications/qcookboob/recipe.py
+++ b/weboob/applications/qcookboob/recipe.py
@@ -28,6 +28,7 @@
from weboob.applications.qcookboob.ui.recipe_ui import Ui_Recipe
from weboob.capabilities.base import empty
+from weboob.tools.capabilities.recipe import recipe_to_krecipes_xml
class Recipe(QFrame):
@@ -106,7 +107,7 @@ def export(self):
dest = result[0]
if not dest.endswith('.kreml'):
dest += '.kreml'
- data = self.recipe.toKrecipesXml(author=self.backend.name)
+ data = recipe_to_krecipes_xml(self.recipe, author=self.backend.name)
try:
with codecs.open(dest, 'w', 'utf-8') as f:
f.write(data)
diff --git a/weboob/capabilities/recipe.py b/weboob/capabilities/recipe.py
index 5e7ab16514175bf92c729021524409d0d69a73a4..c54e6df32508814c2251f61ae03bd665e2212ea7 100644
--- a/weboob/capabilities/recipe.py
+++ b/weboob/capabilities/recipe.py
@@ -18,16 +18,10 @@
# along with weboob. If not, see .
-from .base import Capability, BaseObject, StringField, IntField, Field, empty
+from .base import Capability, BaseObject, StringField, IntField, Field
-import lxml.etree as ET
-import requests
-import base64
-import re
-
-
-__all__ = ['Recipe', 'CapRecipe']
+__all__ = ['CapRecipe', 'Recipe', 'Comment']
class Comment(BaseObject):
@@ -66,99 +60,6 @@ def __init__(self, id='', title=u'', url=None):
super(Recipe, self).__init__(id, url)
self.title = title
- def toKrecipesXml(self, author=None):
- """
- Export recipe to KRecipes XML string
- """
- sauthor = u''
- if not empty(self.author):
- sauthor += '%s@' % self.author
-
- if author is None:
- sauthor += 'Cookboob'
- else:
- sauthor += author
-
- header = u'\n'
- initial_xml = '''\
-
-
-
-'''
- doc = ET.fromstring(initial_xml)
- recipe = doc.find('krecipes-recipe')
- desc = ET.SubElement(recipe, 'krecipes-description')
- title = ET.SubElement(desc, 'title')
- title.text = self.title
- authors = ET.SubElement(desc, 'author')
- authors.text = sauthor
- eyield = ET.SubElement(desc, 'yield')
- if not empty(self.nb_person):
- amount = ET.SubElement(eyield, 'amount')
- if len(self.nb_person) == 1:
- amount.text = '%s' % self.nb_person[0]
- else:
- mini = ET.SubElement(amount, 'min')
- mini.text = u'%s' % self.nb_person[0]
- maxi = ET.SubElement(amount, 'max')
- maxi.text = u'%s' % self.nb_person[1]
- etype = ET.SubElement(eyield, 'type')
- etype.text = 'persons'
- if not empty(self.preparation_time):
- preptime = ET.SubElement(desc, 'preparation-time')
- preptime.text = '%02d:%02d' % (self.preparation_time / 60, self.preparation_time % 60)
- if not empty(self.picture_url) and self.picture_url != '':
- data = requests.get(self.picture_url).content
- datab64 = base64.encodestring(data)[:-1]
-
- pictures = ET.SubElement(desc, 'pictures')
- pic = ET.SubElement(pictures, 'pic', {'format': 'JPEG', 'id': '1'})
- pic.text = ET.CDATA(datab64)
-
- if not empty(self.ingredients):
- ings = ET.SubElement(recipe, 'krecipes-ingredients')
- pat = re.compile('^[0-9,.]*')
- for i in self.ingredients:
- sname = u'%s' % i
- samount = ''
- sunit = ''
- first_nums = pat.match(i).group()
- if first_nums != '':
- samount = first_nums
- sname = i.lstrip('0123456789 ')
-
- ing = ET.SubElement(ings, 'ingredient')
- am = ET.SubElement(ing, 'amount')
- am.text = samount
- unit = ET.SubElement(ing, 'unit')
- unit.text = sunit
- name = ET.SubElement(ing, 'name')
- name.text = sname
-
- if not empty(self.instructions):
- instructions = ET.SubElement(recipe, 'krecipes-instructions')
- instructions.text = self.instructions
-
- if not empty(self.comments):
- ratings = ET.SubElement(recipe, 'krecipes-ratings')
- for c in self.comments:
- rating = ET.SubElement(ratings, 'rating')
- if c.author:
- rater = ET.SubElement(rating, 'rater')
- rater.text = c.author
- if c.text:
- com = ET.SubElement(rating, 'comment')
- com.text = c.text
- crits = ET.SubElement(rating, 'criterion')
- if c.rate:
- crit = ET.SubElement(crits, 'criteria')
- critname = ET.SubElement(crit, 'name')
- critname.text = 'Overall'
- critstars = ET.SubElement(crit, 'stars')
- critstars.text = c.rate.split('/')[0]
-
- return header + ET.tostring(doc, encoding='UTF-8', pretty_print=True).decode('utf-8')
-
class CapRecipe(Capability):
"""
diff --git a/weboob/tools/capabilities/recipe.py b/weboob/tools/capabilities/recipe.py
new file mode 100644
index 0000000000000000000000000000000000000000..986d486107276e430a7f936d2bbd349a3adcd9fa
--- /dev/null
+++ b/weboob/tools/capabilities/recipe.py
@@ -0,0 +1,124 @@
+# -*- coding: utf-8 -*-
+
+# Copyright(C) 2018 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 unicode_literals
+
+import base64
+import re
+
+import lxml.etree as ET
+import requests
+from weboob.capabilities.base import empty
+
+__all__ = ['recipe_to_krecipes_xml']
+
+
+def recipe_to_krecipes_xml(recipe, author=None):
+ """
+ Export recipe to KRecipes XML string
+ """
+ sauthor = u''
+ if not empty(recipe.author):
+ sauthor += '%s@' % recipe.author
+
+ if author is None:
+ sauthor += 'Cookboob'
+ else:
+ sauthor += author
+
+ header = u'\n'
+ initial_xml = '''\
+
+
+
+'''
+ doc = ET.fromstring(initial_xml)
+ xrecipe = doc.find('krecipes-recipe')
+ desc = ET.SubElement(xrecipe, 'krecipes-description')
+ title = ET.SubElement(desc, 'title')
+ title.text = recipe.title
+ authors = ET.SubElement(desc, 'author')
+ authors.text = sauthor
+ eyield = ET.SubElement(desc, 'yield')
+ if not empty(recipe.nb_person):
+ amount = ET.SubElement(eyield, 'amount')
+ if len(recipe.nb_person) == 1:
+ amount.text = '%s' % recipe.nb_person[0]
+ else:
+ mini = ET.SubElement(amount, 'min')
+ mini.text = u'%s' % recipe.nb_person[0]
+ maxi = ET.SubElement(amount, 'max')
+ maxi.text = u'%s' % recipe.nb_person[1]
+ etype = ET.SubElement(eyield, 'type')
+ etype.text = 'persons'
+ if not empty(recipe.preparation_time):
+ preptime = ET.SubElement(desc, 'preparation-time')
+ preptime.text = '%02d:%02d' % (recipe.preparation_time / 60, recipe.preparation_time % 60)
+ if not empty(recipe.picture_url) and recipe.picture_url != '':
+ data = requests.get(recipe.picture_url).content
+ datab64 = base64.encodestring(data)[:-1]
+
+ pictures = ET.SubElement(desc, 'pictures')
+ pic = ET.SubElement(pictures, 'pic', {'format': 'JPEG', 'id': '1'})
+ pic.text = ET.CDATA(datab64)
+
+ if not empty(recipe.ingredients):
+ ings = ET.SubElement(xrecipe, 'krecipes-ingredients')
+ pat = re.compile('^[0-9,.]*')
+ for i in recipe.ingredients:
+ sname = u'%s' % i
+ samount = ''
+ sunit = ''
+ first_nums = pat.match(i).group()
+ if first_nums != '':
+ samount = first_nums
+ sname = i.lstrip('0123456789 ')
+
+ ing = ET.SubElement(ings, 'ingredient')
+ am = ET.SubElement(ing, 'amount')
+ am.text = samount
+ unit = ET.SubElement(ing, 'unit')
+ unit.text = sunit
+ name = ET.SubElement(ing, 'name')
+ name.text = sname
+
+ if not empty(recipe.instructions):
+ instructions = ET.SubElement(xrecipe, 'krecipes-instructions')
+ instructions.text = recipe.instructions
+
+ if not empty(recipe.comments):
+ ratings = ET.SubElement(xrecipe, 'krecipes-ratings')
+ for c in recipe.comments:
+ rating = ET.SubElement(ratings, 'rating')
+ if c.author:
+ rater = ET.SubElement(rating, 'rater')
+ rater.text = c.author
+ if c.text:
+ com = ET.SubElement(rating, 'comment')
+ com.text = c.text
+ crits = ET.SubElement(rating, 'criterion')
+ if c.rate:
+ crit = ET.SubElement(crits, 'criteria')
+ critname = ET.SubElement(crit, 'name')
+ critname.text = 'Overall'
+ critstars = ET.SubElement(crit, 'stars')
+ critstars.text = c.rate.split('/')[0]
+
+ return header + ET.tostring(doc, encoding='UTF-8', pretty_print=True).decode('utf-8')
+