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.

102 lines
4.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. int_or_none,
  8. xpath_attr,
  9. xpath_text,
  10. )
  11. class RuutuIE(InfoExtractor):
  12. _VALID_URL = r'https?://(?:www\.)?ruutu\.fi/video/(?P<id>\d+)'
  13. _TESTS = [
  14. {
  15. 'url': 'http://www.ruutu.fi/video/2058907',
  16. 'md5': 'ab2093f39be1ca8581963451b3c0234f',
  17. 'info_dict': {
  18. 'id': '2058907',
  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/video/2057306',
  29. 'md5': '065a10ae4d5b8cfd9d0c3d332465e3d9',
  30. 'info_dict': {
  31. 'id': '2057306',
  32. 'ext': 'mp4',
  33. 'title': 'Superpesis: katso koko kausi Ruudussa',
  34. 'description': 'md5:da2736052fef3b2bd5e0005e63c25eac',
  35. 'thumbnail': 're:^https?://.*\.jpg$',
  36. 'duration': 40,
  37. 'age_limit': 0,
  38. },
  39. },
  40. ]
  41. def _real_extract(self, url):
  42. video_id = self._match_id(url)
  43. video_xml = self._download_xml(
  44. 'http://gatling.ruutu.fi/media-xml-cache?id=%s' % video_id, video_id)
  45. formats = []
  46. processed_urls = []
  47. def extract_formats(node):
  48. for child in node:
  49. if child.tag.endswith('Files'):
  50. extract_formats(child)
  51. elif child.tag.endswith('File'):
  52. video_url = child.text
  53. if (not video_url or video_url in processed_urls or
  54. any(p in video_url for p in ('NOT_USED', 'NOT-USED'))):
  55. return
  56. processed_urls.append(video_url)
  57. ext = determine_ext(video_url)
  58. if ext == 'm3u8':
  59. formats.extend(self._extract_m3u8_formats(
  60. video_url, video_id, 'mp4', m3u8_id='hls', fatal=False))
  61. elif ext == 'f4m':
  62. formats.extend(self._extract_f4m_formats(
  63. video_url, video_id, f4m_id='hds', fatal=False))
  64. else:
  65. proto = compat_urllib_parse_urlparse(video_url).scheme
  66. if not child.tag.startswith('HTTP') and proto != 'rtmp':
  67. continue
  68. preference = -1 if proto == 'rtmp' else 1
  69. label = child.get('label')
  70. tbr = int_or_none(child.get('bitrate'))
  71. format_id = '%s-%s' % (proto, label if label else tbr) if label or tbr else proto
  72. if not self._is_valid_url(video_url, video_id, format_id):
  73. continue
  74. width, height = [int_or_none(x) for x in child.get('resolution', 'x').split('x')[:2]]
  75. formats.append({
  76. 'format_id': format_id,
  77. 'url': video_url,
  78. 'width': width,
  79. 'height': height,
  80. 'tbr': tbr,
  81. 'preference': preference,
  82. })
  83. extract_formats(video_xml.find('./Clip'))
  84. self._sort_formats(formats)
  85. return {
  86. 'id': video_id,
  87. 'title': xpath_attr(video_xml, './/Behavior/Program', 'program_name', 'title', fatal=True),
  88. 'description': xpath_attr(video_xml, './/Behavior/Program', 'description', 'description'),
  89. 'thumbnail': xpath_attr(video_xml, './/Behavior/Startpicture', 'href', 'thumbnail'),
  90. 'duration': int_or_none(xpath_text(video_xml, './/Runtime', 'duration')),
  91. 'age_limit': int_or_none(xpath_text(video_xml, './/AgeLimit', 'age limit')),
  92. 'formats': formats,
  93. }