psc.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Support for the Podlove Simple Chapters 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. import datetime
  29. import re
  30. from .. import util
  31. class Namespace(object):
  32. supported_namespaces = {
  33. 'http://podlove.org/simple-chapters': 'psc',
  34. }
  35. def __init__(self):
  36. # chapters will only be captured while psc_chapters_flag is True.
  37. self.psc_chapters_flag = False
  38. super(Namespace, self).__init__()
  39. def _start_psc_chapters(self, attrs_d):
  40. context = self._get_context()
  41. if 'psc_chapters' not in context:
  42. self.psc_chapters_flag = True
  43. attrs_d['chapters'] = []
  44. context['psc_chapters'] = util.FeedParserDict(attrs_d)
  45. def _end_psc_chapters(self):
  46. self.psc_chapters_flag = False
  47. def _start_psc_chapter(self, attrs_d):
  48. if self.psc_chapters_flag:
  49. start = self._get_attribute(attrs_d, 'start')
  50. attrs_d['start_parsed'] = _parse_psc_chapter_start(start)
  51. context = self._get_context()['psc_chapters']
  52. context['chapters'].append(util.FeedParserDict(attrs_d))
  53. format_ = re.compile(r'^((\d{2}):)?(\d{2}):(\d{2})(\.(\d{3}))?$')
  54. def _parse_psc_chapter_start(start):
  55. m = format_.match(start)
  56. if m is None:
  57. return None
  58. _, h, m, s, _, ms = m.groups()
  59. h, m, s, ms = (int(h or 0), int(m), int(s), int(ms or 0))
  60. return datetime.timedelta(0, h*60*60 + m*60 + s, ms*1000)