]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/ruutu.py
Imported Upstream version 2015.07.21
[youtubedl] / 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_text,
10 )
11
12
13 class RuutuIE(InfoExtractor):
14 _VALID_URL = r'http://(?:www\.)?ruutu\.fi/ohjelmat/(?:[^/?#]+/)*(?P<id>[^/?#]+)'
15 _TESTS = [
16 {
17 'url': 'http://www.ruutu.fi/ohjelmat/oletko-aina-halunnut-tietaa-mita-tapahtuu-vain-hetki-ennen-lahetysta-nyt-se-selvisi',
18 'md5': 'ab2093f39be1ca8581963451b3c0234f',
19 'info_dict': {
20 'id': '2058907',
21 'display_id': 'oletko-aina-halunnut-tietaa-mita-tapahtuu-vain-hetki-ennen-lahetysta-nyt-se-selvisi',
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/ohjelmat/superpesis/superpesis-katso-koko-kausi-ruudussa',
32 'md5': '065a10ae4d5b8cfd9d0c3d332465e3d9',
33 'info_dict': {
34 'id': '2057306',
35 'display_id': 'superpesis-katso-koko-kausi-ruudussa',
36 'ext': 'mp4',
37 'title': 'Superpesis: katso koko kausi Ruudussa',
38 'description': 'md5:44c44a99fdbe5b380ab74ebd75f0af77',
39 'thumbnail': 're:^https?://.*\.jpg$',
40 'duration': 40,
41 'age_limit': 0,
42 },
43 },
44 ]
45
46 def _real_extract(self, url):
47 display_id = self._match_id(url)
48
49 webpage = self._download_webpage(url, display_id)
50
51 video_id = self._search_regex(
52 r'data-media-id="(\d+)"', webpage, 'media id')
53
54 video_xml_url = None
55
56 media_data = self._search_regex(
57 r'jQuery\.extend\([^,]+,\s*(.+?)\);', webpage,
58 'media data', default=None)
59 if media_data:
60 media_json = self._parse_json(media_data, display_id, fatal=False)
61 if media_json:
62 xml_url = media_json.get('ruutuplayer', {}).get('xmlUrl')
63 if xml_url:
64 video_xml_url = xml_url.replace('{ID}', video_id)
65
66 if not video_xml_url:
67 video_xml_url = 'http://gatling.ruutu.fi/media-xml-cache?id=%s' % video_id
68
69 video_xml = self._download_xml(video_xml_url, video_id)
70
71 formats = []
72 processed_urls = []
73
74 def extract_formats(node):
75 for child in node:
76 if child.tag.endswith('Files'):
77 extract_formats(child)
78 elif child.tag.endswith('File'):
79 video_url = child.text
80 if not video_url or video_url in processed_urls or 'NOT_USED' in video_url:
81 return
82 processed_urls.append(video_url)
83 ext = determine_ext(video_url)
84 if ext == 'm3u8':
85 formats.extend(self._extract_m3u8_formats(
86 video_url, video_id, 'mp4', m3u8_id='hls'))
87 elif ext == 'f4m':
88 formats.extend(self._extract_f4m_formats(
89 video_url, video_id, f4m_id='hds'))
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 width, height = [int_or_none(x) for x in child.get('resolution', '').split('x')]
98 formats.append({
99 'format_id': '%s-%s' % (proto, label if label else tbr),
100 'url': video_url,
101 'width': width,
102 'height': height,
103 'tbr': tbr,
104 'preference': preference,
105 })
106
107 extract_formats(video_xml.find('./Clip'))
108 self._sort_formats(formats)
109
110 return {
111 'id': video_id,
112 'display_id': display_id,
113 'title': self._og_search_title(webpage),
114 'description': self._og_search_description(webpage),
115 'thumbnail': self._og_search_thumbnail(webpage),
116 'duration': int_or_none(xpath_text(video_xml, './/Runtime', 'duration')),
117 'age_limit': int_or_none(xpath_text(video_xml, './/AgeLimit', 'age limit')),
118 'formats': formats,
119 }