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.

153 lines
6.2 KiB

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