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