mediarss.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # Support for the Media RSS 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://search.yahoo.com/mrss/': 'media',
  33. # Old namespace (no trailing slash)
  34. 'http://search.yahoo.com/mrss': 'media',
  35. }
  36. def _start_media_category(self, attrs_d):
  37. attrs_d.setdefault('scheme', 'http://search.yahoo.com/mrss/category_schema')
  38. self._start_category(attrs_d)
  39. def _end_media_category(self):
  40. self._end_category()
  41. def _end_media_keywords(self):
  42. for term in self.pop('media_keywords').split(','):
  43. if term.strip():
  44. self._add_tag(term.strip(), None, None)
  45. def _start_media_title(self, attrs_d):
  46. self._start_title(attrs_d)
  47. def _end_media_title(self):
  48. title_depth = self.title_depth
  49. self._end_title()
  50. self.title_depth = title_depth
  51. def _start_media_group(self, attrs_d):
  52. # don't do anything, but don't break the enclosed tags either
  53. pass
  54. def _start_media_rating(self, attrs_d):
  55. context = self._get_context()
  56. context.setdefault('media_rating', attrs_d)
  57. self.push('rating', 1)
  58. def _end_media_rating(self):
  59. rating = self.pop('rating')
  60. if rating is not None and rating.strip():
  61. context = self._get_context()
  62. context['media_rating']['content'] = rating
  63. def _start_media_credit(self, attrs_d):
  64. context = self._get_context()
  65. context.setdefault('media_credit', [])
  66. context['media_credit'].append(attrs_d)
  67. self.push('credit', 1)
  68. def _end_media_credit(self):
  69. credit = self.pop('credit')
  70. if credit is not None and credit.strip():
  71. context = self._get_context()
  72. context['media_credit'][-1]['content'] = credit
  73. def _start_media_description(self, attrs_d):
  74. self._start_description(attrs_d)
  75. def _end_media_description(self):
  76. self._end_description()
  77. def _start_media_restriction(self, attrs_d):
  78. context = self._get_context()
  79. context.setdefault('media_restriction', attrs_d)
  80. self.push('restriction', 1)
  81. def _end_media_restriction(self):
  82. restriction = self.pop('restriction')
  83. if restriction is not None and restriction.strip():
  84. context = self._get_context()
  85. context['media_restriction']['content'] = [cc.strip().lower() for cc in restriction.split(' ')]
  86. def _start_media_license(self, attrs_d):
  87. context = self._get_context()
  88. context.setdefault('media_license', attrs_d)
  89. self.push('license', 1)
  90. def _end_media_license(self):
  91. license_ = self.pop('license')
  92. if license_ is not None and license_.strip():
  93. context = self._get_context()
  94. context['media_license']['content'] = license_
  95. def _start_media_content(self, attrs_d):
  96. context = self._get_context()
  97. context.setdefault('media_content', [])
  98. context['media_content'].append(attrs_d)
  99. def _start_media_thumbnail(self, attrs_d):
  100. context = self._get_context()
  101. context.setdefault('media_thumbnail', [])
  102. self.push('url', 1) # new
  103. context['media_thumbnail'].append(attrs_d)
  104. def _end_media_thumbnail(self):
  105. url = self.pop('url')
  106. context = self._get_context()
  107. if url is not None and url.strip():
  108. if 'url' not in context['media_thumbnail'][-1]:
  109. context['media_thumbnail'][-1]['url'] = url
  110. def _start_media_player(self, attrs_d):
  111. self.push('media_player', 0)
  112. self._get_context()['media_player'] = FeedParserDict(attrs_d)
  113. def _end_media_player(self):
  114. value = self.pop('media_player')
  115. context = self._get_context()
  116. context['media_player']['content'] = value