ruutu.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..compat import compat_urllib_parse_urlparse
  5. from ..utils import (
  6. determine_ext,
  7. ExtractorError,
  8. int_or_none,
  9. url_or_none,
  10. xpath_attr,
  11. xpath_text,
  12. )
  13. class RuutuIE(InfoExtractor):
  14. _VALID_URL = r'''(?x)
  15. https?://
  16. (?:
  17. (?:www\.)?(?:ruutu|supla)\.fi/(?:video|supla|audio)/|
  18. static\.nelonenmedia\.fi/player/misc/embed_player\.html\?.*?\bnid=
  19. )
  20. (?P<id>\d+)
  21. '''
  22. _TESTS = [
  23. {
  24. 'url': 'http://www.ruutu.fi/video/2058907',
  25. 'md5': 'ab2093f39be1ca8581963451b3c0234f',
  26. 'info_dict': {
  27. 'id': '2058907',
  28. 'ext': 'mp4',
  29. 'title': 'Oletko aina halunnut tietää mitä tapahtuu vain hetki ennen lähetystä? - Nyt se selvisi!',
  30. 'description': 'md5:cfc6ccf0e57a814360df464a91ff67d6',
  31. 'thumbnail': r're:^https?://.*\.jpg$',
  32. 'duration': 114,
  33. 'age_limit': 0,
  34. },
  35. },
  36. {
  37. 'url': 'http://www.ruutu.fi/video/2057306',
  38. 'md5': '065a10ae4d5b8cfd9d0c3d332465e3d9',
  39. 'info_dict': {
  40. 'id': '2057306',
  41. 'ext': 'mp4',
  42. 'title': 'Superpesis: katso koko kausi Ruudussa',
  43. 'description': 'md5:bfb7336df2a12dc21d18fa696c9f8f23',
  44. 'thumbnail': r're:^https?://.*\.jpg$',
  45. 'duration': 40,
  46. 'age_limit': 0,
  47. },
  48. },
  49. {
  50. 'url': 'http://www.supla.fi/supla/2231370',
  51. 'md5': 'df14e782d49a2c0df03d3be2a54ef949',
  52. 'info_dict': {
  53. 'id': '2231370',
  54. 'ext': 'mp4',
  55. 'title': 'Osa 1: Mikael Jungner',
  56. 'description': 'md5:7d90f358c47542e3072ff65d7b1bcffe',
  57. 'thumbnail': r're:^https?://.*\.jpg$',
  58. 'age_limit': 0,
  59. },
  60. },
  61. # Episode where <SourceFile> is "NOT-USED", but has other
  62. # downloadable sources available.
  63. {
  64. 'url': 'http://www.ruutu.fi/video/3193728',
  65. 'only_matching': True,
  66. },
  67. {
  68. # audio podcast
  69. 'url': 'https://www.supla.fi/supla/3382410',
  70. 'md5': 'b9d7155fed37b2ebf6021d74c4b8e908',
  71. 'info_dict': {
  72. 'id': '3382410',
  73. 'ext': 'mp3',
  74. 'title': 'Mikä ihmeen poltergeist?',
  75. 'description': 'md5:bbb6963df17dfd0ecd9eb9a61bf14b52',
  76. 'thumbnail': r're:^https?://.*\.jpg$',
  77. 'age_limit': 0,
  78. },
  79. 'expected_warnings': [
  80. 'HTTP Error 502: Bad Gateway',
  81. 'Failed to download m3u8 information',
  82. ],
  83. },
  84. {
  85. 'url': 'http://www.supla.fi/audio/2231370',
  86. 'only_matching': True,
  87. },
  88. {
  89. 'url': 'https://static.nelonenmedia.fi/player/misc/embed_player.html?nid=3618790',
  90. 'only_matching': True,
  91. },
  92. ]
  93. _API_BASE = 'https://gatling.nelonenmedia.fi'
  94. def _real_extract(self, url):
  95. video_id = self._match_id(url)
  96. video_xml = self._download_xml(
  97. '%s/media-xml-cache' % self._API_BASE, video_id,
  98. query={'id': video_id})
  99. formats = []
  100. processed_urls = []
  101. def extract_formats(node):
  102. for child in node:
  103. if child.tag.endswith('Files'):
  104. extract_formats(child)
  105. elif child.tag.endswith('File'):
  106. video_url = child.text
  107. if (not video_url or video_url in processed_urls
  108. or any(p in video_url for p in ('NOT_USED', 'NOT-USED'))):
  109. continue
  110. processed_urls.append(video_url)
  111. ext = determine_ext(video_url)
  112. auth_video_url = url_or_none(self._download_webpage(
  113. '%s/auth/access/v2' % self._API_BASE, video_id,
  114. note='Downloading authenticated %s stream URL' % ext,
  115. fatal=False, query={'stream': video_url}))
  116. if auth_video_url:
  117. processed_urls.append(auth_video_url)
  118. video_url = auth_video_url
  119. if ext == 'm3u8':
  120. formats.extend(self._extract_m3u8_formats(
  121. video_url, video_id, 'mp4',
  122. entry_protocol='m3u8_native', m3u8_id='hls',
  123. fatal=False))
  124. elif ext == 'f4m':
  125. formats.extend(self._extract_f4m_formats(
  126. video_url, video_id, f4m_id='hds', fatal=False))
  127. elif ext == 'mpd':
  128. # video-only and audio-only streams are of different
  129. # duration resulting in out of sync issue
  130. continue
  131. formats.extend(self._extract_mpd_formats(
  132. video_url, video_id, mpd_id='dash', fatal=False))
  133. elif ext == 'mp3' or child.tag == 'AudioMediaFile':
  134. formats.append({
  135. 'format_id': 'audio',
  136. 'url': video_url,
  137. 'vcodec': 'none',
  138. })
  139. else:
  140. proto = compat_urllib_parse_urlparse(video_url).scheme
  141. if not child.tag.startswith('HTTP') and proto != 'rtmp':
  142. continue
  143. preference = -1 if proto == 'rtmp' else 1
  144. label = child.get('label')
  145. tbr = int_or_none(child.get('bitrate'))
  146. format_id = '%s-%s' % (proto, label if label else tbr) if label or tbr else proto
  147. if not self._is_valid_url(video_url, video_id, format_id):
  148. continue
  149. width, height = [int_or_none(x) for x in child.get('resolution', 'x').split('x')[:2]]
  150. formats.append({
  151. 'format_id': format_id,
  152. 'url': video_url,
  153. 'width': width,
  154. 'height': height,
  155. 'tbr': tbr,
  156. 'preference': preference,
  157. })
  158. extract_formats(video_xml.find('./Clip'))
  159. drm = xpath_text(video_xml, './Clip/DRM', default=None)
  160. if not formats and drm:
  161. raise ExtractorError('This video is DRM protected.', expected=True)
  162. self._sort_formats(formats)
  163. return {
  164. 'id': video_id,
  165. 'title': xpath_attr(video_xml, './/Behavior/Program', 'program_name', 'title', fatal=True),
  166. 'description': xpath_attr(video_xml, './/Behavior/Program', 'description', 'description'),
  167. 'thumbnail': xpath_attr(video_xml, './/Behavior/Startpicture', 'href', 'thumbnail'),
  168. 'duration': int_or_none(xpath_text(video_xml, './/Runtime', 'duration')),
  169. 'age_limit': int_or_none(xpath_text(video_xml, './/AgeLimit', 'age limit')),
  170. 'formats': formats,
  171. }