[teamcoco] Fix "preload" data extraction (fixes #5179)
[youtube-dl] / youtube_dl / extractor / teamcoco.py
1 # -*- coding: utf-8 -*-
2 from __future__ import unicode_literals
3
4 import base64
5 import re
6
7 from .common import InfoExtractor
8 from ..utils import (
9     ExtractorError,
10     qualities,
11 )
12
13
14 class TeamcocoIE(InfoExtractor):
15     _VALID_URL = r'http://teamcoco\.com/video/(?P<video_id>[0-9]+)?/?(?P<display_id>.*)'
16     _TESTS = [
17         {
18             'url': 'http://teamcoco.com/video/80187/conan-becomes-a-mary-kay-beauty-consultant',
19             'md5': '3f7746aa0dc86de18df7539903d399ea',
20             'info_dict': {
21                 'id': '80187',
22                 'ext': 'mp4',
23                 'title': 'Conan Becomes A Mary Kay Beauty Consultant',
24                 'description': 'Mary Kay is perhaps the most trusted name in female beauty, so of course Conan is a natural choice to sell their products.',
25                 'duration': 504,
26                 'age_limit': 0,
27             }
28         }, {
29             'url': 'http://teamcoco.com/video/louis-ck-interview-george-w-bush',
30             'md5': 'cde9ba0fa3506f5f017ce11ead928f9a',
31             'info_dict': {
32                 'id': '19705',
33                 'ext': 'mp4',
34                 'description': 'Louis C.K. got starstruck by George W. Bush, so what? Part one.',
35                 'title': 'Louis C.K. Interview Pt. 1 11/3/11',
36                 'duration': 288,
37                 'age_limit': 0,
38             }
39         }, {
40             'url': 'http://teamcoco.com/video/timothy-olyphant-drinking-whiskey',
41             'info_dict': {
42                 'id': '88748',
43                 'ext': 'mp4',
44                 'title': 'Timothy Olyphant Raises A Toast To “Justified”',
45                 'description': 'md5:15501f23f020e793aeca761205e42c24',
46             },
47             'params': {
48                 'skip_download': True,  # m3u8 downloads
49             }
50         }
51     ]
52     _VIDEO_ID_REGEXES = (
53         r'"eVar42"\s*:\s*(\d+)',
54         r'Ginger\.TeamCoco\.openInApp\("video",\s*"([^"]+)"',
55         r'"id_not"\s*:\s*(\d+)'
56     )
57
58     def _real_extract(self, url):
59         mobj = re.match(self._VALID_URL, url)
60
61         display_id = mobj.group('display_id')
62         webpage = self._download_webpage(url, display_id)
63
64         video_id = mobj.group('video_id')
65         if not video_id:
66             video_id = self._html_search_regex(
67                 self._VIDEO_ID_REGEXES, webpage, 'video id')
68
69         preload = None
70         preloads = re.findall(r'"preload":\s*"([^"]+)"', webpage)
71         if preloads:
72             preload = max([(len(p), p) for p in preloads])[1]
73
74         if not preload:
75             preload = ''.join(re.findall(r'this\.push\("([^"]+)"\);', webpage))
76
77         if not preload:
78             preload = self._html_search_regex([
79                 r'player,\[?"([^"]+)"\]?', r'player.init\(\[?"([^"]+)"\]?\)'
80             ], webpage.replace('","', ''), 'preload data', default=None)
81
82         if not preload:
83             raise ExtractorError(
84                 'Preload information could not be extracted', expected=True)
85
86         data = self._parse_json(
87             base64.b64decode(preload.encode('ascii')).decode('utf-8'), video_id)
88
89         formats = []
90         get_quality = qualities(['500k', '480p', '1000k', '720p', '1080p'])
91         for filed in data['files']:
92             if filed['type'] == 'hls':
93                 formats.extend(self._extract_m3u8_formats(
94                     filed['url'], video_id, ext='mp4'))
95             else:
96                 m_format = re.search(r'(\d+(k|p))\.mp4', filed['url'])
97                 if m_format is not None:
98                     format_id = m_format.group(1)
99                 else:
100                     format_id = filed['bitrate']
101                 tbr = (
102                     int(filed['bitrate'])
103                     if filed['bitrate'].isdigit()
104                     else None)
105
106                 formats.append({
107                     'url': filed['url'],
108                     'ext': 'mp4',
109                     'tbr': tbr,
110                     'format_id': format_id,
111                     'quality': get_quality(format_id),
112                 })
113
114         self._sort_formats(formats)
115
116         return {
117             'id': video_id,
118             'display_id': display_id,
119             'formats': formats,
120             'title': data['title'],
121             'thumbnail': data.get('thumb', {}).get('href'),
122             'description': data.get('teaser'),
123             'duration': data.get('duration'),
124             'age_limit': self._family_friendly_search(webpage),
125         }