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.

250 lines
9.7 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. formats.extend(self._extract_m3u8_formats(
  125. video_url, video_id, 'mp4', m3u8_id=format_id, fatal=False))
  126. elif ext == 'f4m':
  127. formats.extend(self._extract_f4m_formats(
  128. video_url, video_id, f4m_id=format_id, fatal=False))
  129. else:
  130. proto = format_m.group('proto').lower()
  131. abr = int_or_none(xpath_text(fnode, './audioBitrate', 'abr'), 1000)
  132. vbr = int_or_none(xpath_text(fnode, './videoBitrate', 'vbr'), 1000)
  133. width = int_or_none(xpath_text(fnode, './width', 'width'))
  134. height = int_or_none(xpath_text(fnode, './height', 'height'))
  135. filesize = int_or_none(xpath_text(fnode, './filesize', 'filesize'))
  136. format_note = ''
  137. if not format_note:
  138. format_note = None
  139. formats.append({
  140. 'format_id': format_id,
  141. 'url': video_url,
  142. 'ext': ext,
  143. 'acodec': format_m.group('acodec'),
  144. 'vcodec': format_m.group('vcodec'),
  145. 'abr': abr,
  146. 'vbr': vbr,
  147. 'width': width,
  148. 'height': height,
  149. 'filesize': filesize,
  150. 'format_note': format_note,
  151. 'protocol': proto,
  152. '_available': is_available,
  153. })
  154. format_ids.append(format_id)
  155. self._sort_formats(formats)
  156. return {
  157. 'id': video_id,
  158. 'title': title,
  159. 'description': description,
  160. 'duration': duration,
  161. 'thumbnails': thumbnails,
  162. 'uploader': uploader,
  163. 'uploader_id': uploader_id,
  164. 'upload_date': upload_date,
  165. 'formats': formats,
  166. }
  167. def _real_extract(self, url):
  168. video_id = self._match_id(url)
  169. xml_url = 'http://www.zdf.de/ZDFmediathek/xmlservice/web/beitragsDetails?ak=web&id=%s' % video_id
  170. return self.extract_from_xml_url(video_id, xml_url)
  171. class ZDFChannelIE(InfoExtractor):
  172. _VALID_URL = r'(?:zdf:topic:|https?://www\.zdf\.de/ZDFmediathek(?:#)?/.*kanaluebersicht/(?:[^/]+/)?)(?P<id>[0-9]+)'
  173. _TESTS = [{
  174. 'url': 'http://www.zdf.de/ZDFmediathek#/kanaluebersicht/1586442/sendung/Titanic',
  175. 'info_dict': {
  176. 'id': '1586442',
  177. },
  178. 'playlist_count': 3,
  179. }, {
  180. 'url': 'http://www.zdf.de/ZDFmediathek/kanaluebersicht/aktuellste/332',
  181. 'only_matching': True,
  182. }, {
  183. 'url': 'http://www.zdf.de/ZDFmediathek/kanaluebersicht/meist-gesehen/332',
  184. 'only_matching': True,
  185. }, {
  186. 'url': 'http://www.zdf.de/ZDFmediathek/kanaluebersicht/_/1798716?bc=nrt;nrm?flash=off',
  187. 'only_matching': True,
  188. }]
  189. _PAGE_SIZE = 50
  190. def _fetch_page(self, channel_id, page):
  191. offset = page * self._PAGE_SIZE
  192. xml_url = (
  193. 'http://www.zdf.de/ZDFmediathek/xmlservice/web/aktuellste?ak=web&offset=%d&maxLength=%d&id=%s'
  194. % (offset, self._PAGE_SIZE, channel_id))
  195. doc = self._download_xml(
  196. xml_url, channel_id,
  197. note='Downloading channel info',
  198. errnote='Failed to download channel info')
  199. title = doc.find('.//information/title').text
  200. description = doc.find('.//information/detail').text
  201. for asset in doc.findall('.//teasers/teaser'):
  202. a_type = asset.find('./type').text
  203. a_id = asset.find('./details/assetId').text
  204. if a_type not in ('video', 'topic'):
  205. continue
  206. yield {
  207. '_type': 'url',
  208. 'playlist_title': title,
  209. 'playlist_description': description,
  210. 'url': 'zdf:%s:%s' % (a_type, a_id),
  211. }
  212. def _real_extract(self, url):
  213. channel_id = self._match_id(url)
  214. entries = OnDemandPagedList(
  215. functools.partial(self._fetch_page, channel_id), self._PAGE_SIZE)
  216. return {
  217. '_type': 'playlist',
  218. 'id': channel_id,
  219. 'entries': entries,
  220. }