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.

254 lines
9.9 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. determine_ext,
  12. qualities,
  13. float_or_none,
  14. ExtractorError,
  15. )
  16. class ZDFIE(InfoExtractor):
  17. _VALID_URL = r'(?:zdf:|zdf:video:|https?://www\.zdf\.de/ZDFmediathek(?:#)?/(.*beitrag/(?:video/)?))(?P<id>[0-9]+)(?:/[^/?]+)?(?:\?.*)?'
  18. _TESTS = [{
  19. 'url': 'http://www.zdf.de/ZDFmediathek/beitrag/video/2037704/ZDFspezial---Ende-des-Machtpokers--?bc=sts;stt',
  20. 'info_dict': {
  21. 'id': '2037704',
  22. 'ext': 'webm',
  23. 'title': 'ZDFspezial - Ende des Machtpokers',
  24. '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".',
  25. 'duration': 1022,
  26. 'uploader': 'spezial',
  27. 'uploader_id': '225948',
  28. 'upload_date': '20131127',
  29. },
  30. 'skip': 'Videos on ZDF.de are depublicised in short order',
  31. }]
  32. def _parse_smil_formats(self, smil, smil_url, video_id, namespace=None, f4m_params=None, transform_rtmp_url=None):
  33. param_groups = {}
  34. for param_group in smil.findall(self._xpath_ns('./head/paramGroup', namespace)):
  35. group_id = param_group.attrib.get(self._xpath_ns('id', 'http://www.w3.org/XML/1998/namespace'))
  36. params = {}
  37. for param in param_group:
  38. params[param.get('name')] = param.get('value')
  39. param_groups[group_id] = params
  40. formats = []
  41. for video in smil.findall(self._xpath_ns('.//video', namespace)):
  42. src = video.get('src')
  43. if not src:
  44. continue
  45. bitrate = float_or_none(video.get('system-bitrate') or video.get('systemBitrate'), 1000)
  46. group_id = video.get('paramGroup')
  47. param_group = param_groups[group_id]
  48. for proto in param_group['protocols'].split(','):
  49. formats.append({
  50. 'url': '%s://%s' % (proto, param_group['host']),
  51. 'app': param_group['app'],
  52. 'play_path': src,
  53. 'ext': 'flv',
  54. 'format_id': '%s-%d' % (proto, bitrate),
  55. 'tbr': bitrate,
  56. })
  57. self._sort_formats(formats)
  58. return formats
  59. def extract_from_xml_url(self, video_id, xml_url):
  60. doc = self._download_xml(
  61. xml_url, video_id,
  62. note='Downloading video info',
  63. errnote='Failed to download video info')
  64. status_code = doc.find('./status/statuscode')
  65. if status_code is not None and status_code.text != 'ok':
  66. code = status_code.text
  67. if code == 'notVisibleAnymore':
  68. message = 'Video %s is not available' % video_id
  69. else:
  70. message = '%s returned error: %s' % (self.IE_NAME, code)
  71. raise ExtractorError(message, expected=True)
  72. title = doc.find('.//information/title').text
  73. description = xpath_text(doc, './/information/detail', 'description')
  74. duration = int_or_none(xpath_text(doc, './/details/lengthSec', 'duration'))
  75. uploader = xpath_text(doc, './/details/originChannelTitle', 'uploader')
  76. uploader_id = xpath_text(doc, './/details/originChannelId', 'uploader id')
  77. upload_date = unified_strdate(xpath_text(doc, './/details/airtime', 'upload date'))
  78. def xml_to_thumbnails(fnode):
  79. thumbnails = []
  80. for node in fnode:
  81. thumbnail_url = node.text
  82. if not thumbnail_url:
  83. continue
  84. thumbnail = {
  85. 'url': thumbnail_url,
  86. }
  87. if 'key' in node.attrib:
  88. m = re.match('^([0-9]+)x([0-9]+)$', node.attrib['key'])
  89. if m:
  90. thumbnail['width'] = int(m.group(1))
  91. thumbnail['height'] = int(m.group(2))
  92. thumbnails.append(thumbnail)
  93. return thumbnails
  94. thumbnails = xml_to_thumbnails(doc.findall('.//teaserimages/teaserimage'))
  95. format_nodes = doc.findall('.//formitaeten/formitaet')
  96. quality = qualities(['veryhigh', 'high', 'med', 'low'])
  97. def get_quality(elem):
  98. return quality(xpath_text(elem, 'quality'))
  99. format_nodes.sort(key=get_quality)
  100. format_ids = []
  101. formats = []
  102. for fnode in format_nodes:
  103. video_url = fnode.find('url').text
  104. is_available = 'http://www.metafilegenerator' not in video_url
  105. if not is_available:
  106. continue
  107. format_id = fnode.attrib['basetype']
  108. quality = xpath_text(fnode, './quality', 'quality')
  109. format_m = re.match(r'''(?x)
  110. (?P<vcodec>[^_]+)_(?P<acodec>[^_]+)_(?P<container>[^_]+)_
  111. (?P<proto>[^_]+)_(?P<index>[^_]+)_(?P<indexproto>[^_]+)
  112. ''', format_id)
  113. ext = determine_ext(video_url, None) or format_m.group('container')
  114. if ext not in ('smil', 'f4m', 'm3u8'):
  115. format_id = format_id + '-' + quality
  116. if format_id in format_ids:
  117. continue
  118. if ext == 'meta':
  119. continue
  120. elif ext == 'smil':
  121. formats.extend(self._extract_smil_formats(
  122. video_url, video_id, fatal=False))
  123. elif ext == 'm3u8':
  124. # the certificates are misconfigured (see
  125. # https://github.com/rg3/youtube-dl/issues/8665)
  126. if video_url.startswith('https://'):
  127. continue
  128. formats.extend(self._extract_m3u8_formats(
  129. video_url, video_id, 'mp4', m3u8_id=format_id, fatal=False))
  130. elif ext == 'f4m':
  131. formats.extend(self._extract_f4m_formats(
  132. video_url, video_id, f4m_id=format_id, fatal=False))
  133. else:
  134. proto = format_m.group('proto').lower()
  135. abr = int_or_none(xpath_text(fnode, './audioBitrate', 'abr'), 1000)
  136. vbr = int_or_none(xpath_text(fnode, './videoBitrate', 'vbr'), 1000)
  137. width = int_or_none(xpath_text(fnode, './width', 'width'))
  138. height = int_or_none(xpath_text(fnode, './height', 'height'))
  139. filesize = int_or_none(xpath_text(fnode, './filesize', 'filesize'))
  140. format_note = ''
  141. if not format_note:
  142. format_note = None
  143. formats.append({
  144. 'format_id': format_id,
  145. 'url': video_url,
  146. 'ext': ext,
  147. 'acodec': format_m.group('acodec'),
  148. 'vcodec': format_m.group('vcodec'),
  149. 'abr': abr,
  150. 'vbr': vbr,
  151. 'width': width,
  152. 'height': height,
  153. 'filesize': filesize,
  154. 'format_note': format_note,
  155. 'protocol': proto,
  156. '_available': is_available,
  157. })
  158. format_ids.append(format_id)
  159. self._sort_formats(formats)
  160. return {
  161. 'id': video_id,
  162. 'title': title,
  163. 'description': description,
  164. 'duration': duration,
  165. 'thumbnails': thumbnails,
  166. 'uploader': uploader,
  167. 'uploader_id': uploader_id,
  168. 'upload_date': upload_date,
  169. 'formats': formats,
  170. }
  171. def _real_extract(self, url):
  172. video_id = self._match_id(url)
  173. xml_url = 'http://www.zdf.de/ZDFmediathek/xmlservice/web/beitragsDetails?ak=web&id=%s' % video_id
  174. return self.extract_from_xml_url(video_id, xml_url)
  175. class ZDFChannelIE(InfoExtractor):
  176. _VALID_URL = r'(?:zdf:topic:|https?://www\.zdf\.de/ZDFmediathek(?:#)?/.*kanaluebersicht/(?:[^/]+/)?)(?P<id>[0-9]+)'
  177. _TESTS = [{
  178. 'url': 'http://www.zdf.de/ZDFmediathek#/kanaluebersicht/1586442/sendung/Titanic',
  179. 'info_dict': {
  180. 'id': '1586442',
  181. },
  182. 'playlist_count': 3,
  183. }, {
  184. 'url': 'http://www.zdf.de/ZDFmediathek/kanaluebersicht/aktuellste/332',
  185. 'only_matching': True,
  186. }, {
  187. 'url': 'http://www.zdf.de/ZDFmediathek/kanaluebersicht/meist-gesehen/332',
  188. 'only_matching': True,
  189. }, {
  190. 'url': 'http://www.zdf.de/ZDFmediathek/kanaluebersicht/_/1798716?bc=nrt;nrm?flash=off',
  191. 'only_matching': True,
  192. }]
  193. _PAGE_SIZE = 50
  194. def _fetch_page(self, channel_id, page):
  195. offset = page * self._PAGE_SIZE
  196. xml_url = (
  197. 'http://www.zdf.de/ZDFmediathek/xmlservice/web/aktuellste?ak=web&offset=%d&maxLength=%d&id=%s'
  198. % (offset, self._PAGE_SIZE, channel_id))
  199. doc = self._download_xml(
  200. xml_url, channel_id,
  201. note='Downloading channel info',
  202. errnote='Failed to download channel info')
  203. title = doc.find('.//information/title').text
  204. description = doc.find('.//information/detail').text
  205. for asset in doc.findall('.//teasers/teaser'):
  206. a_type = asset.find('./type').text
  207. a_id = asset.find('./details/assetId').text
  208. if a_type not in ('video', 'topic'):
  209. continue
  210. yield {
  211. '_type': 'url',
  212. 'playlist_title': title,
  213. 'playlist_description': description,
  214. 'url': 'zdf:%s:%s' % (a_type, a_id),
  215. }
  216. def _real_extract(self, url):
  217. channel_id = self._match_id(url)
  218. entries = OnDemandPagedList(
  219. functools.partial(self._fetch_page, channel_id), self._PAGE_SIZE)
  220. return {
  221. '_type': 'playlist',
  222. 'id': channel_id,
  223. 'entries': entries,
  224. }