youtube-dl

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

tv2.py (9907B)


      1 # coding: utf-8
      2 from __future__ import unicode_literals
      3 
      4 import re
      5 
      6 from .common import InfoExtractor
      7 from ..compat import compat_HTTPError
      8 from ..utils import (
      9     determine_ext,
     10     ExtractorError,
     11     int_or_none,
     12     float_or_none,
     13     js_to_json,
     14     parse_iso8601,
     15     remove_end,
     16     strip_or_none,
     17     try_get,
     18 )
     19 
     20 
     21 class TV2IE(InfoExtractor):
     22     _VALID_URL = r'https?://(?:www\.)?tv2\.no/v/(?P<id>\d+)'
     23     _TESTS = [{
     24         'url': 'http://www.tv2.no/v/916509/',
     25         'info_dict': {
     26             'id': '916509',
     27             'ext': 'flv',
     28             'title': 'Se Frode Gryttens hyllest av Steven Gerrard',
     29             'description': 'TV 2 Sportens huspoet tar avskjed med Liverpools kaptein Steven Gerrard.',
     30             'timestamp': 1431715610,
     31             'upload_date': '20150515',
     32             'duration': 156.967,
     33             'view_count': int,
     34             'categories': list,
     35         },
     36     }]
     37     _API_DOMAIN = 'sumo.tv2.no'
     38     _PROTOCOLS = ('HDS', 'HLS', 'DASH')
     39     _GEO_COUNTRIES = ['NO']
     40 
     41     def _real_extract(self, url):
     42         video_id = self._match_id(url)
     43         api_base = 'http://%s/api/web/asset/%s' % (self._API_DOMAIN, video_id)
     44 
     45         asset = self._download_json(
     46             api_base + '.json', video_id,
     47             'Downloading metadata JSON')['asset']
     48         title = asset.get('subtitle') or asset['title']
     49         is_live = asset.get('live') is True
     50 
     51         formats = []
     52         format_urls = []
     53         for protocol in self._PROTOCOLS:
     54             try:
     55                 data = self._download_json(
     56                     api_base + '/play.json?protocol=%s&videoFormat=SMIL+ISMUSP' % protocol,
     57                     video_id, 'Downloading play JSON')['playback']
     58             except ExtractorError as e:
     59                 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 401:
     60                     error = self._parse_json(e.cause.read().decode(), video_id)['error']
     61                     error_code = error.get('code')
     62                     if error_code == 'ASSET_PLAYBACK_INVALID_GEO_LOCATION':
     63                         self.raise_geo_restricted(countries=self._GEO_COUNTRIES)
     64                     elif error_code == 'SESSION_NOT_AUTHENTICATED':
     65                         self.raise_login_required()
     66                     raise ExtractorError(error['description'])
     67                 raise
     68             items = try_get(data, lambda x: x['items']['item'])
     69             if not items:
     70                 continue
     71             if not isinstance(items, list):
     72                 items = [items]
     73             for item in items:
     74                 if not isinstance(item, dict):
     75                     continue
     76                 video_url = item.get('url')
     77                 if not video_url or video_url in format_urls:
     78                     continue
     79                 format_id = '%s-%s' % (protocol.lower(), item.get('mediaFormat'))
     80                 if not self._is_valid_url(video_url, video_id, format_id):
     81                     continue
     82                 format_urls.append(video_url)
     83                 ext = determine_ext(video_url)
     84                 if ext == 'f4m':
     85                     formats.extend(self._extract_f4m_formats(
     86                         video_url, video_id, f4m_id=format_id, fatal=False))
     87                 elif ext == 'm3u8':
     88                     if not data.get('drmProtected'):
     89                         formats.extend(self._extract_m3u8_formats(
     90                             video_url, video_id, 'mp4',
     91                             'm3u8' if is_live else 'm3u8_native',
     92                             m3u8_id=format_id, fatal=False))
     93                 elif ext == 'mpd':
     94                     formats.extend(self._extract_mpd_formats(
     95                         video_url, video_id, format_id, fatal=False))
     96                 elif ext == 'ism' or video_url.endswith('.ism/Manifest'):
     97                     pass
     98                 else:
     99                     formats.append({
    100                         'url': video_url,
    101                         'format_id': format_id,
    102                         'tbr': int_or_none(item.get('bitrate')),
    103                         'filesize': int_or_none(item.get('fileSize')),
    104                     })
    105         if not formats and data.get('drmProtected'):
    106             raise ExtractorError('This video is DRM protected.', expected=True)
    107         self._sort_formats(formats)
    108 
    109         thumbnails = [{
    110             'id': thumbnail.get('@type'),
    111             'url': thumbnail.get('url'),
    112         } for _, thumbnail in (asset.get('imageVersions') or {}).items()]
    113 
    114         return {
    115             'id': video_id,
    116             'url': video_url,
    117             'title': self._live_title(title) if is_live else title,
    118             'description': strip_or_none(asset.get('description')),
    119             'thumbnails': thumbnails,
    120             'timestamp': parse_iso8601(asset.get('createTime')),
    121             'duration': float_or_none(asset.get('accurateDuration') or asset.get('duration')),
    122             'view_count': int_or_none(asset.get('views')),
    123             'categories': asset.get('keywords', '').split(','),
    124             'formats': formats,
    125             'is_live': is_live,
    126         }
    127 
    128 
    129 class TV2ArticleIE(InfoExtractor):
    130     _VALID_URL = r'https?://(?:www\.)?tv2\.no/(?:a|\d{4}/\d{2}/\d{2}(/[^/]+)+)/(?P<id>\d+)'
    131     _TESTS = [{
    132         'url': 'http://www.tv2.no/2015/05/16/nyheter/alesund/krim/pingvin/6930542',
    133         'info_dict': {
    134             'id': '6930542',
    135             'title': 'Russen hetses etter pingvintyveri - innrømmer å ha åpnet luken på buret',
    136             'description': 'De fire siktede nekter fortsatt for å ha stjålet pingvinbabyene, men innrømmer å ha åpnet luken til de små kyllingene.',
    137         },
    138         'playlist_count': 2,
    139     }, {
    140         'url': 'http://www.tv2.no/a/6930542',
    141         'only_matching': True,
    142     }]
    143 
    144     def _real_extract(self, url):
    145         playlist_id = self._match_id(url)
    146 
    147         webpage = self._download_webpage(url, playlist_id)
    148 
    149         # Old embed pattern (looks unused nowadays)
    150         assets = re.findall(r'data-assetid=["\'](\d+)', webpage)
    151 
    152         if not assets:
    153             # New embed pattern
    154             for v in re.findall(r'(?s)TV2ContentboxVideo\(({.+?})\)', webpage):
    155                 video = self._parse_json(
    156                     v, playlist_id, transform_source=js_to_json, fatal=False)
    157                 if not video:
    158                     continue
    159                 asset = video.get('assetId')
    160                 if asset:
    161                     assets.append(asset)
    162 
    163         entries = [
    164             self.url_result('http://www.tv2.no/v/%s' % asset_id, 'TV2')
    165             for asset_id in assets]
    166 
    167         title = remove_end(self._og_search_title(webpage), ' - TV2.no')
    168         description = remove_end(self._og_search_description(webpage), ' - TV2.no')
    169 
    170         return self.playlist_result(entries, playlist_id, title, description)
    171 
    172 
    173 class KatsomoIE(TV2IE):
    174     _VALID_URL = r'https?://(?:www\.)?(?:katsomo|mtv(uutiset)?)\.fi/(?:sarja/[0-9a-z-]+-\d+/[0-9a-z-]+-|(?:#!/)?jakso/(?:\d+/[^/]+/)?|video/prog)(?P<id>\d+)'
    175     _TESTS = [{
    176         'url': 'https://www.mtv.fi/sarja/mtv-uutiset-live-33001002003/lahden-pelicans-teki-kovan-ratkaisun-ville-nieminen-pihalle-1181321',
    177         'info_dict': {
    178             'id': '1181321',
    179             'ext': 'mp4',
    180             'title': 'Lahden Pelicans teki kovan ratkaisun – Ville Nieminen pihalle',
    181             'description': 'Päätöksen teki Pelicansin hallitus.',
    182             'timestamp': 1575116484,
    183             'upload_date': '20191130',
    184             'duration': 37.12,
    185             'view_count': int,
    186             'categories': list,
    187         },
    188         'params': {
    189             # m3u8 download
    190             'skip_download': True,
    191         },
    192     }, {
    193         'url': 'http://www.katsomo.fi/#!/jakso/33001005/studio55-fi/658521/jukka-kuoppamaki-tekee-yha-lauluja-vaikka-lentokoneessa',
    194         'only_matching': True,
    195     }, {
    196         'url': 'https://www.mtvuutiset.fi/video/prog1311159',
    197         'only_matching': True,
    198     }, {
    199         'url': 'https://www.katsomo.fi/#!/jakso/1311159',
    200         'only_matching': True,
    201     }]
    202     _API_DOMAIN = 'api.katsomo.fi'
    203     _PROTOCOLS = ('HLS', 'MPD')
    204     _GEO_COUNTRIES = ['FI']
    205 
    206 
    207 class MTVUutisetArticleIE(InfoExtractor):
    208     _VALID_URL = r'https?://(?:www\.)mtvuutiset\.fi/artikkeli/[^/]+/(?P<id>\d+)'
    209     _TESTS = [{
    210         'url': 'https://www.mtvuutiset.fi/artikkeli/tallaisia-vaurioita-viking-amorellassa-on-useamman-osaston-alla-vetta/7931384',
    211         'info_dict': {
    212             'id': '1311159',
    213             'ext': 'mp4',
    214             'title': 'Viking Amorellan matkustajien evakuointi on alkanut – tältä operaatio näyttää laivalla',
    215             'description': 'Viking Amorellan matkustajien evakuointi on alkanut – tältä operaatio näyttää laivalla',
    216             'timestamp': 1600608966,
    217             'upload_date': '20200920',
    218             'duration': 153.7886666,
    219             'view_count': int,
    220             'categories': list,
    221         },
    222         'params': {
    223             # m3u8 download
    224             'skip_download': True,
    225         },
    226     }, {
    227         # multiple Youtube embeds
    228         'url': 'https://www.mtvuutiset.fi/artikkeli/50-vuotta-subarun-vastaiskua/6070962',
    229         'only_matching': True,
    230     }]
    231 
    232     def _real_extract(self, url):
    233         article_id = self._match_id(url)
    234         article = self._download_json(
    235             'http://api.mtvuutiset.fi/mtvuutiset/api/json/' + article_id,
    236             article_id)
    237 
    238         def entries():
    239             for video in (article.get('videos') or []):
    240                 video_type = video.get('videotype')
    241                 video_url = video.get('url')
    242                 if not (video_url and video_type in ('katsomo', 'youtube')):
    243                     continue
    244                 yield self.url_result(
    245                     video_url, video_type.capitalize(), video.get('video_id'))
    246 
    247         return self.playlist_result(
    248             entries(), article_id, article.get('title'), article.get('description'))