[youtube] Fix extraction.
[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     qualities,
10     unescapeHTML,
11 )
12
13
14 class OraTVIE(InfoExtractor):
15     _VALID_URL = r'https?://(?:www\.)?(?:ora\.tv|unsafespeech\.com)/([^/]+/)*(?P<id>[^/\?#]+)'
16     _TESTS = [{
17         'url': 'https://www.ora.tv/larrykingnow/2015/12/16/vine-youtube-stars-zach-king-king-bach-on-their-viral-videos-0_36jupg6090pq',
18         'md5': 'fa33717591c631ec93b04b0e330df786',
19         'info_dict': {
20             'id': '50178',
21             'ext': 'mp4',
22             'title': 'Vine & YouTube Stars Zach King & King Bach On Their Viral Videos!',
23             'description': 'md5:ebbc5b1424dd5dba7be7538148287ac1',
24         }
25     }, {
26         'url': 'http://www.unsafespeech.com/video/2016/5/10/student-self-censorship-and-the-thought-police-on-university-campuses-0_6622bnkppw4d',
27         'only_matching': True,
28     }]
29
30     def _real_extract(self, url):
31         display_id = self._match_id(url)
32         webpage = self._download_webpage(url, display_id)
33
34         video_data = self._search_regex(
35             r'"(?:video|current)"\s*:\s*({[^}]+?})', webpage, 'current video')
36         m3u8_url = self._search_regex(
37             r'hls_stream"?\s*:\s*"([^"]+)', video_data, 'm3u8 url', None)
38         if m3u8_url:
39             formats = self._extract_m3u8_formats(
40                 m3u8_url, display_id, 'mp4', 'm3u8_native',
41                 m3u8_id='hls', fatal=False)
42             # similar to GameSpotIE
43             m3u8_path = compat_urlparse.urlparse(m3u8_url).path
44             QUALITIES_RE = r'((,[a-z]+\d+)+,?)'
45             available_qualities = self._search_regex(
46                 QUALITIES_RE, m3u8_path, 'qualities').strip(',').split(',')
47             http_path = m3u8_path[1:].split('/', 1)[1]
48             http_template = re.sub(QUALITIES_RE, r'%s', http_path)
49             http_template = http_template.replace('.csmil/master.m3u8', '')
50             http_template = compat_urlparse.urljoin(
51                 'http://videocdn-pmd.ora.tv/', http_template)
52             preference = qualities(
53                 ['mobile400', 'basic400', 'basic600', 'sd900', 'sd1200', 'sd1500', 'hd720', 'hd1080'])
54             for q in available_qualities:
55                 formats.append({
56                     'url': http_template % q,
57                     'format_id': q,
58                     'preference': preference(q),
59                 })
60             self._sort_formats(formats)
61         else:
62             return self.url_result(self._search_regex(
63                 r'"youtube_id"\s*:\s*"([^"]+)', webpage, 'youtube id'), 'Youtube')
64
65         return {
66             'id': self._search_regex(
67                 r'"id"\s*:\s*(\d+)', video_data, 'video id', default=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(self._search_regex(
73                 r'"thumb"\s*:\s*"([^"]+)', video_data, 'thumbnail', None)),
74             'formats': formats,
75         }