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