[bliptv] remove extractor and add support for site replacement(makertv)
[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\.fi/video/(?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:da2736052fef3b2bd5e0005e63c25eac',
38                 'thumbnail': 're:^https?://.*\.jpg$',
39                 'duration': 40,
40                 'age_limit': 0,
41             },
42         },
43     ]
44
45     def _real_extract(self, url):
46         video_id = self._match_id(url)
47
48         video_xml = self._download_xml(
49             'http://gatling.ruutu.fi/media-xml-cache?id=%s' % video_id, video_id)
50
51         formats = []
52         processed_urls = []
53
54         def extract_formats(node):
55             for child in node:
56                 if child.tag.endswith('Files'):
57                     extract_formats(child)
58                 elif child.tag.endswith('File'):
59                     video_url = child.text
60                     if not video_url or video_url in processed_urls or 'NOT_USED' in video_url:
61                         return
62                     processed_urls.append(video_url)
63                     ext = determine_ext(video_url)
64                     if ext == 'm3u8':
65                         formats.extend(self._extract_m3u8_formats(
66                             video_url, video_id, 'mp4', m3u8_id='hls'))
67                     elif ext == 'f4m':
68                         formats.extend(self._extract_f4m_formats(
69                             video_url, video_id, f4m_id='hds'))
70                     else:
71                         proto = compat_urllib_parse_urlparse(video_url).scheme
72                         if not child.tag.startswith('HTTP') and proto != 'rtmp':
73                             continue
74                         preference = -1 if proto == 'rtmp' else 1
75                         label = child.get('label')
76                         tbr = int_or_none(child.get('bitrate'))
77                         width, height = [int_or_none(x) for x in child.get('resolution', 'x').split('x')[:2]]
78                         formats.append({
79                             'format_id': '%s-%s' % (proto, label if label else tbr),
80                             'url': video_url,
81                             'width': width,
82                             'height': height,
83                             'tbr': tbr,
84                             'preference': preference,
85                         })
86
87         extract_formats(video_xml.find('./Clip'))
88         self._sort_formats(formats)
89
90         return {
91             'id': video_id,
92             'title': xpath_attr(video_xml, './/Behavior/Program', 'program_name', 'title', fatal=True),
93             'description': xpath_attr(video_xml, './/Behavior/Program', 'description', 'description'),
94             'thumbnail': xpath_attr(video_xml, './/Behavior/Startpicture', 'href', 'thumbnail'),
95             'duration': int_or_none(xpath_text(video_xml, './/Runtime', 'duration')),
96             'age_limit': int_or_none(xpath_text(video_xml, './/AgeLimit', 'age limit')),
97             'formats': formats,
98         }