Workaround for regex engine limitation
[youtube-dl] / youtube_dl / extractor / teamcoco.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     ExtractorError,
8 )
9
10
11 class TeamcocoIE(InfoExtractor):
12     _VALID_URL = r'http://teamcoco\.com/video/([^/]*)?/?(.*)'
13     _TESTS = [
14     {
15         'url': 'http://teamcoco.com/video/80187/conan-becomes-a-mary-kay-beauty-consultant',
16         'file': '80187.mp4',
17         'md5': '3f7746aa0dc86de18df7539903d399ea',
18         'info_dict': {
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         }
22     },
23     {
24         'url': 'http://teamcoco.com/video/louis-ck-interview-george-w-bush',
25         'file': '19705.mp4',
26         'md5': 'cde9ba0fa3506f5f017ce11ead928f9a',
27         'info_dict': {
28             "description": "Louis C.K. got starstruck by George W. Bush, so what? Part one.",
29             "title": "Louis C.K. Interview Pt. 1 11/3/11"
30         }
31     }
32     ]
33
34     def _real_extract(self, url):
35         mobj = re.match(self._VALID_URL, url)
36         if mobj is None:
37             raise ExtractorError('Invalid URL: %s' % url)
38         url_title = mobj.group(2)
39         if url_title == '':
40             url_title = mobj.group(1)
41         webpage = self._download_webpage(url, url_title)
42         
43         video_id = mobj.group(1)
44         if mobj.group(2) == '':
45             video_id = self._html_search_regex(
46                 r'<article class="video" data-id="(\d+?)"',
47                 webpage, 'video id')
48         
49         self.report_extraction(video_id)
50
51         data_url = 'http://teamcoco.com/cvp/2.0/%s.xml' % video_id
52         data = self._download_xml(data_url, video_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             'formats': formats,
88             'title': self._og_search_title(webpage),
89             'thumbnail': self._og_search_thumbnail(webpage),
90             'description': self._og_search_description(webpage),
91         }