2 from __future__ 
import unicode_literals
 
   4 from .common 
import InfoExtractor
 
   5 from ..compat 
import compat_urllib_parse_urlparse
 
  15 class RuutuIE(InfoExtractor
): 
  16     _VALID_URL 
= r
'https?://(?:www\.)?(?:ruutu|supla)\.fi/(?:video|supla)/(?P<id>\d+)' 
  19             'url': 'http://www.ruutu.fi/video/2058907', 
  20             'md5': 'ab2093f39be1ca8581963451b3c0234f', 
  24                 'title': 'Oletko aina halunnut tietää mitä tapahtuu vain hetki ennen lähetystä? - Nyt se selvisi!', 
  25                 'description': 'md5:cfc6ccf0e57a814360df464a91ff67d6', 
  26                 'thumbnail': 're:^https?://.*\.jpg$', 
  32             'url': 'http://www.ruutu.fi/video/2057306', 
  33             'md5': '065a10ae4d5b8cfd9d0c3d332465e3d9', 
  37                 'title': 'Superpesis: katso koko kausi Ruudussa', 
  38                 'description': 'md5:bfb7336df2a12dc21d18fa696c9f8f23', 
  39                 'thumbnail': 're:^https?://.*\.jpg$', 
  45             'url': 'http://www.supla.fi/supla/2231370', 
  46             'md5': 'df14e782d49a2c0df03d3be2a54ef949', 
  50                 'title': 'Osa 1: Mikael Jungner', 
  51                 'description': 'md5:7d90f358c47542e3072ff65d7b1bcffe', 
  52                 'thumbnail': 're:^https?://.*\.jpg$', 
  58     def _real_extract(self
, url
): 
  59         video_id 
= self
._match
_id
(url
) 
  61         video_xml 
= self
._download
_xml
( 
  62             'http://gatling.ruutu.fi/media-xml-cache?id=%s' % video_id
, video_id
) 
  67         def extract_formats(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'))): 
  76                     processed_urls
.append(video_url
) 
  77                     ext 
= determine_ext(video_url
) 
  79                         formats
.extend(self
._extract
_m
3u8_formats
( 
  80                             video_url
, video_id
, 'mp4', m3u8_id
='hls', fatal
=False)) 
  82                         formats
.extend(self
._extract
_f
4m
_formats
( 
  83                             video_url
, video_id
, f4m_id
='hds', fatal
=False)) 
  85                         proto 
= compat_urllib_parse_urlparse(video_url
).scheme
 
  86                         if not child
.tag
.startswith('HTTP') and proto 
!= 'rtmp': 
  88                         preference 
= -1 if proto 
== 'rtmp' else 1 
  89                         label 
= child
.get('label') 
  90                         tbr 
= int_or_none(child
.get('bitrate')) 
  91                         format_id 
= '%s-%s' % (proto
, label 
if label 
else tbr
) if label 
or tbr 
else proto
 
  92                         if not self
._is
_valid
_url
(video_url
, video_id
, format_id
): 
  94                         width
, height 
= [int_or_none(x
) for x 
in child
.get('resolution', 'x').split('x')[:2]] 
  96                             'format_id': format_id
, 
 101                             'preference': preference
, 
 104         extract_formats(video_xml
.find('./Clip')) 
 106         drm 
= xpath_text(video_xml
, './Clip/DRM', default
=None) 
 107         if not formats 
and drm
: 
 108             raise ExtractorError('This video is DRM protected.', expected
=True) 
 110         self
._sort
_formats
(formats
) 
 114             'title': xpath_attr(video_xml
, './/Behavior/Program', 'program_name', 'title', fatal
=True), 
 115             'description': xpath_attr(video_xml
, './/Behavior/Program', 'description', 'description'), 
 116             'thumbnail': xpath_attr(video_xml
, './/Behavior/Startpicture', 'href', 'thumbnail'), 
 117             'duration': int_or_none(xpath_text(video_xml
, './/Runtime', 'duration')), 
 118             'age_limit': int_or_none(xpath_text(video_xml
, './/AgeLimit', 'age limit')),