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.

119 lines
4.8 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. int_or_none,
  8. xpath_text,
  9. )
  10. class RuutuIE(InfoExtractor):
  11. _VALID_URL = r'http://(?:www\.)?ruutu\.fi/ohjelmat/(?:[^/?#]+/)*(?P<id>[^/?#]+)'
  12. _TESTS = [
  13. {
  14. 'url': 'http://www.ruutu.fi/ohjelmat/oletko-aina-halunnut-tietaa-mita-tapahtuu-vain-hetki-ennen-lahetysta-nyt-se-selvisi',
  15. 'md5': 'ab2093f39be1ca8581963451b3c0234f',
  16. 'info_dict': {
  17. 'id': '2058907',
  18. 'display_id': 'oletko-aina-halunnut-tietaa-mita-tapahtuu-vain-hetki-ennen-lahetysta-nyt-se-selvisi',
  19. 'ext': 'mp4',
  20. 'title': 'Oletko aina halunnut tietää mitä tapahtuu vain hetki ennen lähetystä? - Nyt se selvisi!',
  21. 'description': 'md5:cfc6ccf0e57a814360df464a91ff67d6',
  22. 'thumbnail': 're:^https?://.*\.jpg$',
  23. 'duration': 114,
  24. 'age_limit': 0,
  25. },
  26. },
  27. {
  28. 'url': 'http://www.ruutu.fi/ohjelmat/superpesis/superpesis-katso-koko-kausi-ruudussa',
  29. 'md5': '065a10ae4d5b8cfd9d0c3d332465e3d9',
  30. 'info_dict': {
  31. 'id': '2057306',
  32. 'display_id': 'superpesis-katso-koko-kausi-ruudussa',
  33. 'ext': 'mp4',
  34. 'title': 'Superpesis: katso koko kausi Ruudussa',
  35. 'description': 'md5:44c44a99fdbe5b380ab74ebd75f0af77',
  36. 'thumbnail': 're:^https?://.*\.jpg$',
  37. 'duration': 40,
  38. 'age_limit': 0,
  39. },
  40. },
  41. ]
  42. def _real_extract(self, url):
  43. display_id = self._match_id(url)
  44. webpage = self._download_webpage(url, display_id)
  45. video_id = self._search_regex(
  46. r'data-media-id="(\d+)"', webpage, 'media id')
  47. video_xml_url = None
  48. media_data = self._search_regex(
  49. r'jQuery\.extend\([^,]+,\s*(.+?)\);', webpage,
  50. 'media data', default=None)
  51. if media_data:
  52. media_json = self._parse_json(media_data, display_id, fatal=False)
  53. if media_json:
  54. xml_url = media_json.get('ruutuplayer', {}).get('xmlUrl')
  55. if xml_url:
  56. video_xml_url = xml_url.replace('{ID}', video_id)
  57. if not video_xml_url:
  58. video_xml_url = 'http://gatling.ruutu.fi/media-xml-cache?id=%s' % video_id
  59. video_xml = self._download_xml(video_xml_url, video_id)
  60. formats = []
  61. processed_urls = []
  62. def extract_formats(node):
  63. for child in node:
  64. if child.tag.endswith('Files'):
  65. extract_formats(child)
  66. elif child.tag.endswith('File'):
  67. video_url = child.text
  68. if not video_url or video_url in processed_urls or 'NOT_USED' in video_url:
  69. return
  70. processed_urls.append(video_url)
  71. ext = determine_ext(video_url)
  72. if ext == 'm3u8':
  73. formats.extend(self._extract_m3u8_formats(
  74. video_url, video_id, 'mp4', m3u8_id='hls'))
  75. elif ext == 'f4m':
  76. formats.extend(self._extract_f4m_formats(
  77. video_url, video_id, f4m_id='hds'))
  78. else:
  79. proto = compat_urllib_parse_urlparse(video_url).scheme
  80. if not child.tag.startswith('HTTP') and proto != 'rtmp':
  81. continue
  82. preference = -1 if proto == 'rtmp' else 1
  83. label = child.get('label')
  84. tbr = int_or_none(child.get('bitrate'))
  85. width, height = [int_or_none(x) for x in child.get('resolution', '').split('x')]
  86. formats.append({
  87. 'format_id': '%s-%s' % (proto, label if label else tbr),
  88. 'url': video_url,
  89. 'width': width,
  90. 'height': height,
  91. 'tbr': tbr,
  92. 'preference': preference,
  93. })
  94. extract_formats(video_xml.find('./Clip'))
  95. self._sort_formats(formats)
  96. return {
  97. 'id': video_id,
  98. 'display_id': display_id,
  99. 'title': self._og_search_title(webpage),
  100. 'description': self._og_search_description(webpage),
  101. 'thumbnail': self._og_search_thumbnail(webpage),
  102. 'duration': int_or_none(xpath_text(video_xml, './/Runtime', 'duration')),
  103. 'age_limit': int_or_none(xpath_text(video_xml, './/AgeLimit', 'age limit')),
  104. 'formats': formats,
  105. }