youtube-dl

Another place where youtube-dl lives on
git clone git://git.oshgnacknak.de/youtube-dl.git
Log | Files | Refs | README | LICENSE

voxmedia.py (9844B)


      1 # coding: utf-8
      2 from __future__ import unicode_literals
      3 
      4 from .common import InfoExtractor
      5 from .once import OnceIE
      6 from ..compat import compat_urllib_parse_unquote
      7 from ..utils import (
      8     ExtractorError,
      9     int_or_none,
     10     try_get,
     11     unified_timestamp,
     12 )
     13 
     14 
     15 class VoxMediaVolumeIE(OnceIE):
     16     _VALID_URL = r'https?://volume\.vox-cdn\.com/embed/(?P<id>[0-9a-f]{9})'
     17 
     18     def _real_extract(self, url):
     19         video_id = self._match_id(url)
     20         webpage = self._download_webpage(url, video_id)
     21 
     22         setup = self._parse_json(self._search_regex(
     23             r'setup\s*=\s*({.+});', webpage, 'setup'), video_id)
     24         player_setup = setup.get('player_setup') or setup
     25         video_data = player_setup.get('video') or {}
     26         formatted_metadata = video_data.get('formatted_metadata') or {}
     27         info = {
     28             'id': video_id,
     29             'title': player_setup.get('title') or video_data.get('title_short'),
     30             'description': video_data.get('description_long') or video_data.get('description_short'),
     31             'thumbnail': formatted_metadata.get('thumbnail') or video_data.get('brightcove_thumbnail'),
     32             'timestamp': unified_timestamp(formatted_metadata.get('video_publish_date')),
     33         }
     34         asset = try_get(setup, lambda x: x['embed_assets']['chorus'], dict) or {}
     35 
     36         formats = []
     37         hls_url = asset.get('hls_url')
     38         if hls_url:
     39             formats.extend(self._extract_m3u8_formats(
     40                 hls_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False))
     41         mp4_url = asset.get('mp4_url')
     42         if mp4_url:
     43             tbr = self._search_regex(r'-(\d+)k\.', mp4_url, 'bitrate', default=None)
     44             format_id = 'http'
     45             if tbr:
     46                 format_id += '-' + tbr
     47             formats.append({
     48                 'format_id': format_id,
     49                 'url': mp4_url,
     50                 'tbr': int_or_none(tbr),
     51             })
     52         if formats:
     53             self._sort_formats(formats)
     54             info['formats'] = formats
     55             info['duration'] = int_or_none(asset.get('duration'))
     56             return info
     57 
     58         for provider_video_type in ('ooyala', 'youtube', 'brightcove'):
     59             provider_video_id = video_data.get('%s_id' % provider_video_type)
     60             if not provider_video_id:
     61                 continue
     62             if provider_video_type == 'brightcove':
     63                 info['formats'] = self._extract_once_formats(provider_video_id)
     64                 self._sort_formats(info['formats'])
     65             else:
     66                 info.update({
     67                     '_type': 'url_transparent',
     68                     'url': provider_video_id if provider_video_type == 'youtube' else '%s:%s' % (provider_video_type, provider_video_id),
     69                     'ie_key': provider_video_type.capitalize(),
     70                 })
     71             return info
     72         raise ExtractorError('Unable to find provider video id')
     73 
     74 
     75 class VoxMediaIE(InfoExtractor):
     76     _VALID_URL = r'https?://(?:www\.)?(?:(?:theverge|vox|sbnation|eater|polygon|curbed|racked|funnyordie)\.com|recode\.net)/(?:[^/]+/)*(?P<id>[^/?]+)'
     77     _TESTS = [{
     78         # Volume embed, Youtube
     79         'url': 'http://www.theverge.com/2014/6/27/5849272/material-world-how-google-discovered-what-software-is-made-of',
     80         'info_dict': {
     81             'id': 'j4mLW6x17VM',
     82             'ext': 'mp4',
     83             'title': 'Material world: how Google discovered what software is made of',
     84             'description': 'md5:dfc17e7715e3b542d66e33a109861382',
     85             'upload_date': '20190710',
     86             'uploader_id': 'TheVerge',
     87             'uploader': 'The Verge',
     88         },
     89         'add_ie': ['Youtube'],
     90     }, {
     91         # Volume embed, Youtube
     92         'url': 'http://www.theverge.com/2014/10/21/7025853/google-nexus-6-hands-on-photos-video-android-phablet',
     93         'md5': 'fd19aa0cf3a0eea515d4fd5c8c0e9d68',
     94         'info_dict': {
     95             'id': 'Gy8Md3Eky38',
     96             'ext': 'mp4',
     97             'title': 'The Nexus 6: hands-on with Google\'s phablet',
     98             'description': 'md5:d9f0216e5fb932dd2033d6db37ac3f1d',
     99             'uploader_id': 'TheVerge',
    100             'upload_date': '20141021',
    101             'uploader': 'The Verge',
    102             'timestamp': 1413907200,
    103         },
    104         'add_ie': ['Youtube'],
    105         'skip': 'similar to the previous test',
    106     }, {
    107         # Volume embed, Youtube
    108         'url': 'http://www.vox.com/2016/3/31/11336640/mississippi-lgbt-religious-freedom-bill',
    109         'info_dict': {
    110             'id': '22986359b',
    111             'ext': 'mp4',
    112             'title': "Mississippi's laws are so bad that its anti-LGBTQ law isn't needed to allow discrimination",
    113             'description': 'md5:fc1317922057de31cd74bce91eb1c66c',
    114             'upload_date': '20150915',
    115             'timestamp': 1442332800,
    116             'duration': 285,
    117         },
    118         'add_ie': ['Youtube'],
    119         'skip': 'similar to the previous test',
    120     }, {
    121         # youtube embed
    122         'url': 'http://www.vox.com/2016/3/24/11291692/robot-dance',
    123         'md5': '83b3080489fb103941e549352d3e0977',
    124         'info_dict': {
    125             'id': 'FcNHTJU1ufM',
    126             'ext': 'mp4',
    127             'title': 'How "the robot" became the greatest novelty dance of all time',
    128             'description': 'md5:b081c0d588b8b2085870cda55e6da176',
    129             'upload_date': '20160324',
    130             'uploader_id': 'voxdotcom',
    131             'uploader': 'Vox',
    132         },
    133         'add_ie': ['Youtube'],
    134         'skip': 'Page no longer contain videos',
    135     }, {
    136         # SBN.VideoLinkset.entryGroup multiple ooyala embeds
    137         'url': 'http://www.sbnation.com/college-football-recruiting/2015/2/3/7970291/national-signing-day-rationalizations-itll-be-ok-itll-be-ok',
    138         'info_dict': {
    139             'id': 'national-signing-day-rationalizations-itll-be-ok-itll-be-ok',
    140             'title': '25 lies you will tell yourself on National Signing Day',
    141             'description': 'It\'s the most self-delusional time of the year, and everyone\'s gonna tell the same lies together!',
    142         },
    143         'playlist': [{
    144             'md5': '721fededf2ab74ae4176c8c8cbfe092e',
    145             'info_dict': {
    146                 'id': 'p3cThlMjE61VDi_SD9JlIteSNPWVDBB9',
    147                 'ext': 'mp4',
    148                 'title': 'Buddy Hield vs Steph Curry (and the world)',
    149                 'description': 'Let’s dissect only the most important Final Four storylines.',
    150             },
    151         }, {
    152             'md5': 'bf0c5cc115636af028be1bab79217ea9',
    153             'info_dict': {
    154                 'id': 'BmbmVjMjE6esPHxdALGubTrouQ0jYLHj',
    155                 'ext': 'mp4',
    156                 'title': 'Chasing Cinderella 2016: Syracuse basketball',
    157                 'description': 'md5:e02d56b026d51aa32c010676765a690d',
    158             },
    159         }],
    160         'skip': 'Page no longer contain videos',
    161     }, {
    162         # volume embed, Brightcove Once
    163         'url': 'https://www.recode.net/2014/6/17/11628066/post-post-pc-ceo-the-full-code-conference-video-of-microsofts-satya',
    164         'md5': '2dbc77b8b0bff1894c2fce16eded637d',
    165         'info_dict': {
    166             'id': '1231c973d',
    167             'ext': 'mp4',
    168             'title': 'Post-Post-PC CEO: The Full Code Conference Video of Microsoft\'s Satya Nadella',
    169             'description': 'The longtime veteran was chosen earlier this year as the software giant\'s third leader in its history.',
    170             'timestamp': 1402938000,
    171             'upload_date': '20140616',
    172             'duration': 4114,
    173         },
    174         'add_ie': ['VoxMediaVolume'],
    175     }]
    176 
    177     def _real_extract(self, url):
    178         display_id = self._match_id(url)
    179         webpage = compat_urllib_parse_unquote(self._download_webpage(url, display_id))
    180 
    181         def create_entry(provider_video_id, provider_video_type, title=None, description=None):
    182             video_url = {
    183                 'youtube': '%s',
    184                 'ooyala': 'ooyala:%s',
    185                 'volume': 'http://volume.vox-cdn.com/embed/%s',
    186             }[provider_video_type] % provider_video_id
    187             return {
    188                 '_type': 'url_transparent',
    189                 'url': video_url,
    190                 'title': title or self._og_search_title(webpage),
    191                 'description': description or self._og_search_description(webpage),
    192             }
    193 
    194         entries = []
    195         entries_data = self._search_regex([
    196             r'Chorus\.VideoContext\.addVideo\((\[{.+}\])\);',
    197             r'var\s+entry\s*=\s*({.+});',
    198             r'SBN\.VideoLinkset\.entryGroup\(\s*(\[.+\])',
    199         ], webpage, 'video data', default=None)
    200         if entries_data:
    201             entries_data = self._parse_json(entries_data, display_id)
    202             if isinstance(entries_data, dict):
    203                 entries_data = [entries_data]
    204             for video_data in entries_data:
    205                 provider_video_id = video_data.get('provider_video_id')
    206                 provider_video_type = video_data.get('provider_video_type')
    207                 if provider_video_id and provider_video_type:
    208                     entries.append(create_entry(
    209                         provider_video_id, provider_video_type,
    210                         video_data.get('title'), video_data.get('description')))
    211 
    212         provider_video_id = self._search_regex(
    213             r'data-ooyala-id="([^"]+)"', webpage, 'ooyala id', default=None)
    214         if provider_video_id:
    215             entries.append(create_entry(provider_video_id, 'ooyala'))
    216 
    217         volume_uuid = self._search_regex(
    218             r'data-volume-uuid="([^"]+)"', webpage, 'volume uuid', default=None)
    219         if volume_uuid:
    220             entries.append(create_entry(volume_uuid, 'volume'))
    221 
    222         if len(entries) == 1:
    223             return entries[0]
    224         else:
    225             return self.playlist_result(entries, display_id, self._og_search_title(webpage), self._og_search_description(webpage))