Merge remote-tracking branch 'yan12125/IE_camdemy'
[youtube-dl] / youtube_dl / extractor / teamcoco.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6
7
8 class TeamcocoIE(InfoExtractor):
9     _VALID_URL = r'http://teamcoco\.com/video/(?P<video_id>[0-9]+)?/?(?P<display_id>.*)'
10     _TESTS = [
11         {
12             'url': 'http://teamcoco.com/video/80187/conan-becomes-a-mary-kay-beauty-consultant',
13             'md5': '3f7746aa0dc86de18df7539903d399ea',
14             'info_dict': {
15                 'id': '80187',
16                 'ext': 'mp4',
17                 'title': 'Conan Becomes A Mary Kay Beauty Consultant',
18                 'description': 'Mary Kay is perhaps the most trusted name in female beauty, so of course Conan is a natural choice to sell their products.',
19                 'age_limit': 0,
20             }
21         }, {
22             'url': 'http://teamcoco.com/video/louis-ck-interview-george-w-bush',
23             'md5': 'cde9ba0fa3506f5f017ce11ead928f9a',
24             'info_dict': {
25                 'id': '19705',
26                 'ext': 'mp4',
27                 "description": "Louis C.K. got starstruck by George W. Bush, so what? Part one.",
28                 "title": "Louis C.K. Interview Pt. 1 11/3/11",
29                 'age_limit': 0,
30             }
31         }
32     ]
33     _VIDEO_ID_REGEXES = (
34         r'"eVar42"\s*:\s*(\d+)',
35         r'Ginger\.TeamCoco\.openInApp\("video",\s*"([^"]+)"',
36         r'"id_not"\s*:\s*(\d+)'
37     )
38
39     def _real_extract(self, url):
40         mobj = re.match(self._VALID_URL, url)
41
42         display_id = mobj.group('display_id')
43         webpage = self._download_webpage(url, display_id)
44
45         video_id = mobj.group("video_id")
46         if not video_id:
47             video_id = self._html_search_regex(
48                 self._VIDEO_ID_REGEXES, webpage, 'video id')
49
50         data_url = 'http://teamcoco.com/cvp/2.0/%s.xml' % video_id
51         data = self._download_xml(
52             data_url, display_id, 'Downloading data webpage')
53
54         qualities = ['500k', '480p', '1000k', '720p', '1080p']
55         formats = []
56         for filed in data.findall('files/file'):
57             if filed.attrib.get('playmode') == 'all':
58                 # it just duplicates one of the entries
59                 break
60             file_url = filed.text
61             m_format = re.search(r'(\d+(k|p))\.mp4', file_url)
62             if m_format is not None:
63                 format_id = m_format.group(1)
64             else:
65                 format_id = filed.attrib['bitrate']
66             tbr = (
67                 int(filed.attrib['bitrate'])
68                 if filed.attrib['bitrate'].isdigit()
69                 else None)
70
71             try:
72                 quality = qualities.index(format_id)
73             except ValueError:
74                 quality = -1
75             formats.append({
76                 'url': file_url,
77                 'ext': 'mp4',
78                 'tbr': tbr,
79                 'format_id': format_id,
80                 'quality': quality,
81             })
82
83         self._sort_formats(formats)
84
85         return {
86             'id': video_id,
87             'display_id': display_id,
88             'formats': formats,
89             'title': self._og_search_title(webpage),
90             'thumbnail': self._og_search_thumbnail(webpage),
91             'description': self._og_search_description(webpage),
92             'age_limit': self._family_friendly_search(webpage),
93         }