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.

262 lines
10 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. subtitles = {}
  79. captions_url = doc.find('.//caption/url')
  80. if captions_url is not None:
  81. subtitles['de'] = [{
  82. 'url': captions_url.text,
  83. 'ext': 'ttml',
  84. }]
  85. def xml_to_thumbnails(fnode):
  86. thumbnails = []
  87. for node in fnode:
  88. thumbnail_url = node.text
  89. if not thumbnail_url:
  90. continue
  91. thumbnail = {
  92. 'url': thumbnail_url,
  93. }
  94. if 'key' in node.attrib:
  95. m = re.match('^([0-9]+)x([0-9]+)$', node.attrib['key'])
  96. if m:
  97. thumbnail['width'] = int(m.group(1))
  98. thumbnail['height'] = int(m.group(2))
  99. thumbnails.append(thumbnail)
  100. return thumbnails
  101. thumbnails = xml_to_thumbnails(doc.findall('.//teaserimages/teaserimage'))
  102. format_nodes = doc.findall('.//formitaeten/formitaet')
  103. quality = qualities(['veryhigh', 'high', 'med', 'low'])
  104. def get_quality(elem):
  105. return quality(xpath_text(elem, 'quality'))
  106. format_nodes.sort(key=get_quality)
  107. format_ids = []
  108. formats = []
  109. for fnode in format_nodes:
  110. video_url = fnode.find('url').text
  111. is_available = 'http://www.metafilegenerator' not in video_url
  112. if not is_available:
  113. continue
  114. format_id = fnode.attrib['basetype']
  115. quality = xpath_text(fnode, './quality', 'quality')
  116. format_m = re.match(r'''(?x)
  117. (?P<vcodec>[^_]+)_(?P<acodec>[^_]+)_(?P<container>[^_]+)_
  118. (?P<proto>[^_]+)_(?P<index>[^_]+)_(?P<indexproto>[^_]+)
  119. ''', format_id)
  120. ext = determine_ext(video_url, None) or format_m.group('container')
  121. if ext not in ('smil', 'f4m', 'm3u8'):
  122. format_id = format_id + '-' + quality
  123. if format_id in format_ids:
  124. continue
  125. if ext == 'meta':
  126. continue
  127. elif ext == 'smil':
  128. formats.extend(self._extract_smil_formats(
  129. video_url, video_id, fatal=False))
  130. elif ext == 'm3u8':
  131. # the certificates are misconfigured (see
  132. # https://github.com/rg3/youtube-dl/issues/8665)
  133. if video_url.startswith('https://'):
  134. continue
  135. formats.extend(self._extract_m3u8_formats(
  136. video_url, video_id, 'mp4', m3u8_id=format_id, fatal=False))
  137. elif ext == 'f4m':
  138. formats.extend(self._extract_f4m_formats(
  139. video_url, video_id, f4m_id=format_id, fatal=False))
  140. else:
  141. proto = format_m.group('proto').lower()
  142. abr = int_or_none(xpath_text(fnode, './audioBitrate', 'abr'), 1000)
  143. vbr = int_or_none(xpath_text(fnode, './videoBitrate', 'vbr'), 1000)
  144. width = int_or_none(xpath_text(fnode, './width', 'width'))
  145. height = int_or_none(xpath_text(fnode, './height', 'height'))
  146. filesize = int_or_none(xpath_text(fnode, './filesize', 'filesize'))
  147. format_note = ''
  148. if not format_note:
  149. format_note = None
  150. formats.append({
  151. 'format_id': format_id,
  152. 'url': video_url,
  153. 'ext': ext,
  154. 'acodec': format_m.group('acodec'),
  155. 'vcodec': format_m.group('vcodec'),
  156. 'abr': abr,
  157. 'vbr': vbr,
  158. 'width': width,
  159. 'height': height,
  160. 'filesize': filesize,
  161. 'format_note': format_note,
  162. 'protocol': proto,
  163. '_available': is_available,
  164. })
  165. format_ids.append(format_id)
  166. self._sort_formats(formats)
  167. return {
  168. 'id': video_id,
  169. 'title': title,
  170. 'description': description,
  171. 'duration': duration,
  172. 'thumbnails': thumbnails,
  173. 'uploader': uploader,
  174. 'uploader_id': uploader_id,
  175. 'upload_date': upload_date,
  176. 'formats': formats,
  177. 'subtitles': subtitles,
  178. }
  179. def _real_extract(self, url):
  180. video_id = self._match_id(url)
  181. xml_url = 'http://www.zdf.de/ZDFmediathek/xmlservice/web/beitragsDetails?ak=web&id=%s' % video_id
  182. return self.extract_from_xml_url(video_id, xml_url)
  183. class ZDFChannelIE(InfoExtractor):
  184. _VALID_URL = r'(?:zdf:topic:|https?://www\.zdf\.de/ZDFmediathek(?:#)?/.*kanaluebersicht/(?:[^/]+/)?)(?P<id>[0-9]+)'
  185. _TESTS = [{
  186. 'url': 'http://www.zdf.de/ZDFmediathek#/kanaluebersicht/1586442/sendung/Titanic',
  187. 'info_dict': {
  188. 'id': '1586442',
  189. },
  190. 'playlist_count': 3,
  191. }, {
  192. 'url': 'http://www.zdf.de/ZDFmediathek/kanaluebersicht/aktuellste/332',
  193. 'only_matching': True,
  194. }, {
  195. 'url': 'http://www.zdf.de/ZDFmediathek/kanaluebersicht/meist-gesehen/332',
  196. 'only_matching': True,
  197. }, {
  198. 'url': 'http://www.zdf.de/ZDFmediathek/kanaluebersicht/_/1798716?bc=nrt;nrm?flash=off',
  199. 'only_matching': True,
  200. }]
  201. _PAGE_SIZE = 50
  202. def _fetch_page(self, channel_id, page):
  203. offset = page * self._PAGE_SIZE
  204. xml_url = (
  205. 'http://www.zdf.de/ZDFmediathek/xmlservice/web/aktuellste?ak=web&offset=%d&maxLength=%d&id=%s'
  206. % (offset, self._PAGE_SIZE, channel_id))
  207. doc = self._download_xml(
  208. xml_url, channel_id,
  209. note='Downloading channel info',
  210. errnote='Failed to download channel info')
  211. title = doc.find('.//information/title').text
  212. description = doc.find('.//information/detail').text
  213. for asset in doc.findall('.//teasers/teaser'):
  214. a_type = asset.find('./type').text
  215. a_id = asset.find('./details/assetId').text
  216. if a_type not in ('video', 'topic'):
  217. continue
  218. yield {
  219. '_type': 'url',
  220. 'playlist_title': title,
  221. 'playlist_description': description,
  222. 'url': 'zdf:%s:%s' % (a_type, a_id),
  223. }
  224. def _real_extract(self, url):
  225. channel_id = self._match_id(url)
  226. entries = OnDemandPagedList(
  227. functools.partial(self._fetch_page, channel_id), self._PAGE_SIZE)
  228. return {
  229. '_type': 'playlist',
  230. 'id': channel_id,
  231. 'entries': entries,
  232. }