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