]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/testtube.py
[SpankBang] Use python2.6 compatible string formatting spec
[youtube-dl] / youtube_dl / extractor / testtube.py
1 from __future__ import unicode_literals
2
3 from .common import InfoExtractor
4 from ..utils import (
5     int_or_none,
6     qualities,
7 )
8
9
10 class TestTubeIE(InfoExtractor):
11     _VALID_URL = r'https?://testtube\.com/[^/?#]+/(?P<id>[^/?#]+)'
12     _TESTS = [{
13         'url': 'https://testtube.com/dnews/5-weird-ways-plants-can-eat-animals?utm_source=FB&utm_medium=DNews&utm_campaign=DNewsSocial',
14         'info_dict': {
15             'id': '60163',
16             'display_id': '5-weird-ways-plants-can-eat-animals',
17             'duration': 275,
18             'ext': 'mp4',
19             'title': '5 Weird Ways Plants Can Eat Animals',
20             'description': 'Why have some plants evolved to eat meat?',
21             'thumbnail': 're:^https?://.*\.jpg$',
22             'uploader': 'DNews',
23             'uploader_id': 'dnews',
24         },
25     }]
26
27     def _real_extract(self, url):
28         display_id = self._match_id(url)
29
30         webpage = self._download_webpage(url, display_id)
31         video_id = self._search_regex(
32             r"player\.loadRevision3Item\('video_id',\s*([0-9]+)\);",
33             webpage, 'video ID')
34
35         all_info = self._download_json(
36             'https://testtube.com/api/getPlaylist.json?api_key=ba9c741bce1b9d8e3defcc22193f3651b8867e62&codecs=h264,vp8,theora&video_id=%s' % video_id,
37             video_id)
38         info = all_info['items'][0]
39
40         formats = []
41         for vcodec, fdatas in info['media'].items():
42             for name, fdata in fdatas.items():
43                 formats.append({
44                     'format_id': '%s-%s' % (vcodec, name),
45                     'url': fdata['url'],
46                     'vcodec': vcodec,
47                     'tbr': fdata.get('bitrate'),
48                 })
49         self._sort_formats(formats)
50
51         duration = int_or_none(info.get('duration'))
52         images = info.get('images')
53         thumbnails = None
54         preference = qualities(['mini', 'small', 'medium', 'large'])
55         if images:
56             thumbnails = [{
57                 'id': thumbnail_id,
58                 'url': img_url,
59                 'preference': preference(thumbnail_id)
60             } for thumbnail_id, img_url in images.items()]
61
62         return {
63             'id': video_id,
64             'display_id': display_id,
65             'title': info['title'],
66             'description': info.get('summary'),
67             'thumbnails': thumbnails,
68             'uploader': info.get('show', {}).get('name'),
69             'uploader_id': info.get('show', {}).get('slug'),
70             'duration': duration,
71             'formats': formats,
72         }