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