changes to support multiple itunes (sub)categories

This commit is contained in:
xaxa 2016-03-21 11:38:44 -03:00
parent 290045ac49
commit 086d51c555

View file

@ -12,6 +12,7 @@
from lxml import etree from lxml import etree
from feedgen.ext.base import BaseExtension from feedgen.ext.base import BaseExtension
from feedgen.util import ensure_format
class PodcastExtension(BaseExtension): class PodcastExtension(BaseExtension):
@ -54,12 +55,17 @@ class PodcastExtension(BaseExtension):
block = etree.SubElement(channel, '{%s}block' % ITUNES_NS) block = etree.SubElement(channel, '{%s}block' % ITUNES_NS)
block.text = 'yes' if self.__itunes_block else 'no' block.text = 'yes' if self.__itunes_block else 'no'
if self.__itunes_category: for c in self.__itunes_category or []:
category = etree.SubElement(channel, '{%s}category' % ITUNES_NS) if not c.get('cat'):
category.attrib['text'] = self.__itunes_category['cat'] continue
if self.__itunes_category.get('sub'): category = channel.find('{%s}category[@text="%s"]' % (ITUNES_NS,c.get('cat')))
if category == None:
category = etree.SubElement(channel, '{%s}category' % ITUNES_NS)
category.attrib['text'] = c.get('cat')
if c.get('sub'):
subcategory = etree.SubElement(category, '{%s}category' % ITUNES_NS) subcategory = etree.SubElement(category, '{%s}category' % ITUNES_NS)
subcategory.attrib['text'] = self.__itunes_category['sub'] subcategory.attrib['text'] = c.get('sub')
if self.__itunes_image: if self.__itunes_image:
image = etree.SubElement(channel, '{%s}image' % ITUNES_NS) image = etree.SubElement(channel, '{%s}image' % ITUNES_NS)
@ -120,27 +126,43 @@ class PodcastExtension(BaseExtension):
self.__itunes_block = itunes_block self.__itunes_block = itunes_block
return self.__itunes_block return self.__itunes_block
def itunes_category(self, itunes_category=None, replace=False, **kwargs):
def itunes_category(self, itunes_category=None, itunes_subcategory=None):
'''Get or set the ITunes category which appears in the category column '''Get or set the ITunes category which appears in the category column
and in iTunes Store Browser. and in iTunes Store Browser.
The (sub-)category has to be one from the values defined at The (sub-)category has to be one from the values defined at
http://www.apple.com/itunes/podcasts/specs.html#categories http://www.apple.com/itunes/podcasts/specs.html#categories
:param itunes_category: Category of the podcast. This method can be called with:
:param itunes_subcategory: Subcategory of the podcast. - the fields of an itunes_category as keyword arguments
:returns: Category data of the podcast. - the fields of an itunes_category as a dictionary
- a list of dictionaries containing the itunes_category fields
An itunes_category has the following fields:
- *cat* name for a category.
- *sub* name for a subcategory, child of category
If a podcast has more than one subcategory from the same category, the
category is called more than once.
Like: [{"cat":"Arts","sub":"Design"},{"cat":"Arts","sub":"Food"}]
The code will be:
<itunes:category text="Arts">
<itunes:category text="Design"/>
<itunes:category text="Food"/>
</itunes:category>
:param itunes_category: Dictionary or list of dictionaries with itunes_category data.
:param replace: Add or replace old data.
:returns: List of itunes_categories as dictionaries.
''' '''
if itunes_category is None and kwargs:
itunes_category = kwargs
if not itunes_category is None: if not itunes_category is None:
if not itunes_category in self._itunes_categories.keys(): if replace or self.__itunes_category is None:
raise ValueError('Invalid category') self.__itunes_category = []
cat = {'cat':itunes_category} self.__itunes_category += ensure_format( itunes_category,
if not itunes_subcategory is None: set(['cat', 'sub']), set(['cat']))
if not itunes_subcategory in self._itunes_categories[itunes_category]:
raise ValueError('Invalid subcategory')
cat['sub'] = itunes_subcategory
self.__itunes_category = cat
return self.__itunes_category return self.__itunes_category