Format: text/x-python
⬇ Download File (4.0 KB)
   1  # Support for the iTunes format
   2  # Copyright 2010-2023 Kurt McKee <contactme@kurtmckee.org>
   3  # Copyright 2002-2008 Mark Pilgrim
   4  # All rights reserved.
   5  #
   6  # This file is a part of feedparser.
   7  #
   8  # Redistribution and use in source and binary forms, with or without
   9  # modification, are permitted provided that the following conditions are met:
  10  #
  11  # * Redistributions of source code must retain the above copyright notice,
  12  #   this list of conditions and the following disclaimer.
  13  # * Redistributions in binary form must reproduce the above copyright notice,
  14  #   this list of conditions and the following disclaimer in the documentation
  15  #   and/or other materials provided with the distribution.
  16  #
  17  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS'
  18  # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19  # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20  # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21  # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22  # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23  # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24  # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25  # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26  # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27  # POSSIBILITY OF SUCH DAMAGE.
  28  
  29  from ..util import FeedParserDict
  30  
  31  
  32  class Namespace(object):
  33      supported_namespaces = {
  34          # Canonical namespace
  35          'http://www.itunes.com/DTDs/PodCast-1.0.dtd': 'itunes',
  36  
  37          # Extra namespace
  38          'http://example.com/DTDs/PodCast-1.0.dtd': 'itunes',
  39      }
  40  
  41      def _start_itunes_author(self, attrs_d):
  42          self._start_author(attrs_d)
  43  
  44      def _end_itunes_author(self):
  45          self._end_author()
  46  
  47      def _end_itunes_category(self):
  48          self._end_category()
  49  
  50      def _start_itunes_name(self, attrs_d):
  51          self._start_name(attrs_d)
  52  
  53      def _end_itunes_name(self):
  54          self._end_name()
  55  
  56      def _start_itunes_email(self, attrs_d):
  57          self._start_email(attrs_d)
  58  
  59      def _end_itunes_email(self):
  60          self._end_email()
  61  
  62      def _start_itunes_subtitle(self, attrs_d):
  63          self._start_subtitle(attrs_d)
  64  
  65      def _end_itunes_subtitle(self):
  66          self._end_subtitle()
  67  
  68      def _start_itunes_summary(self, attrs_d):
  69          self._start_summary(attrs_d)
  70  
  71      def _end_itunes_summary(self):
  72          self._end_summary()
  73  
  74      def _start_itunes_owner(self, attrs_d):
  75          self.inpublisher = 1
  76          self.push('publisher', 0)
  77  
  78      def _end_itunes_owner(self):
  79          self.pop('publisher')
  80          self.inpublisher = 0
  81          self._sync_author_detail('publisher')
  82  
  83      def _end_itunes_keywords(self):
  84          for term in self.pop('itunes_keywords').split(','):
  85              if term.strip():
  86                  self._add_tag(term.strip(), 'http://www.itunes.com/', None)
  87  
  88      def _start_itunes_category(self, attrs_d):
  89          self._add_tag(attrs_d.get('text'), 'http://www.itunes.com/', None)
  90          self.push('category', 1)
  91  
  92      def _start_itunes_image(self, attrs_d):
  93          self.push('itunes_image', 0)
  94          if attrs_d.get('href'):
  95              self._get_context()['image'] = FeedParserDict({'href': attrs_d.get('href')})
  96          elif attrs_d.get('url'):
  97              self._get_context()['image'] = FeedParserDict({'href': attrs_d.get('url')})
  98      _start_itunes_link = _start_itunes_image
  99  
 100      def _end_itunes_block(self):
 101          value = self.pop('itunes_block', 0)
 102          self._get_context()['itunes_block'] = (value == 'yes' or value == 'Yes') and 1 or 0
 103  
 104      def _end_itunes_explicit(self):
 105          value = self.pop('itunes_explicit', 0)
 106          # Convert 'yes' -> True, 'clean' to False, and any other value to None
 107          # False and None both evaluate as False, so the difference can be ignored
 108          # by applications that only need to know if the content is explicit.
 109          self._get_context()['itunes_explicit'] = (None, False, True)[(value == 'yes' and 2) or value == 'clean' or 0]
 110