Merge remote-tracking branch 'rzhxeo/crunchyroll'
[youtube-dl] / youtube_dl / extractor / teamcoco.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5     ExtractorError,
6 )
7
8
9 class TeamcocoIE(InfoExtractor):
10     _VALID_URL = r'http://teamcoco\.com/video/(?P<url_title>.*)'
11     _TEST = {
12         u'url': u'http://teamcoco.com/video/louis-ck-interview-george-w-bush',
13         u'file': u'19705.mp4',
14         u'md5': u'cde9ba0fa3506f5f017ce11ead928f9a',
15         u'info_dict': {
16             u"description": u"Louis C.K. got starstruck by George W. Bush, so what? Part one.", 
17             u"title": u"Louis C.K. Interview Pt. 1 11/3/11"
18         }
19     }
20
21     def _real_extract(self, url):
22         mobj = re.match(self._VALID_URL, url)
23         if mobj is None:
24             raise ExtractorError(u'Invalid URL: %s' % url)
25         url_title = mobj.group('url_title')
26         webpage = self._download_webpage(url, url_title)
27
28         video_id = self._html_search_regex(r'<article class="video" data-id="(\d+?)"',
29             webpage, u'video id')
30
31         self.report_extraction(video_id)
32
33         data_url = 'http://teamcoco.com/cvp/2.0/%s.xml' % video_id
34         data = self._download_xml(data_url, video_id, 'Downloading data webpage')
35
36
37         qualities = ['500k', '480p', '1000k', '720p', '1080p']
38         formats = []
39         for file in data.findall('files/file'):
40             if file.attrib.get('playmode') == 'all':
41                 # it just duplicates one of the entries
42                 break
43             file_url = file.text
44             m_format = re.search(r'(\d+(k|p))\.mp4', file_url)
45             if m_format is not None:
46                 format_id = m_format.group(1)
47             else:
48                 format_id = file.attrib['bitrate']
49             formats.append({
50                 'url': file_url,
51                 'ext': 'mp4',
52                 'format_id': format_id,
53             })
54         def sort_key(f):
55             try:
56                 return qualities.index(f['format_id'])
57             except ValueError:
58                 return -1
59         formats.sort(key=sort_key)
60         if not formats:
61             raise ExtractorError(u'Unable to extract video URL')
62
63         return {
64             'id':          video_id,
65             'formats': formats,
66             'title':       self._og_search_title(webpage),
67             'thumbnail':   self._og_search_thumbnail(webpage),
68             'description': self._og_search_description(webpage),
69         }