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