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.

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