aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl/extractor/nowness.py
blob: 401d3b64616f1d4d6478f3e88203d5a902e428b4 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# encoding: utf-8
from __future__ import unicode_literals

from .brightcove import BrightcoveIE
from .common import InfoExtractor
from ..utils import ExtractorError
from ..compat import compat_urllib_request


class NownessBaseIE(InfoExtractor):
    def extract_url_result(self, post):
        if post['type'] == 'video':
            for media in post['media']:
                if media['type'] == 'video':
                    video_id = media['content']
                    source = media['source']
                    if source == 'brightcove':
                        player_code = self._download_webpage(
                            'http://www.nowness.com/iframe?id=%s' % video_id, video_id,
                            note='Downloading player JavaScript',
                            errnote='Player download failed')
                        bc_url = BrightcoveIE._extract_brightcove_url(player_code)
                        if bc_url is None:
                            raise ExtractorError('Could not find player definition')
                        return self.url_result(bc_url, 'Brightcove')
                    elif source == 'vimeo':
                        return self.url_result('http://vimeo.com/%s' % video_id, 'Vimeo')
                    elif source == 'youtube':
                        return self.url_result(video_id, 'Youtube')
                    elif source == 'cinematique':
                        return self.url_result('http://cinematique.com/embed/%s' % video_id, 'Cinematique')


class NownessIE(NownessBaseIE):
    IE_NAME = 'nowness'
    _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/(story|series/[^/]+)/(?P<id>[0-9a-z-]+)'
    _TESTS = [
        {
            'url': 'https://www.nowness.com/story/candor-the-art-of-gesticulation',
            'md5': '068bc0202558c2e391924cb8cc470676',
            'info_dict': {
                'id': '2520295746001',
                'ext': 'mp4',
                'title': 'Candor: The Art of Gesticulation',
                'description': 'Candor: The Art of Gesticulation',
                'thumbnail': 're:^https?://.*\.jpg',
                'uploader': 'Nowness',
            }
        },
        {
            'url': 'https://cn.nowness.com/story/kasper-bjorke-ft-jaakko-eino-kalevi-tnr',
            'md5': 'e79cf125e387216f86b2e0a5b5c63aa3',
            'info_dict': {
                'id': '3716354522001',
                'ext': 'mp4',
                'title': 'Kasper Bjørke ft. Jaakko Eino Kalevi: TNR',
                'description': 'Kasper Bjørke ft. Jaakko Eino Kalevi: TNR',
                'thumbnail': 're:^https?://.*\.jpg',
                'uploader': 'Nowness',
            }
        },
    ]

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

        lang = 'zh-cn' if 'cn.nowness.com' in url else 'en-us'
        request = compat_urllib_request.Request('http://api.nowness.com/api/post/getBySlug/%s' % display_id, headers={
            'X-Nowness-Language': lang,
        })
        post = self._download_json(request, display_id)
        return self.extract_url_result(post)


class NownessPlaylistIE(NownessBaseIE):
    IE_NAME = 'nowness:playlist'
    _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/playlist/(?P<id>\d+)/[0-9a-z-]+'
    _TEST = {
        'url': 'https://www.nowness.com/playlist/3286/i-guess-thats-why-they-call-it-the-blues',
        'info_dict':
        {
            'id': '3286',
        },
        'playlist_mincount': 8,
    }

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

        lang = 'zh-cn' if 'cn.nowness.com' in url else 'en-us'
        request = compat_urllib_request.Request('http://api.nowness.com/api/post?PlaylistId=%s' % playlist_id, headers={
            'X-Nowness-Language': lang,
        })
        playlist = self._download_json(request, playlist_id)
        entries = [self.extract_url_result(item) for item in playlist['items']]
        return self.playlist_result(entries, playlist_id)


class NownessSerieIE(NownessBaseIE):
    IE_NAME = 'nowness:serie'
    _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/series/(?P<id>[0-9a-z-]+)'
    _TEST = {
        'url': 'https://www.nowness.com/series/60-seconds',
        'info_dict':
        {
            'id': '60',
        },
        'playlist_mincount': 4,
    }

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

        lang = 'zh-cn' if 'cn.nowness.com' in url else 'en-us'
        request = compat_urllib_request.Request('http://api.nowness.com/api/series/getBySlug/%s' % display_id, headers={
            'X-Nowness-Language': lang,
        })
        serie = self._download_json(request, display_id)
        serie_id = str(serie['id'])
        entries = [self.extract_url_result(post) for post in serie['posts']]
        return self.playlist_result(entries, serie_id)