itunes.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. from ..util import FeedParserDict
  29. class Namespace(object):
  30. supported_namespaces = {
  31. # Canonical namespace
  32. 'http://www.itunes.com/DTDs/PodCast-1.0.dtd': 'itunes',
  33. # Extra namespace
  34. 'http://example.com/DTDs/PodCast-1.0.dtd': 'itunes',
  35. }
  36. def _start_itunes_author(self, attrs_d):
  37. self._start_author(attrs_d)
  38. def _end_itunes_author(self):
  39. self._end_author()
  40. def _end_itunes_category(self):
  41. self._end_category()
  42. def _start_itunes_name(self, attrs_d):
  43. self._start_name(attrs_d)
  44. def _end_itunes_name(self):
  45. self._end_name()
  46. def _start_itunes_email(self, attrs_d):
  47. self._start_email(attrs_d)
  48. def _end_itunes_email(self):
  49. self._end_email()
  50. def _start_itunes_subtitle(self, attrs_d):
  51. self._start_subtitle(attrs_d)
  52. def _end_itunes_subtitle(self):
  53. self._end_subtitle()
  54. def _start_itunes_summary(self, attrs_d):
  55. self._start_summary(attrs_d)
  56. def _end_itunes_summary(self):
  57. self._end_summary()
  58. def _start_itunes_owner(self, attrs_d):
  59. self.inpublisher = 1
  60. self.push('publisher', 0)
  61. def _end_itunes_owner(self):
  62. self.pop('publisher')
  63. self.inpublisher = 0
  64. self._sync_author_detail('publisher')
  65. def _end_itunes_keywords(self):
  66. for term in self.pop('itunes_keywords').split(','):
  67. if term.strip():
  68. self._add_tag(term.strip(), 'http://www.itunes.com/', None)
  69. def _start_itunes_category(self, attrs_d):
  70. self._add_tag(attrs_d.get('text'), 'http://www.itunes.com/', None)
  71. self.push('category', 1)
  72. def _start_itunes_image(self, attrs_d):
  73. self.push('itunes_image', 0)
  74. if attrs_d.get('href'):
  75. self._get_context()['image'] = FeedParserDict({'href': attrs_d.get('href')})
  76. elif attrs_d.get('url'):
  77. self._get_context()['image'] = FeedParserDict({'href': attrs_d.get('url')})
  78. _start_itunes_link = _start_itunes_image
  79. def _end_itunes_block(self):
  80. value = self.pop('itunes_block', 0)
  81. self._get_context()['itunes_block'] = (value == 'yes' or value == 'Yes') and 1 or 0
  82. def _end_itunes_explicit(self):
  83. value = self.pop('itunes_explicit', 0)
  84. # Convert 'yes' -> True, 'clean' to False, and any other value to None
  85. # False and None both evaluate as False, so the difference can be ignored
  86. # by applications that only need to know if the content is explicit.
  87. self._get_context()['itunes_explicit'] = (None, False, True)[(value == 'yes' and 2) or value == 'clean' or 0]