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.

194 lines
7.5KB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. int_or_none,
  6. unified_strdate,
  7. xpath_text,
  8. determine_ext,
  9. float_or_none,
  10. ExtractorError,
  11. )
  12. class DreiSatIE(InfoExtractor):
  13. IE_NAME = '3sat'
  14. _GEO_COUNTRIES = ['DE']
  15. _VALID_URL = r'https?://(?:www\.)?3sat\.de/mediathek/(?:(?:index|mediathek)\.php)?\?(?:(?:mode|display)=[^&]+&)*obj=(?P<id>[0-9]+)'
  16. _TESTS = [
  17. {
  18. 'url': 'http://www.3sat.de/mediathek/index.php?mode=play&obj=45918',
  19. 'md5': 'be37228896d30a88f315b638900a026e',
  20. 'info_dict': {
  21. 'id': '45918',
  22. 'ext': 'mp4',
  23. 'title': 'Waidmannsheil',
  24. 'description': 'md5:cce00ca1d70e21425e72c86a98a56817',
  25. 'uploader': 'SCHWEIZWEIT',
  26. 'uploader_id': '100000210',
  27. 'upload_date': '20140913'
  28. },
  29. 'params': {
  30. 'skip_download': True, # m3u8 downloads
  31. }
  32. },
  33. {
  34. 'url': 'http://www.3sat.de/mediathek/mediathek.php?mode=play&obj=51066',
  35. 'only_matching': True,
  36. },
  37. ]
  38. def _parse_smil_formats(self, smil, smil_url, video_id, namespace=None, f4m_params=None, transform_rtmp_url=None):
  39. param_groups = {}
  40. for param_group in smil.findall(self._xpath_ns('./head/paramGroup', namespace)):
  41. group_id = param_group.get(self._xpath_ns(
  42. 'id', 'http://www.w3.org/XML/1998/namespace'))
  43. params = {}
  44. for param in param_group:
  45. params[param.get('name')] = param.get('value')
  46. param_groups[group_id] = params
  47. formats = []
  48. for video in smil.findall(self._xpath_ns('.//video', namespace)):
  49. src = video.get('src')
  50. if not src:
  51. continue
  52. bitrate = int_or_none(self._search_regex(r'_(\d+)k', src, 'bitrate', None)) or float_or_none(video.get('system-bitrate') or video.get('systemBitrate'), 1000)
  53. group_id = video.get('paramGroup')
  54. param_group = param_groups[group_id]
  55. for proto in param_group['protocols'].split(','):
  56. formats.append({
  57. 'url': '%s://%s' % (proto, param_group['host']),
  58. 'app': param_group['app'],
  59. 'play_path': src,
  60. 'ext': 'flv',
  61. 'format_id': '%s-%d' % (proto, bitrate),
  62. 'tbr': bitrate,
  63. })
  64. self._sort_formats(formats)
  65. return formats
  66. def extract_from_xml_url(self, video_id, xml_url):
  67. doc = self._download_xml(
  68. xml_url, video_id,
  69. note='Downloading video info',
  70. errnote='Failed to download video info')
  71. status_code = xpath_text(doc, './status/statuscode')
  72. if status_code and status_code != 'ok':
  73. if status_code == 'notVisibleAnymore':
  74. message = 'Video %s is not available' % video_id
  75. else:
  76. message = '%s returned error: %s' % (self.IE_NAME, status_code)
  77. raise ExtractorError(message, expected=True)
  78. title = xpath_text(doc, './/information/title', 'title', True)
  79. urls = []
  80. formats = []
  81. for fnode in doc.findall('.//formitaeten/formitaet'):
  82. video_url = xpath_text(fnode, 'url')
  83. if not video_url or video_url in urls:
  84. continue
  85. urls.append(video_url)
  86. is_available = 'http://www.metafilegenerator' not in video_url
  87. geoloced = 'static_geoloced_online' in video_url
  88. if not is_available or geoloced:
  89. continue
  90. format_id = fnode.attrib['basetype']
  91. format_m = re.match(r'''(?x)
  92. (?P<vcodec>[^_]+)_(?P<acodec>[^_]+)_(?P<container>[^_]+)_
  93. (?P<proto>[^_]+)_(?P<index>[^_]+)_(?P<indexproto>[^_]+)
  94. ''', format_id)
  95. ext = determine_ext(video_url, None) or format_m.group('container')
  96. if ext == 'meta':
  97. continue
  98. elif ext == 'smil':
  99. formats.extend(self._extract_smil_formats(
  100. video_url, video_id, fatal=False))
  101. elif ext == 'm3u8':
  102. # the certificates are misconfigured (see
  103. # https://github.com/ytdl-org/youtube-dl/issues/8665)
  104. if video_url.startswith('https://'):
  105. continue
  106. formats.extend(self._extract_m3u8_formats(
  107. video_url, video_id, 'mp4', 'm3u8_native',
  108. m3u8_id=format_id, fatal=False))
  109. elif ext == 'f4m':
  110. formats.extend(self._extract_f4m_formats(
  111. video_url, video_id, f4m_id=format_id, fatal=False))
  112. else:
  113. quality = xpath_text(fnode, './quality')
  114. if quality:
  115. format_id += '-' + quality
  116. abr = int_or_none(xpath_text(fnode, './audioBitrate'), 1000)
  117. vbr = int_or_none(xpath_text(fnode, './videoBitrate'), 1000)
  118. tbr = int_or_none(self._search_regex(
  119. r'_(\d+)k', video_url, 'bitrate', None))
  120. if tbr and vbr and not abr:
  121. abr = tbr - vbr
  122. formats.append({
  123. 'format_id': format_id,
  124. 'url': video_url,
  125. 'ext': ext,
  126. 'acodec': format_m.group('acodec'),
  127. 'vcodec': format_m.group('vcodec'),
  128. 'abr': abr,
  129. 'vbr': vbr,
  130. 'tbr': tbr,
  131. 'width': int_or_none(xpath_text(fnode, './width')),
  132. 'height': int_or_none(xpath_text(fnode, './height')),
  133. 'filesize': int_or_none(xpath_text(fnode, './filesize')),
  134. 'protocol': format_m.group('proto').lower(),
  135. })
  136. geolocation = xpath_text(doc, './/details/geolocation')
  137. if not formats and geolocation and geolocation != 'none':
  138. self.raise_geo_restricted(countries=self._GEO_COUNTRIES)
  139. self._sort_formats(formats)
  140. thumbnails = []
  141. for node in doc.findall('.//teaserimages/teaserimage'):
  142. thumbnail_url = node.text
  143. if not thumbnail_url:
  144. continue
  145. thumbnail = {
  146. 'url': thumbnail_url,
  147. }
  148. thumbnail_key = node.get('key')
  149. if thumbnail_key:
  150. m = re.match('^([0-9]+)x([0-9]+)$', thumbnail_key)
  151. if m:
  152. thumbnail['width'] = int(m.group(1))
  153. thumbnail['height'] = int(m.group(2))
  154. thumbnails.append(thumbnail)
  155. upload_date = unified_strdate(xpath_text(doc, './/details/airtime'))
  156. return {
  157. 'id': video_id,
  158. 'title': title,
  159. 'description': xpath_text(doc, './/information/detail'),
  160. 'duration': int_or_none(xpath_text(doc, './/details/lengthSec')),
  161. 'thumbnails': thumbnails,
  162. 'uploader': xpath_text(doc, './/details/originChannelTitle'),
  163. 'uploader_id': xpath_text(doc, './/details/originChannelId'),
  164. 'upload_date': upload_date,
  165. 'formats': formats,
  166. }
  167. def _real_extract(self, url):
  168. video_id = self._match_id(url)
  169. details_url = 'http://www.3sat.de/mediathek/xmlservice/web/beitragsDetails?id=%s' % video_id
  170. return self.extract_from_xml_url(video_id, details_url)