wsj.py 4.58 KB
Newer Older
Déstin Reed's avatar
Déstin Reed committed
1
# coding: utf-8
2 3 4 5 6
from __future__ import unicode_literals

from .common import InfoExtractor
from ..utils import (
    int_or_none,
remitamine's avatar
remitamine committed
7
    float_or_none,
8 9 10 11 12
    unified_strdate,
)


class WSJIE(InfoExtractor):
John Hawkinson's avatar
John Hawkinson committed
13
    _VALID_URL = r'''(?x)
14 15
                        (?:
                            https?://video-api\.wsj\.com/api-video/player/iframe\.html\?.*?\bguid=|
16
                            https?://(?:www\.)?(?:wsj|barrons)\.com/video/(?:[^/]+/)+|
17 18 19 20
                            wsj:
                        )
                        (?P<id>[a-fA-F0-9-]{36})
                    '''
21
    IE_DESC = 'Wall Street Journal'
remitamine's avatar
remitamine committed
22
    _TESTS = [{
23
        'url': 'http://video-api.wsj.com/api-video/player/iframe.html?guid=1BD01A4C-BFE8-40A5-A42F-8A8AF9898B1A',
remitamine's avatar
remitamine committed
24
        'md5': 'e230a5bb249075e40793b655a54a02e4',
25 26 27 28
        'info_dict': {
            'id': '1BD01A4C-BFE8-40A5-A42F-8A8AF9898B1A',
            'ext': 'mp4',
            'upload_date': '20150202',
Philipp Hagemeister's avatar
Philipp Hagemeister committed
29 30
            'uploader_id': 'jdesai',
            'creator': 'jdesai',
31 32 33 34
            'categories': list,  # a long list
            'duration': 90,
            'title': 'Bills Coach Rex Ryan Updates His Old Jets Tattoo',
        },
remitamine's avatar
remitamine committed
35 36 37
    }, {
        'url': 'http://www.wsj.com/video/can-alphabet-build-a-smarter-city/359DDAA8-9AC1-489C-82E6-0429C1E430E0.html',
        'only_matching': True,
38 39 40
    }, {
        'url': 'http://www.barrons.com/video/capitalism-deserves-more-respect-from-millennials/F301217E-6F46-43AE-B8D2-B7180D642EE9.html',
        'only_matching': True,
41 42 43
    }, {
        'url': 'https://www.wsj.com/video/series/a-brief-history-of/the-modern-cell-carrier-how-we-got-here/980E2187-401D-48A1-B82B-1486CEE06CB9',
        'only_matching': True,
remitamine's avatar
remitamine committed
44
    }]
45 46 47 48

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

49 50 51 52 53 54 55 56 57 58 59
        info = self._download_json(
            'http://video-api.wsj.com/api-video/find_all_videos.asp', video_id,
            query={
                'type': 'guid',
                'count': 1,
                'query': video_id,
                'fields': ','.join((
                    'type', 'hls', 'videoMP4List', 'thumbnailList', 'author',
                    'description', 'name', 'duration', 'videoURL', 'titletag',
                    'formattedCreationDate', 'keywords', 'editor')),
            })['items'][0]
60 61
        title = info.get('name', info.get('titletag'))

remitamine's avatar
remitamine committed
62 63 64 65 66 67 68 69 70
        formats = []

        f4m_url = info.get('videoURL')
        if f4m_url:
            formats.extend(self._extract_f4m_formats(
                f4m_url, video_id, f4m_id='hds', fatal=False))

        m3u8_url = info.get('hls')
        if m3u8_url:
71 72
            formats.extend(self._extract_m3u8_formats(
                info['hls'], video_id, ext='mp4',
remitamine's avatar
remitamine committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
                entry_protocol='m3u8_native', m3u8_id='hls', fatal=False))

        for v in info.get('videoMP4List', []):
            mp4_url = v.get('url')
            if not mp4_url:
                continue
            tbr = int_or_none(v.get('bitrate'))
            formats.append({
                'url': mp4_url,
                'format_id': 'http' + ('-%d' % tbr if tbr else ''),
                'tbr': tbr,
                'width': int_or_none(v.get('width')),
                'height': int_or_none(v.get('height')),
                'fps': float_or_none(v.get('fps')),
            })
88 89 90 91 92
        self._sort_formats(formats)

        return {
            'id': video_id,
            'formats': formats,
remitamine's avatar
remitamine committed
93 94 95 96 97 98 99
            # Thumbnails are conveniently in the correct format already
            'thumbnails': info.get('thumbnailList'),
            'creator': info.get('author'),
            'uploader_id': info.get('editor'),
            'duration': int_or_none(info.get('duration')),
            'upload_date': unified_strdate(info.get(
                'formattedCreationDate'), day_first=False),
100
            'title': title,
remitamine's avatar
remitamine committed
101
            'categories': info.get('keywords'),
102
        }
John Hawkinson's avatar
John Hawkinson committed
103 104 105


class WSJArticleIE(InfoExtractor):
106 107
    _VALID_URL = r'(?i)https?://(?:www\.)?wsj\.com/articles/(?P<id>[^/?#&]+)'
    _TEST = {
John Hawkinson's avatar
John Hawkinson committed
108 109 110 111 112 113 114 115
        'url': 'https://www.wsj.com/articles/dont-like-china-no-pandas-for-you-1490366939?',
        'info_dict': {
            'id': '4B13FA62-1D8C-45DB-8EA1-4105CB20B362',
            'ext': 'mp4',
            'upload_date': '20170221',
            'uploader_id': 'ralcaraz',
            'title': 'Bao Bao the Panda Leaves for China',
        }
116
    }
John Hawkinson's avatar
John Hawkinson committed
117 118 119 120

    def _real_extract(self, url):
        article_id = self._match_id(url)
        webpage = self._download_webpage(url, article_id)
121 122
        video_id = self._search_regex(
            r'data-src=["\']([a-fA-F0-9-]{36})', webpage, 'video id')
John Hawkinson's avatar
John Hawkinson committed
123
        return self.url_result('wsj:%s' % video_id, WSJIE.ie_key(), video_id)