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.

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