logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

sonyliv.py (4467B)


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