ruutu.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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|audio)/(?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': [
  72. 'HTTP Error 502: Bad Gateway',
  73. 'Failed to download m3u8 information',
  74. ],
  75. },
  76. {
  77. 'url': 'http://www.supla.fi/audio/2231370',
  78. 'only_matching': True,
  79. },
  80. ]
  81. def _real_extract(self, url):
  82. video_id = self._match_id(url)
  83. video_xml = self._download_xml(
  84. 'https://gatling.nelonenmedia.fi/media-xml-cache', video_id,
  85. query={'id': video_id})
  86. formats = []
  87. processed_urls = []
  88. def extract_formats(node):
  89. for child in node:
  90. if child.tag.endswith('Files'):
  91. extract_formats(child)
  92. elif child.tag.endswith('File'):
  93. video_url = child.text
  94. if (not video_url or video_url in processed_urls
  95. or any(p in video_url for p in ('NOT_USED', 'NOT-USED'))):
  96. continue
  97. processed_urls.append(video_url)
  98. ext = determine_ext(video_url)
  99. if ext == 'm3u8':
  100. formats.extend(self._extract_m3u8_formats(
  101. video_url, video_id, 'mp4', m3u8_id='hls', fatal=False))
  102. elif ext == 'f4m':
  103. formats.extend(self._extract_f4m_formats(
  104. video_url, video_id, f4m_id='hds', fatal=False))
  105. elif ext == 'mpd':
  106. # video-only and audio-only streams are of different
  107. # duration resulting in out of sync issue
  108. continue
  109. formats.extend(self._extract_mpd_formats(
  110. video_url, video_id, mpd_id='dash', fatal=False))
  111. elif ext == 'mp3' or child.tag == 'AudioMediaFile':
  112. formats.append({
  113. 'format_id': 'audio',
  114. 'url': video_url,
  115. 'vcodec': 'none',
  116. })
  117. else:
  118. proto = compat_urllib_parse_urlparse(video_url).scheme
  119. if not child.tag.startswith('HTTP') and proto != 'rtmp':
  120. continue
  121. preference = -1 if proto == 'rtmp' else 1
  122. label = child.get('label')
  123. tbr = int_or_none(child.get('bitrate'))
  124. format_id = '%s-%s' % (proto, label if label else tbr) if label or tbr else proto
  125. if not self._is_valid_url(video_url, video_id, format_id):
  126. continue
  127. width, height = [int_or_none(x) for x in child.get('resolution', 'x').split('x')[:2]]
  128. formats.append({
  129. 'format_id': format_id,
  130. 'url': video_url,
  131. 'width': width,
  132. 'height': height,
  133. 'tbr': tbr,
  134. 'preference': preference,
  135. })
  136. extract_formats(video_xml.find('./Clip'))
  137. drm = xpath_text(video_xml, './Clip/DRM', default=None)
  138. if not formats and drm:
  139. raise ExtractorError('This video is DRM protected.', expected=True)
  140. self._sort_formats(formats)
  141. return {
  142. 'id': video_id,
  143. 'title': xpath_attr(video_xml, './/Behavior/Program', 'program_name', 'title', fatal=True),
  144. 'description': xpath_attr(video_xml, './/Behavior/Program', 'description', 'description'),
  145. 'thumbnail': xpath_attr(video_xml, './/Behavior/Startpicture', 'href', 'thumbnail'),
  146. 'duration': int_or_none(xpath_text(video_xml, './/Runtime', 'duration')),
  147. 'age_limit': int_or_none(xpath_text(video_xml, './/AgeLimit', 'age limit')),
  148. 'formats': formats,
  149. }