youtube-dl

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

sonyliv.py (4467B)


      1 # coding: utf-8
      2 from __future__ import unicode_literals
      3 
      4 import time
      5 import uuid
      6 
      7 from .common import InfoExtractor
      8 from ..compat import compat_HTTPError
      9 from ..utils import (
     10     ExtractorError,
     11     int_or_none,
     12 )
     13 
     14 
     15 class SonyLIVIE(InfoExtractor):
     16     _VALID_URL = r'https?://(?:www\.)?sonyliv\.com/(?:s(?:how|port)s/[^/]+|movies|clip|trailer|music-videos)/[^/?#&]+-(?P<id>\d+)'
     17     _TESTS = [{
     18         'url': 'https://www.sonyliv.com/shows/bachelors-delight-1700000113/achaari-cheese-toast-1000022678?watch=true',
     19         'info_dict': {
     20             'title': 'Bachelors Delight - Achaari Cheese Toast',
     21             'id': '1000022678',
     22             'ext': 'mp4',
     23             'upload_date': '20200411',
     24             'description': 'md5:3957fa31d9309bf336ceb3f37ad5b7cb',
     25             'timestamp': 1586632091,
     26             'duration': 185,
     27             'season_number': 1,
     28             'episode': 'Achaari Cheese Toast',
     29             'episode_number': 1,
     30             'release_year': 2016,
     31         },
     32         'params': {
     33             'skip_download': True,
     34         },
     35     }, {
     36         'url': 'https://www.sonyliv.com/movies/tahalka-1000050121?watch=true',
     37         'only_matching': True,
     38     }, {
     39         'url': 'https://www.sonyliv.com/clip/jigarbaaz-1000098925',
     40         'only_matching': True,
     41     }, {
     42         'url': 'https://www.sonyliv.com/trailer/sandwiched-forever-1000100286?watch=true',
     43         'only_matching': True,
     44     }, {
     45         'url': 'https://www.sonyliv.com/sports/india-tour-of-australia-2020-21-1700000286/cricket-hls-day-3-1st-test-aus-vs-ind-19-dec-2020-1000100959?watch=true',
     46         'only_matching': True,
     47     }, {
     48         'url': 'https://www.sonyliv.com/music-videos/yeh-un-dinon-ki-baat-hai-1000018779',
     49         'only_matching': True,
     50     }]
     51     _GEO_COUNTRIES = ['IN']
     52     _TOKEN = None
     53 
     54     def _call_api(self, version, path, video_id):
     55         headers = {}
     56         if self._TOKEN:
     57             headers['security_token'] = self._TOKEN
     58         try:
     59             return self._download_json(
     60                 'https://apiv2.sonyliv.com/AGL/%s/A/ENG/WEB/%s' % (version, path),
     61                 video_id, headers=headers)['resultObj']
     62         except ExtractorError as e:
     63             if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
     64                 message = self._parse_json(
     65                     e.cause.read().decode(), video_id)['message']
     66                 if message == 'Geoblocked Country':
     67                     self.raise_geo_restricted(countries=self._GEO_COUNTRIES)
     68                 raise ExtractorError(message)
     69             raise
     70 
     71     def _real_initialize(self):
     72         self._TOKEN = self._call_api('1.4', 'ALL/GETTOKEN', None)
     73 
     74     def _real_extract(self, url):
     75         video_id = self._match_id(url)
     76         content = self._call_api(
     77             '1.5', 'IN/CONTENT/VIDEOURL/VOD/' + video_id, video_id)
     78         if content.get('isEncrypted'):
     79             raise ExtractorError('This video is DRM protected.', expected=True)
     80         dash_url = content['videoURL']
     81         headers = {
     82             'x-playback-session-id': '%s-%d' % (uuid.uuid4().hex, time.time() * 1000)
     83         }
     84         formats = self._extract_mpd_formats(
     85             dash_url, video_id, mpd_id='dash', headers=headers, fatal=False)
     86         formats.extend(self._extract_m3u8_formats(
     87             dash_url.replace('.mpd', '.m3u8').replace('/DASH/', '/HLS/'),
     88             video_id, 'mp4', m3u8_id='hls', headers=headers, fatal=False))
     89         for f in formats:
     90             f.setdefault('http_headers', {}).update(headers)
     91         self._sort_formats(formats)
     92 
     93         metadata = self._call_api(
     94             '1.6', 'IN/DETAIL/' + video_id, video_id)['containers'][0]['metadata']
     95         title = metadata['title']
     96         episode = metadata.get('episodeTitle')
     97         if episode and title != episode:
     98             title += ' - ' + episode
     99 
    100         return {
    101             'id': video_id,
    102             'title': title,
    103             'formats': formats,
    104             'thumbnail': content.get('posterURL'),
    105             'description': metadata.get('longDescription') or metadata.get('shortDescription'),
    106             'timestamp': int_or_none(metadata.get('creationDate'), 1000),
    107             'duration': int_or_none(metadata.get('duration')),
    108             'season_number': int_or_none(metadata.get('season')),
    109             'episode': episode,
    110             'episode_number': int_or_none(metadata.get('episodeNumber')),
    111             'release_year': int_or_none(metadata.get('year')),
    112         }