Merge pull request #4831 from light94/master
[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             }
20         }, {
21             'url': 'http://teamcoco.com/video/louis-ck-interview-george-w-bush',
22             'md5': 'cde9ba0fa3506f5f017ce11ead928f9a',
23             'info_dict': {
24                 'id': '19705',
25                 'ext': 'mp4',
26                 "description": "Louis C.K. got starstruck by George W. Bush, so what? Part one.",
27                 "title": "Louis C.K. Interview Pt. 1 11/3/11"
28             }
29         }
30     ]
31
32     def _real_extract(self, url):
33         mobj = re.match(self._VALID_URL, url)
34
35         display_id = mobj.group('display_id')
36         webpage = self._download_webpage(url, display_id)
37
38         video_id = mobj.group("video_id")
39         if not video_id:
40             video_id = self._html_search_regex(
41                 r'<div\s+class="player".*?data-id="(\d+?)"',
42                 webpage, 'video id')
43
44         data_url = 'http://teamcoco.com/cvp/2.0/%s.xml' % video_id
45         data = self._download_xml(
46             data_url, display_id, 'Downloading data webpage')
47
48         qualities = ['500k', '480p', '1000k', '720p', '1080p']
49         formats = []
50         for filed in data.findall('files/file'):
51             if filed.attrib.get('playmode') == 'all':
52                 # it just duplicates one of the entries
53                 break
54             file_url = filed.text
55             m_format = re.search(r'(\d+(k|p))\.mp4', file_url)
56             if m_format is not None:
57                 format_id = m_format.group(1)
58             else:
59                 format_id = filed.attrib['bitrate']
60             tbr = (
61                 int(filed.attrib['bitrate'])
62                 if filed.attrib['bitrate'].isdigit()
63                 else None)
64
65             try:
66                 quality = qualities.index(format_id)
67             except ValueError:
68                 quality = -1
69             formats.append({
70                 'url': file_url,
71                 'ext': 'mp4',
72                 'tbr': tbr,
73                 'format_id': format_id,
74                 'quality': quality,
75             })
76
77         self._sort_formats(formats)
78
79         return {
80             'id': video_id,
81             'display_id': display_id,
82             'formats': formats,
83             'title': self._og_search_title(webpage),
84             'thumbnail': self._og_search_thumbnail(webpage),
85             'description': self._og_search_description(webpage),
86         }