[trutube] Support multiple formats (#2433)
[youtube-dl] / youtube_dl / extractor / trutube.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 TruTubeIE(InfoExtractor):
12     _VALID_URL = r'https?://(?:www\.)?trutube\.tv/video/(?P<id>[0-9]+)/.*'
13     _TEST = {
14         'url': 'http://trutube.tv/video/14880/Ramses-II-Proven-To-Be-A-Red-Headed-Caucasoid-',
15         'md5': 'c5b6e301b0a2040b074746cbeaa26ca1',
16         'info_dict': {
17             'id': '14880',
18             'ext': 'flv',
19             'title': 'Ramses II - Proven To Be A Red Headed Caucasoid',
20             'thumbnail': 're:^http:.*\.jpg$',
21         }
22     }
23
24     def _real_extract(self, url):
25         mobj = re.match(self._VALID_URL, url)
26         video_id = mobj.group('id')
27
28         webpage = self._download_webpage(url, video_id)
29         video_title = self._og_search_title(webpage).strip()
30         thumbnail = self._search_regex(
31             r"var splash_img = '([^']+)';", webpage, 'thumbnail', fatal=False)
32
33         all_formats = re.finditer(
34             r"var (?P<key>[a-z]+)_video_file\s*=\s*'(?P<url>[^']+)';", webpage)
35         formats = [{
36             'format_id': m.group('key'),
37             'quality': -i,
38             'url': m.group('url'),
39         } for i, m in enumerate(all_formats)]
40         self._sort_formats(formats)
41
42         return {
43             'id': video_id,
44             'title': video_title,
45             'formats': formats,
46             'thumbnail': thumbnail,
47         }