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.

275 lines
11 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from .generic import GenericIE
  6. from ..utils import (
  7. determine_ext,
  8. ExtractorError,
  9. qualities,
  10. int_or_none,
  11. parse_duration,
  12. unified_strdate,
  13. xpath_text,
  14. )
  15. from ..compat import compat_etree_fromstring
  16. class ARDMediathekIE(InfoExtractor):
  17. IE_NAME = 'ARD:mediathek'
  18. _VALID_URL = r'^https?://(?:(?:www\.)?ardmediathek\.de|mediathek\.daserste\.de)/(?:.*/)(?P<video_id>[0-9]+|[^0-9][^/\?]+)[^/\?]*(?:\?.*)?'
  19. _TESTS = [{
  20. 'url': 'http://www.ardmediathek.de/tv/Dokumentation-und-Reportage/Ich-liebe-das-Leben-trotzdem/rbb-Fernsehen/Video?documentId=29582122&bcastId=3822114',
  21. 'info_dict': {
  22. 'id': '29582122',
  23. 'ext': 'mp4',
  24. 'title': 'Ich liebe das Leben trotzdem',
  25. 'description': 'md5:45e4c225c72b27993314b31a84a5261c',
  26. 'duration': 4557,
  27. },
  28. 'params': {
  29. # m3u8 download
  30. 'skip_download': True,
  31. },
  32. }, {
  33. 'url': 'http://www.ardmediathek.de/tv/Tatort/Tatort-Scheinwelten-H%C3%B6rfassung-Video/Das-Erste/Video?documentId=29522730&bcastId=602916',
  34. 'md5': 'f4d98b10759ac06c0072bbcd1f0b9e3e',
  35. 'info_dict': {
  36. 'id': '29522730',
  37. 'ext': 'mp4',
  38. 'title': 'Tatort: Scheinwelten - Hörfassung (Video tgl. ab 20 Uhr)',
  39. 'description': 'md5:196392e79876d0ac94c94e8cdb2875f1',
  40. 'duration': 5252,
  41. },
  42. }, {
  43. # audio
  44. 'url': 'http://www.ardmediathek.de/tv/WDR-H%C3%B6rspiel-Speicher/Tod-eines-Fu%C3%9Fballers/WDR-3/Audio-Podcast?documentId=28488308&bcastId=23074086',
  45. 'md5': '219d94d8980b4f538c7fcb0865eb7f2c',
  46. 'info_dict': {
  47. 'id': '28488308',
  48. 'ext': 'mp3',
  49. 'title': 'Tod eines Fußballers',
  50. 'description': 'md5:f6e39f3461f0e1f54bfa48c8875c86ef',
  51. 'duration': 3240,
  52. },
  53. }, {
  54. 'url': 'http://mediathek.daserste.de/sendungen_a-z/328454_anne-will/22429276_vertrauen-ist-gut-spionieren-ist-besser-geht',
  55. 'only_matching': True,
  56. }]
  57. def _extract_media_info(self, media_info_url, webpage, video_id):
  58. media_info = self._download_json(
  59. media_info_url, video_id, 'Downloading media JSON')
  60. formats = self._extract_formats(media_info, video_id)
  61. if not formats:
  62. if '"fsk"' in webpage:
  63. raise ExtractorError(
  64. 'This video is only available after 20:00', expected=True)
  65. elif media_info.get('_geoblocked'):
  66. raise ExtractorError('This video is not available due to geo restriction', expected=True)
  67. self._sort_formats(formats)
  68. duration = int_or_none(media_info.get('_duration'))
  69. thumbnail = media_info.get('_previewImage')
  70. subtitles = {}
  71. subtitle_url = media_info.get('_subtitleUrl')
  72. if subtitle_url:
  73. subtitles['de'] = [{
  74. 'ext': 'ttml',
  75. 'url': subtitle_url,
  76. }]
  77. return {
  78. 'id': video_id,
  79. 'duration': duration,
  80. 'thumbnail': thumbnail,
  81. 'formats': formats,
  82. 'subtitles': subtitles,
  83. }
  84. def _extract_formats(self, media_info, video_id):
  85. type_ = media_info.get('_type')
  86. media_array = media_info.get('_mediaArray', [])
  87. formats = []
  88. for num, media in enumerate(media_array):
  89. for stream in media.get('_mediaStreamArray', []):
  90. stream_urls = stream.get('_stream')
  91. if not stream_urls:
  92. continue
  93. if not isinstance(stream_urls, list):
  94. stream_urls = [stream_urls]
  95. quality = stream.get('_quality')
  96. server = stream.get('_server')
  97. for stream_url in stream_urls:
  98. ext = determine_ext(stream_url)
  99. if quality != 'auto' and ext in ('f4m', 'm3u8'):
  100. continue
  101. if ext == 'f4m':
  102. formats.extend(self._extract_f4m_formats(
  103. stream_url + '?hdcore=3.1.1&plugin=aasp-3.1.1.69.124',
  104. video_id, preference=-1, f4m_id='hds', fatal=False))
  105. elif ext == 'm3u8':
  106. formats.extend(self._extract_m3u8_formats(
  107. stream_url, video_id, 'mp4', preference=1, m3u8_id='hls', fatal=False))
  108. else:
  109. if server and server.startswith('rtmp'):
  110. f = {
  111. 'url': server,
  112. 'play_path': stream_url,
  113. 'format_id': 'a%s-rtmp-%s' % (num, quality),
  114. }
  115. elif stream_url.startswith('http'):
  116. f = {
  117. 'url': stream_url,
  118. 'format_id': 'a%s-%s-%s' % (num, ext, quality)
  119. }
  120. else:
  121. continue
  122. m = re.search(r'_(?P<width>\d+)x(?P<height>\d+)\.mp4$', stream_url)
  123. if m:
  124. f.update({
  125. 'width': int(m.group('width')),
  126. 'height': int(m.group('height')),
  127. })
  128. if type_ == 'audio':
  129. f['vcodec'] = 'none'
  130. formats.append(f)
  131. return formats
  132. def _real_extract(self, url):
  133. # determine video id from url
  134. m = re.match(self._VALID_URL, url)
  135. numid = re.search(r'documentId=([0-9]+)', url)
  136. if numid:
  137. video_id = numid.group(1)
  138. else:
  139. video_id = m.group('video_id')
  140. webpage = self._download_webpage(url, video_id)
  141. if '>Der gewünschte Beitrag ist nicht mehr verfügbar.<' in webpage:
  142. raise ExtractorError('Video %s is no longer available' % video_id, expected=True)
  143. if 'Diese Sendung ist für Jugendliche unter 12 Jahren nicht geeignet. Der Clip ist deshalb nur von 20 bis 6 Uhr verfügbar.' in webpage:
  144. raise ExtractorError('This program is only suitable for those aged 12 and older. Video %s is therefore only available between 20 pm and 6 am.' % video_id, expected=True)
  145. if re.search(r'[\?&]rss($|[=&])', url):
  146. doc = compat_etree_fromstring(webpage.encode('utf-8'))
  147. if doc.tag == 'rss':
  148. return GenericIE()._extract_rss(url, video_id, doc)
  149. title = self._html_search_regex(
  150. [r'<h1(?:\s+class="boxTopHeadline")?>(.*?)</h1>',
  151. r'<meta name="dcterms.title" content="(.*?)"/>',
  152. r'<h4 class="headline">(.*?)</h4>'],
  153. webpage, 'title')
  154. description = self._html_search_meta(
  155. 'dcterms.abstract', webpage, 'description', default=None)
  156. if description is None:
  157. description = self._html_search_meta(
  158. 'description', webpage, 'meta description')
  159. # Thumbnail is sometimes not present.
  160. # It is in the mobile version, but that seems to use a different URL
  161. # structure altogether.
  162. thumbnail = self._og_search_thumbnail(webpage, default=None)
  163. media_streams = re.findall(r'''(?x)
  164. mediaCollection\.addMediaStream\([0-9]+,\s*[0-9]+,\s*"[^"]*",\s*
  165. "([^"]+)"''', webpage)
  166. if media_streams:
  167. QUALITIES = qualities(['lo', 'hi', 'hq'])
  168. formats = []
  169. for furl in set(media_streams):
  170. if furl.endswith('.f4m'):
  171. fid = 'f4m'
  172. else:
  173. fid_m = re.match(r'.*\.([^.]+)\.[^.]+$', furl)
  174. fid = fid_m.group(1) if fid_m else None
  175. formats.append({
  176. 'quality': QUALITIES(fid),
  177. 'format_id': fid,
  178. 'url': furl,
  179. })
  180. self._sort_formats(formats)
  181. info = {
  182. 'formats': formats,
  183. }
  184. else: # request JSON file
  185. info = self._extract_media_info(
  186. 'http://www.ardmediathek.de/play/media/%s' % video_id, webpage, video_id)
  187. info.update({
  188. 'id': video_id,
  189. 'title': title,
  190. 'description': description,
  191. 'thumbnail': thumbnail,
  192. })
  193. return info
  194. class ARDIE(InfoExtractor):
  195. _VALID_URL = '(?P<mainurl>https?://(www\.)?daserste\.de/[^?#]+/videos/(?P<display_id>[^/?#]+)-(?P<id>[0-9]+))\.html'
  196. _TEST = {
  197. 'url': 'http://www.daserste.de/information/reportage-dokumentation/dokus/videos/die-story-im-ersten-mission-unter-falscher-flagge-100.html',
  198. 'md5': 'd216c3a86493f9322545e045ddc3eb35',
  199. 'info_dict': {
  200. 'display_id': 'die-story-im-ersten-mission-unter-falscher-flagge',
  201. 'id': '100',
  202. 'ext': 'mp4',
  203. 'duration': 2600,
  204. 'title': 'Die Story im Ersten: Mission unter falscher Flagge',
  205. 'upload_date': '20140804',
  206. 'thumbnail': 're:^https?://.*\.jpg$',
  207. }
  208. }
  209. def _real_extract(self, url):
  210. mobj = re.match(self._VALID_URL, url)
  211. display_id = mobj.group('display_id')
  212. player_url = mobj.group('mainurl') + '~playerXml.xml'
  213. doc = self._download_xml(player_url, display_id)
  214. video_node = doc.find('./video')
  215. upload_date = unified_strdate(xpath_text(
  216. video_node, './broadcastDate'))
  217. thumbnail = xpath_text(video_node, './/teaserImage//variant/url')
  218. formats = []
  219. for a in video_node.findall('.//asset'):
  220. f = {
  221. 'format_id': a.attrib['type'],
  222. 'width': int_or_none(a.find('./frameWidth').text),
  223. 'height': int_or_none(a.find('./frameHeight').text),
  224. 'vbr': int_or_none(a.find('./bitrateVideo').text),
  225. 'abr': int_or_none(a.find('./bitrateAudio').text),
  226. 'vcodec': a.find('./codecVideo').text,
  227. 'tbr': int_or_none(a.find('./totalBitrate').text),
  228. }
  229. if a.find('./serverPrefix').text:
  230. f['url'] = a.find('./serverPrefix').text
  231. f['playpath'] = a.find('./fileName').text
  232. else:
  233. f['url'] = a.find('./fileName').text
  234. formats.append(f)
  235. self._sort_formats(formats)
  236. return {
  237. 'id': mobj.group('id'),
  238. 'formats': formats,
  239. 'display_id': display_id,
  240. 'title': video_node.find('./title').text,
  241. 'duration': parse_duration(video_node.find('./duration').text),
  242. 'upload_date': upload_date,
  243. 'thumbnail': thumbnail,
  244. }