[ora] minimise fragile regex shenanigans; recognise unsafespeech.com URLs
[youtube-dl] / youtube_dl / extractor / ora.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 from .common import InfoExtractor
6 from ..compat import compat_urlparse
7 from ..utils import (
8     get_element_by_attribute,
9     js_to_json,
10     qualities,
11     unescapeHTML,
12 )
13
14
15 class OraTVIE(InfoExtractor):
16     _VALID_URL = r'https?://(?:www\.)?(ora\.tv|unsafespeech\.com)/([^/]+/)*(?P<id>[^/\?#]+)'
17     _TEST = {
18         'url': 'https://www.ora.tv/larrykingnow/2015/12/16/vine-youtube-stars-zach-king-king-bach-on-their-viral-videos-0_36jupg6090pq',
19         'md5': 'fa33717591c631ec93b04b0e330df786',
20         'info_dict': {
21             'id': '50178',
22             'ext': 'mp4',
23             'title': 'Vine & YouTube Stars Zach King & King Bach On Their Viral Videos!',
24             'description': 'md5:ebbc5b1424dd5dba7be7538148287ac1',
25         }
26     }
27
28     def _real_extract(self, url):
29         display_id = self._match_id(url)
30         webpage = self._download_webpage(url, display_id)
31
32         ora_meta = self._parse_json(self._search_regex(
33             r'(?s);\s*ora_meta = ({.*?});</script>', webpage, 'ora_meta'), display_id,
34             transform_source=lambda data: js_to_json(re.sub('":(document|\().*?(:false|\(\)),', '":null,', data)))
35
36         video_data = ora_meta.get('video', ora_meta.get('current'))
37         m3u8_url = video_data['hls_stream']
38
39         if m3u8_url:
40             formats = self._extract_m3u8_formats(
41                 m3u8_url, display_id, 'mp4', 'm3u8_native',
42                 m3u8_id='hls', fatal=False)
43             # similar to GameSpotIE
44             m3u8_path = compat_urlparse.urlparse(m3u8_url).path
45             QUALITIES_RE = r'((,[a-z]+\d+)+,?)'
46             available_qualities = self._search_regex(
47                 QUALITIES_RE, m3u8_path, 'qualities').strip(',').split(',')
48             http_path = m3u8_path[1:].split('/', 1)[1]
49             http_template = re.sub(QUALITIES_RE, r'%s', http_path)
50             http_template = http_template.replace('.csmil/master.m3u8', '')
51             http_template = compat_urlparse.urljoin(
52                 'http://videocdn-pmd.ora.tv/', http_template)
53             preference = qualities(
54                 ['mobile400', 'basic400', 'basic600', 'sd900', 'sd1200', 'sd1500', 'hd720', 'hd1080'])
55             for q in available_qualities:
56                 formats.append({
57                     'url': http_template % q,
58                     'format_id': q,
59                     'preference': preference(q),
60                 })
61             self._sort_formats(formats)
62         else:
63             return self.url_result(self._search_regex(
64                 r'"youtube_id"\s*:\s*"([^"]+)', webpage, 'youtube id'), 'Youtube')
65
66         return {
67             'id': video_data.get('id', display_id),
68             'display_id': display_id,
69             'title': unescapeHTML(self._og_search_title(webpage)),
70             'description': get_element_by_attribute(
71                 'class', 'video_txt_decription', webpage),
72             'thumbnail': self._proto_relative_url(video_data.get('thumb')),
73             'formats': formats,
74         }