]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/testtube.py
Imported Upstream version 2015.05.15
[youtubedl] / 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': 'webm',
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 'url': 'https://testtube.com/iflscience/insane-jet-ski-flipping',
27 'info_dict': {
28 'id': 'fAGfJ4YjVus',
29 'ext': 'mp4',
30 'title': 'Flipping Jet-Ski Skills | Outrageous Acts of Science',
31 'uploader': 'Science Channel',
32 'uploader_id': 'ScienceChannel',
33 'upload_date': '20150203',
34 'description': 'md5:e61374030015bae1d2e22f096d4769d6',
35 }
36 }]
37
38 def _real_extract(self, url):
39 display_id = self._match_id(url)
40
41 webpage = self._download_webpage(url, display_id)
42
43 youtube_url = self._html_search_regex(
44 r'<iframe[^>]+src="((?:https?:)?//www.youtube.com/embed/[^"]+)"',
45 webpage, 'youtube iframe', default=None)
46 if youtube_url:
47 return self.url_result(youtube_url, 'Youtube', video_id=display_id)
48
49 video_id = self._search_regex(
50 r"player\.loadRevision3Item\('video_id',\s*([0-9]+)\);",
51 webpage, 'video ID')
52
53 all_info = self._download_json(
54 'https://testtube.com/api/getPlaylist.json?api_key=ba9c741bce1b9d8e3defcc22193f3651b8867e62&codecs=h264,vp8,theora&video_id=%s' % video_id,
55 video_id)
56 info = all_info['items'][0]
57
58 formats = []
59 for vcodec, fdatas in info['media'].items():
60 for name, fdata in fdatas.items():
61 formats.append({
62 'format_id': '%s-%s' % (vcodec, name),
63 'url': fdata['url'],
64 'vcodec': vcodec,
65 'tbr': fdata.get('bitrate'),
66 })
67 self._sort_formats(formats)
68
69 duration = int_or_none(info.get('duration'))
70 images = info.get('images')
71 thumbnails = None
72 preference = qualities(['mini', 'small', 'medium', 'large'])
73 if images:
74 thumbnails = [{
75 'id': thumbnail_id,
76 'url': img_url,
77 'preference': preference(thumbnail_id)
78 } for thumbnail_id, img_url in images.items()]
79
80 return {
81 'id': video_id,
82 'display_id': display_id,
83 'title': info['title'],
84 'description': info.get('summary'),
85 'thumbnails': thumbnails,
86 'uploader': info.get('show', {}).get('name'),
87 'uploader_id': info.get('show', {}).get('slug'),
88 'duration': duration,
89 'formats': formats,
90 }