aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl/extractor/testtube.py
blob: 26655d690250f495caf98de2cfaad6aff3eda331 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from __future__ import unicode_literals

from .common import InfoExtractor
from ..utils import (
    int_or_none,
    qualities,
)


class TestTubeIE(InfoExtractor):
    _VALID_URL = r'https?://testtube\.com/[^/?#]+/(?P<id>[^/?#]+)'
    _TESTS = [{
        'url': 'https://testtube.com/dnews/5-weird-ways-plants-can-eat-animals?utm_source=FB&utm_medium=DNews&utm_campaign=DNewsSocial',
        'info_dict': {
            'id': '60163',
            'display_id': '5-weird-ways-plants-can-eat-animals',
            'duration': 275,
            'ext': 'webm',
            'title': '5 Weird Ways Plants Can Eat Animals',
            'description': 'Why have some plants evolved to eat meat?',
            'thumbnail': 're:^https?://.*\.jpg$',
            'uploader': 'DNews',
            'uploader_id': 'dnews',
        },
    }, {
        'url': 'https://testtube.com/iflscience/insane-jet-ski-flipping',
        'info_dict': {
            'id': 'fAGfJ4YjVus',
            'ext': 'mp4',
            'title': 'Flipping Jet-Ski Skills | Outrageous Acts of Science',
            'uploader': 'Science Channel',
            'uploader_id': 'ScienceChannel',
            'upload_date': '20150203',
            'description': 'md5:e61374030015bae1d2e22f096d4769d6',
        }
    }]

    def _real_extract(self, url):
        display_id = self._match_id(url)

        webpage = self._download_webpage(url, display_id)

        youtube_url = self._html_search_regex(
            r'<iframe[^>]+src="((?:https?:)?//www.youtube.com/embed/[^"]+)"',
            webpage, 'youtube iframe', default=None)
        if youtube_url:
            return self.url_result(youtube_url, 'Youtube', video_id=display_id)

        video_id = self._search_regex(
            r"player\.loadRevision3Item\('video_id',\s*([0-9]+)\);",
            webpage, 'video ID')

        all_info = self._download_json(
            'https://testtube.com/api/getPlaylist.json?api_key=ba9c741bce1b9d8e3defcc22193f3651b8867e62&codecs=h264,vp8,theora&video_id=%s' % video_id,
            video_id)
        info = all_info['items'][0]

        formats = []
        for vcodec, fdatas in info['media'].items():
            for name, fdata in fdatas.items():
                formats.append({
                    'format_id': '%s-%s' % (vcodec, name),
                    'url': fdata['url'],
                    'vcodec': vcodec,
                    'tbr': fdata.get('bitrate'),
                })
        self._sort_formats(formats)

        duration = int_or_none(info.get('duration'))
        images = info.get('images')
        thumbnails = None
        preference = qualities(['mini', 'small', 'medium', 'large'])
        if images:
            thumbnails = [{
                'id': thumbnail_id,
                'url': img_url,
                'preference': preference(thumbnail_id)
            } for thumbnail_id, img_url in images.items()]

        return {
            'id': video_id,
            'display_id': display_id,
            'title': info['title'],
            'description': info.get('summary'),
            'thumbnails': thumbnails,
            'uploader': info.get('show', {}).get('name'),
            'uploader_id': info.get('show', {}).get('slug'),
            'duration': duration,
            'formats': formats,
        }