Merge branch 'master' of https://github.com/DarkstaIkers/youtube-dl into DarkstaIkers...
[youtube-dl] / youtube_dl / extractor / ruutu.py
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     int_or_none,
9     xpath_attr,
10     xpath_text,
11 )
12
13
14 class RuutuIE(InfoExtractor):
15     _VALID_URL = r'https?://(?:www\.)?(?:ruutu|supla)\.fi/(?:video|supla)/(?P<id>\d+)'
16     _TESTS = [
17         {
18             'url': 'http://www.ruutu.fi/video/2058907',
19             'md5': 'ab2093f39be1ca8581963451b3c0234f',
20             'info_dict': {
21                 'id': '2058907',
22                 'ext': 'mp4',
23                 'title': 'Oletko aina halunnut tietää mitä tapahtuu vain hetki ennen lähetystä? - Nyt se selvisi!',
24                 'description': 'md5:cfc6ccf0e57a814360df464a91ff67d6',
25                 'thumbnail': 're:^https?://.*\.jpg$',
26                 'duration': 114,
27                 'age_limit': 0,
28             },
29         },
30         {
31             'url': 'http://www.ruutu.fi/video/2057306',
32             'md5': '065a10ae4d5b8cfd9d0c3d332465e3d9',
33             'info_dict': {
34                 'id': '2057306',
35                 'ext': 'mp4',
36                 'title': 'Superpesis: katso koko kausi Ruudussa',
37                 'description': 'md5:bfb7336df2a12dc21d18fa696c9f8f23',
38                 'thumbnail': 're:^https?://.*\.jpg$',
39                 'duration': 40,
40                 'age_limit': 0,
41             },
42         },
43         {
44             'url': 'http://www.supla.fi/supla/2231370',
45             'md5': 'df14e782d49a2c0df03d3be2a54ef949',
46             'info_dict': {
47                 'id': '2231370',
48                 'ext': 'mp4',
49                 'title': 'Osa 1: Mikael Jungner',
50                 'description': 'md5:7d90f358c47542e3072ff65d7b1bcffe',
51                 'thumbnail': 're:^https?://.*\.jpg$',
52                 'age_limit': 0,
53             },
54         },
55     ]
56
57     def _real_extract(self, url):
58         video_id = self._match_id(url)
59
60         video_xml = self._download_xml(
61             'http://gatling.ruutu.fi/media-xml-cache?id=%s' % video_id, video_id)
62
63         formats = []
64         processed_urls = []
65
66         def extract_formats(node):
67             for child in node:
68                 if child.tag.endswith('Files'):
69                     extract_formats(child)
70                 elif child.tag.endswith('File'):
71                     video_url = child.text
72                     if (not video_url or video_url in processed_urls or
73                             any(p in video_url for p in ('NOT_USED', 'NOT-USED'))):
74                         return
75                     processed_urls.append(video_url)
76                     ext = determine_ext(video_url)
77                     if ext == 'm3u8':
78                         formats.extend(self._extract_m3u8_formats(
79                             video_url, video_id, 'mp4', m3u8_id='hls', fatal=False))
80                     elif ext == 'f4m':
81                         formats.extend(self._extract_f4m_formats(
82                             video_url, video_id, f4m_id='hds', 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
103         extract_formats(video_xml.find('./Clip'))
104         self._sort_formats(formats)
105
106         return {
107             'id': video_id,
108             'title': xpath_attr(video_xml, './/Behavior/Program', 'program_name', 'title', fatal=True),
109             'description': xpath_attr(video_xml, './/Behavior/Program', 'description', 'description'),
110             'thumbnail': xpath_attr(video_xml, './/Behavior/Startpicture', 'href', 'thumbnail'),
111             'duration': int_or_none(xpath_text(video_xml, './/Runtime', 'duration')),
112             'age_limit': int_or_none(xpath_text(video_xml, './/AgeLimit', 'age limit')),
113             'formats': formats,
114         }