You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.9 KiB

  1. import operator
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. parse_xml_doc,
  6. unified_strdate,
  7. )
  8. class ZDFIE(InfoExtractor):
  9. _VALID_URL = r'^http://www\.zdf\.de\/ZDFmediathek(?P<hash>#)?\/(.*beitrag\/video\/)(?P<video_id>[^/\?]+)(?:\?.*)?'
  10. def _real_extract(self, url):
  11. mobj = re.match(self._VALID_URL, url)
  12. video_id = mobj.group('video_id')
  13. xml_url = u'http://www.zdf.de/ZDFmediathek/xmlservice/web/beitragsDetails?ak=web&id=%s' % video_id
  14. info_xml = self._download_webpage(
  15. xml_url, video_id, note=u'Downloading video info')
  16. doc = parse_xml_doc(info_xml)
  17. title = doc.find('.//information/title').text
  18. description = doc.find('.//information/detail').text
  19. uploader_node = doc.find('.//details/originChannelTitle')
  20. uploader = None if uploader_node is None else uploader_node.text
  21. duration_str = doc.find('.//details/length').text
  22. duration_m = re.match(r'''(?x)^
  23. (?P<hours>[0-9]{2})
  24. :(?P<minutes>[0-9]{2})
  25. :(?P<seconds>[0-9]{2})
  26. (?:\.(?P<ms>[0-9]+)?)
  27. ''', duration_str)
  28. duration = (
  29. (
  30. (int(duration_m.group('hours')) * 60 * 60) +
  31. (int(duration_m.group('minutes')) * 60) +
  32. int(duration_m.group('seconds'))
  33. )
  34. if duration_m
  35. else None
  36. )
  37. upload_date = unified_strdate(doc.find('.//details/airtime').text)
  38. def xml_to_format(fnode):
  39. video_url = fnode.find('url').text
  40. is_available = u'http://www.metafilegenerator' not in video_url
  41. format_id = fnode.attrib['basetype']
  42. format_m = re.match(r'''(?x)
  43. (?P<vcodec>[^_]+)_(?P<acodec>[^_]+)_(?P<container>[^_]+)_
  44. (?P<proto>[^_]+)_(?P<index>[^_]+)_(?P<indexproto>[^_]+)
  45. ''', format_id)
  46. ext = format_m.group('container')
  47. is_supported = ext != 'f4f'
  48. PROTO_ORDER = ['http', 'rtmp', 'rtsp']
  49. try:
  50. proto_pref = -PROTO_ORDER.index(format_m.group('proto'))
  51. except ValueError:
  52. proto_pref = 999
  53. quality = fnode.find('./quality').text
  54. QUALITY_ORDER = ['veryhigh', '300', 'high', 'med', 'low']
  55. try:
  56. quality_pref = -QUALITY_ORDER.index(quality)
  57. except ValueError:
  58. quality_pref = 999
  59. abr = int(fnode.find('./audioBitrate').text) // 1000
  60. vbr = int(fnode.find('./videoBitrate').text) // 1000
  61. pref = (is_available, is_supported,
  62. proto_pref, quality_pref, vbr, abr)
  63. format_note = u''
  64. if not is_supported:
  65. format_note += u'(unsupported)'
  66. if not format_note:
  67. format_note = None
  68. return {
  69. 'format_id': format_id + u'-' + quality,
  70. 'url': video_url,
  71. 'ext': ext,
  72. 'acodec': format_m.group('acodec'),
  73. 'vcodec': format_m.group('vcodec'),
  74. 'abr': abr,
  75. 'vbr': vbr,
  76. 'width': int(fnode.find('./width').text),
  77. 'height': int(fnode.find('./height').text),
  78. 'filesize': int(fnode.find('./filesize').text),
  79. 'format_note': format_note,
  80. '_pref': pref,
  81. '_available': is_available,
  82. }
  83. format_nodes = doc.findall('.//formitaeten/formitaet')
  84. formats = sorted(filter(lambda f: f['_available'],
  85. map(xml_to_format, format_nodes)),
  86. key=operator.itemgetter('_pref'))
  87. return {
  88. 'id': video_id,
  89. 'title': title,
  90. 'formats': formats,
  91. 'description': description,
  92. 'uploader': uploader,
  93. 'duration': duration,
  94. 'upload_date': upload_date,
  95. }