summaryrefslogtreecommitdiff
path: root/youtube_dl/extractor/ruutu.py
blob: 6c09df25a07de17dfb29ead61bd1464e72a1c36a (plain)
    1 # coding: utf-8
    2 from __future__ import unicode_literals
    3 
    4 from .common import InfoExtractor
    5 from ..compat import compat_urllib_parse_urlparse
    6 from ..utils import (
    7     determine_ext,
    8     ExtractorError,
    9     int_or_none,
   10     xpath_attr,
   11     xpath_text,
   12 )
   13 
   14 
   15 class RuutuIE(InfoExtractor):
   16     _VALID_URL = r'https?://(?:www\.)?(?:ruutu|supla)\.fi/(?:video|supla)/(?P<id>\d+)'
   17     _TESTS = [
   18         {
   19             'url': 'http://www.ruutu.fi/video/2058907',
   20             'md5': 'ab2093f39be1ca8581963451b3c0234f',
   21             'info_dict': {
   22                 'id': '2058907',
   23                 'ext': 'mp4',
   24                 'title': 'Oletko aina halunnut tietää mitä tapahtuu vain hetki ennen lähetystä? - Nyt se selvisi!',
   25                 'description': 'md5:cfc6ccf0e57a814360df464a91ff67d6',
   26                 'thumbnail': r're:^https?://.*\.jpg$',
   27                 'duration': 114,
   28                 'age_limit': 0,
   29             },
   30         },
   31         {
   32             'url': 'http://www.ruutu.fi/video/2057306',
   33             'md5': '065a10ae4d5b8cfd9d0c3d332465e3d9',
   34             'info_dict': {
   35                 'id': '2057306',
   36                 'ext': 'mp4',
   37                 'title': 'Superpesis: katso koko kausi Ruudussa',
   38                 'description': 'md5:bfb7336df2a12dc21d18fa696c9f8f23',
   39                 'thumbnail': r're:^https?://.*\.jpg$',
   40                 'duration': 40,
   41                 'age_limit': 0,
   42             },
   43         },
   44         {
   45             'url': 'http://www.supla.fi/supla/2231370',
   46             'md5': 'df14e782d49a2c0df03d3be2a54ef949',
   47             'info_dict': {
   48                 'id': '2231370',
   49                 'ext': 'mp4',
   50                 'title': 'Osa 1: Mikael Jungner',
   51                 'description': 'md5:7d90f358c47542e3072ff65d7b1bcffe',
   52                 'thumbnail': r're:^https?://.*\.jpg$',
   53                 'age_limit': 0,
   54             },
   55         },
   56     ]
   57 
   58     def _real_extract(self, url):
   59         video_id = self._match_id(url)
   60 
   61         video_xml = self._download_xml(
   62             'http://gatling.ruutu.fi/media-xml-cache?id=%s' % video_id, video_id)
   63 
   64         formats = []
   65         processed_urls = []
   66 
   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                         return
   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 
  110         extract_formats(video_xml.find('./Clip'))
  111 
  112         drm = xpath_text(video_xml, './Clip/DRM', default=None)
  113         if not formats and drm:
  114             raise ExtractorError('This video is DRM protected.', expected=True)
  115 
  116         self._sort_formats(formats)
  117 
  118         return {
  119             'id': video_id,
  120             'title': xpath_attr(video_xml, './/Behavior/Program', 'program_name', 'title', fatal=True),
  121             'description': xpath_attr(video_xml, './/Behavior/Program', 'description', 'description'),
  122             'thumbnail': xpath_attr(video_xml, './/Behavior/Startpicture', 'href', 'thumbnail'),
  123             'duration': int_or_none(xpath_text(video_xml, './/Runtime', 'duration')),
  124             'age_limit': int_or_none(xpath_text(video_xml, './/AgeLimit', 'age limit')),
  125             'formats': formats,
  126         }

Generated by cgit