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.

133 lines
5.4 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. def _real_extract(self, url):
  61. video_id = self._match_id(url)
  62. video_xml = self._download_xml(
  63. 'https://gatling.nelonenmedia.fi/media-xml-cache', video_id,
  64. query={'id': video_id})
  65. formats = []
  66. processed_urls = []
  67. def extract_formats(node):
  68. for child in node:
  69. if child.tag.endswith('Files'):
  70. extract_formats(child)
  71. elif child.tag.endswith('File'):
  72. video_url = child.text
  73. if (not video_url or video_url in processed_urls or
  74. any(p in video_url for p in ('NOT_USED', 'NOT-USED'))):
  75. continue
  76. processed_urls.append(video_url)
  77. ext = determine_ext(video_url)
  78. if ext == 'm3u8':
  79. formats.extend(self._extract_m3u8_formats(
  80. video_url, video_id, 'mp4', m3u8_id='hls', fatal=False))
  81. elif ext == 'f4m':
  82. formats.extend(self._extract_f4m_formats(
  83. video_url, video_id, f4m_id='hds', fatal=False))
  84. elif ext == 'mpd':
  85. # video-only and audio-only streams are of different
  86. # duration resulting in out of sync issue
  87. continue
  88. formats.extend(self._extract_mpd_formats(
  89. video_url, video_id, mpd_id='dash', fatal=False))
  90. else:
  91. proto = compat_urllib_parse_urlparse(video_url).scheme
  92. if not child.tag.startswith('HTTP') and proto != 'rtmp':
  93. continue
  94. preference = -1 if proto == 'rtmp' else 1
  95. label = child.get('label')
  96. tbr = int_or_none(child.get('bitrate'))
  97. format_id = '%s-%s' % (proto, label if label else tbr) if label or tbr else proto
  98. if not self._is_valid_url(video_url, video_id, format_id):
  99. continue
  100. width, height = [int_or_none(x) for x in child.get('resolution', 'x').split('x')[:2]]
  101. formats.append({
  102. 'format_id': format_id,
  103. 'url': video_url,
  104. 'width': width,
  105. 'height': height,
  106. 'tbr': tbr,
  107. 'preference': preference,
  108. })
  109. extract_formats(video_xml.find('./Clip'))
  110. drm = xpath_text(video_xml, './Clip/DRM', default=None)
  111. if not formats and drm:
  112. raise ExtractorError('This video is DRM protected.', expected=True)
  113. self._sort_formats(formats)
  114. return {
  115. 'id': video_id,
  116. 'title': xpath_attr(video_xml, './/Behavior/Program', 'program_name', 'title', fatal=True),
  117. 'description': xpath_attr(video_xml, './/Behavior/Program', 'description', 'description'),
  118. 'thumbnail': xpath_attr(video_xml, './/Behavior/Startpicture', 'href', 'thumbnail'),
  119. 'duration': int_or_none(xpath_text(video_xml, './/Runtime', 'duration')),
  120. 'age_limit': int_or_none(xpath_text(video_xml, './/AgeLimit', 'age limit')),
  121. 'formats': formats,
  122. }