You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
5.1 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import json
  5. from .common import InfoExtractor
  6. from ..compat import compat_HTTPError
  7. from ..utils import (
  8. ExtractorError,
  9. int_or_none,
  10. parse_iso8601,
  11. str_or_none,
  12. urlencode_postdata,
  13. clean_html,
  14. )
  15. class ShahidIE(InfoExtractor):
  16. _NETRC_MACHINE = 'shahid'
  17. _VALID_URL = r'https?://shahid\.mbc\.net/ar/(?:serie|show|movie)s/[^/]+/(?P<type>episode|clip|movie)-(?P<id>\d+)'
  18. _TESTS = [{
  19. 'url': 'https://shahid.mbc.net/ar/shows/%D9%85%D8%AC%D9%84%D8%B3-%D8%A7%D9%84%D8%B4%D8%A8%D8%A7%D8%A8-%D8%A7%D9%84%D9%85%D9%88%D8%B3%D9%85-1-%D9%83%D9%84%D9%8A%D8%A8-1/clip-275286',
  20. 'info_dict': {
  21. 'id': '275286',
  22. 'ext': 'mp4',
  23. 'title': 'مجلس الشباب الموسم 1 كليب 1',
  24. 'timestamp': 1506988800,
  25. 'upload_date': '20171003',
  26. },
  27. 'params': {
  28. # m3u8 download
  29. 'skip_download': True,
  30. }
  31. }, {
  32. 'url': 'https://shahid.mbc.net/ar/movies/%D8%A7%D9%84%D9%82%D9%86%D8%A7%D8%B5%D8%A9/movie-151746',
  33. 'only_matching': True
  34. }, {
  35. # shahid plus subscriber only
  36. 'url': 'https://shahid.mbc.net/ar/series/%D9%85%D8%B1%D8%A7%D9%8A%D8%A7-2011-%D8%A7%D9%84%D9%85%D9%88%D8%B3%D9%85-1-%D8%A7%D9%84%D8%AD%D9%84%D9%82%D8%A9-1/episode-90511',
  37. 'only_matching': True
  38. }]
  39. def _api2_request(self, *args, **kwargs):
  40. try:
  41. return self._download_json(*args, **kwargs)
  42. except ExtractorError as e:
  43. if isinstance(e.cause, compat_HTTPError):
  44. fail_data = self._parse_json(
  45. e.cause.read().decode('utf-8'), None, fatal=False)
  46. if fail_data:
  47. faults = fail_data.get('faults', [])
  48. faults_message = ', '.join([clean_html(fault['userMessage']) for fault in faults if fault.get('userMessage')])
  49. if faults_message:
  50. raise ExtractorError(faults_message, expected=True)
  51. raise
  52. def _real_initialize(self):
  53. email, password = self._get_login_info()
  54. if email is None:
  55. return
  56. user_data = self._api2_request(
  57. 'https://shahid.mbc.net/wd/service/users/login',
  58. None, 'Logging in', data=json.dumps({
  59. 'email': email,
  60. 'password': password,
  61. 'basic': 'false',
  62. }).encode('utf-8'), headers={
  63. 'Content-Type': 'application/json; charset=UTF-8',
  64. })['user']
  65. self._download_webpage(
  66. 'https://shahid.mbc.net/populateContext',
  67. None, 'Populate Context', data=urlencode_postdata({
  68. 'firstName': user_data['firstName'],
  69. 'lastName': user_data['lastName'],
  70. 'userName': user_data['email'],
  71. 'csg_user_name': user_data['email'],
  72. 'subscriberId': user_data['id'],
  73. 'sessionId': user_data['sessionId'],
  74. }))
  75. def _get_api_data(self, response):
  76. data = response.get('data', {})
  77. error = data.get('error')
  78. if error:
  79. raise ExtractorError(
  80. '%s returned error: %s' % (self.IE_NAME, '\n'.join(error.values())),
  81. expected=True)
  82. return data
  83. def _real_extract(self, url):
  84. page_type, video_id = re.match(self._VALID_URL, url).groups()
  85. if page_type == 'clip':
  86. page_type = 'episode'
  87. playout = self._api2_request(
  88. 'https://api2.shahid.net/proxy/v2/playout/url/' + video_id,
  89. video_id, 'Downloading player JSON')['playout']
  90. if playout.get('drm'):
  91. raise ExtractorError('This video is DRM protected.', expected=True)
  92. formats = self._extract_m3u8_formats(playout['url'], video_id, 'mp4')
  93. self._sort_formats(formats)
  94. video = self._get_api_data(self._download_json(
  95. 'http://api.shahid.net/api/v1_1/%s/%s' % (page_type, video_id),
  96. video_id, 'Downloading video JSON', query={
  97. 'apiKey': 'sh@hid0nlin3',
  98. 'hash': 'b2wMCTHpSmyxGqQjJFOycRmLSex+BpTK/ooxy6vHaqs=',
  99. }))[page_type]
  100. title = video['title']
  101. categories = [
  102. category['name']
  103. for category in video.get('genres', []) if 'name' in category]
  104. return {
  105. 'id': video_id,
  106. 'title': title,
  107. 'description': video.get('description'),
  108. 'thumbnail': video.get('thumbnailUrl'),
  109. 'duration': int_or_none(video.get('duration')),
  110. 'timestamp': parse_iso8601(video.get('referenceDate')),
  111. 'categories': categories,
  112. 'series': video.get('showTitle') or video.get('showName'),
  113. 'season': video.get('seasonTitle'),
  114. 'season_number': int_or_none(video.get('seasonNumber')),
  115. 'season_id': str_or_none(video.get('seasonId')),
  116. 'episode_number': int_or_none(video.get('number')),
  117. 'episode_id': video_id,
  118. 'formats': formats,
  119. }