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.

177 lines
6.1 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import functools
  4. import re
  5. from .common import InfoExtractor
  6. from ..utils import (
  7. int_or_none,
  8. unified_strdate,
  9. OnDemandPagedList,
  10. xpath_text,
  11. )
  12. def extract_from_xml_url(ie, video_id, xml_url):
  13. doc = ie._download_xml(
  14. xml_url, video_id,
  15. note='Downloading video info',
  16. errnote='Failed to download video info')
  17. title = doc.find('.//information/title').text
  18. description = xpath_text(doc, './/information/detail', 'description')
  19. duration = int_or_none(xpath_text(doc, './/details/lengthSec', 'duration'))
  20. uploader = xpath_text(doc, './/details/originChannelTitle', 'uploader')
  21. uploader_id = xpath_text(doc, './/details/originChannelId', 'uploader id')
  22. upload_date = unified_strdate(xpath_text(doc, './/details/airtime', 'upload date'))
  23. def xml_to_format(fnode):
  24. video_url = fnode.find('url').text
  25. is_available = 'http://www.metafilegenerator' not in video_url
  26. format_id = fnode.attrib['basetype']
  27. format_m = re.match(r'''(?x)
  28. (?P<vcodec>[^_]+)_(?P<acodec>[^_]+)_(?P<container>[^_]+)_
  29. (?P<proto>[^_]+)_(?P<index>[^_]+)_(?P<indexproto>[^_]+)
  30. ''', format_id)
  31. ext = format_m.group('container')
  32. proto = format_m.group('proto').lower()
  33. quality = xpath_text(fnode, './quality', 'quality')
  34. abr = int_or_none(xpath_text(fnode, './audioBitrate', 'abr'), 1000)
  35. vbr = int_or_none(xpath_text(fnode, './videoBitrate', 'vbr'), 1000)
  36. width = int_or_none(xpath_text(fnode, './width', 'width'))
  37. height = int_or_none(xpath_text(fnode, './height', 'height'))
  38. filesize = int_or_none(xpath_text(fnode, './filesize', 'filesize'))
  39. format_note = ''
  40. if not format_note:
  41. format_note = None
  42. return {
  43. 'format_id': format_id + '-' + quality,
  44. 'url': video_url,
  45. 'ext': ext,
  46. 'acodec': format_m.group('acodec'),
  47. 'vcodec': format_m.group('vcodec'),
  48. 'abr': abr,
  49. 'vbr': vbr,
  50. 'width': width,
  51. 'height': height,
  52. 'filesize': filesize,
  53. 'format_note': format_note,
  54. 'protocol': proto,
  55. '_available': is_available,
  56. }
  57. def xml_to_thumbnails(fnode):
  58. thumbnails = []
  59. for node in fnode:
  60. thumbnail_url = node.text
  61. if not thumbnail_url:
  62. continue
  63. thumbnail = {
  64. 'url': thumbnail_url,
  65. }
  66. if 'key' in node.attrib:
  67. m = re.match('^([0-9]+)x([0-9]+)$', node.attrib['key'])
  68. if m:
  69. thumbnail['width'] = int(m.group(1))
  70. thumbnail['height'] = int(m.group(2))
  71. thumbnails.append(thumbnail)
  72. return thumbnails
  73. thumbnails = xml_to_thumbnails(doc.findall('.//teaserimages/teaserimage'))
  74. format_nodes = doc.findall('.//formitaeten/formitaet')
  75. formats = list(filter(
  76. lambda f: f['_available'],
  77. map(xml_to_format, format_nodes)))
  78. ie._sort_formats(formats)
  79. return {
  80. 'id': video_id,
  81. 'title': title,
  82. 'description': description,
  83. 'duration': duration,
  84. 'thumbnails': thumbnails,
  85. 'uploader': uploader,
  86. 'uploader_id': uploader_id,
  87. 'upload_date': upload_date,
  88. 'formats': formats,
  89. }
  90. class ZDFIE(InfoExtractor):
  91. _VALID_URL = r'(?:zdf:|zdf:video:|https?://www\.zdf\.de/ZDFmediathek(?:#)?/(.*beitrag/(?:video/)?))(?P<id>[0-9]+)(?:/[^/?]+)?(?:\?.*)?'
  92. _TEST = {
  93. 'url': 'http://www.zdf.de/ZDFmediathek/beitrag/video/2037704/ZDFspezial---Ende-des-Machtpokers--?bc=sts;stt',
  94. 'info_dict': {
  95. 'id': '2037704',
  96. 'ext': 'webm',
  97. 'title': 'ZDFspezial - Ende des Machtpokers',
  98. 'description': 'Union und SPD haben sich auf einen Koalitionsvertrag geeinigt. Aber was bedeutet das für die Bürger? Sehen Sie hierzu das ZDFspezial "Ende des Machtpokers - Große Koalition für Deutschland".',
  99. 'duration': 1022,
  100. 'uploader': 'spezial',
  101. 'uploader_id': '225948',
  102. 'upload_date': '20131127',
  103. },
  104. 'skip': 'Videos on ZDF.de are depublicised in short order',
  105. }
  106. def _real_extract(self, url):
  107. video_id = self._match_id(url)
  108. xml_url = 'http://www.zdf.de/ZDFmediathek/xmlservice/web/beitragsDetails?ak=web&id=%s' % video_id
  109. return extract_from_xml_url(self, video_id, xml_url)
  110. class ZDFChannelIE(InfoExtractor):
  111. _VALID_URL = r'(?:zdf:topic:|https?://www\.zdf\.de/ZDFmediathek(?:#)?/.*kanaluebersicht/)(?P<id>[0-9]+)'
  112. _TEST = {
  113. 'url': 'http://www.zdf.de/ZDFmediathek#/kanaluebersicht/1586442/sendung/Titanic',
  114. 'info_dict': {
  115. 'id': '1586442',
  116. },
  117. 'playlist_count': 3,
  118. }
  119. _PAGE_SIZE = 50
  120. def _fetch_page(self, channel_id, page):
  121. offset = page * self._PAGE_SIZE
  122. xml_url = (
  123. 'http://www.zdf.de/ZDFmediathek/xmlservice/web/aktuellste?ak=web&offset=%d&maxLength=%d&id=%s'
  124. % (offset, self._PAGE_SIZE, channel_id))
  125. doc = self._download_xml(
  126. xml_url, channel_id,
  127. note='Downloading channel info',
  128. errnote='Failed to download channel info')
  129. title = doc.find('.//information/title').text
  130. description = doc.find('.//information/detail').text
  131. for asset in doc.findall('.//teasers/teaser'):
  132. a_type = asset.find('./type').text
  133. a_id = asset.find('./details/assetId').text
  134. if a_type not in ('video', 'topic'):
  135. continue
  136. yield {
  137. '_type': 'url',
  138. 'playlist_title': title,
  139. 'playlist_description': description,
  140. 'url': 'zdf:%s:%s' % (a_type, a_id),
  141. }
  142. def _real_extract(self, url):
  143. channel_id = self._match_id(url)
  144. entries = OnDemandPagedList(
  145. functools.partial(self._fetch_page, channel_id), self._PAGE_SIZE)
  146. return {
  147. '_type': 'playlist',
  148. 'id': channel_id,
  149. 'entries': entries,
  150. }