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