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.

215 lines
7.8 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import json
  4. import math
  5. import re
  6. from .aws import AWSIE
  7. from ..compat import compat_HTTPError
  8. from ..utils import (
  9. clean_html,
  10. ExtractorError,
  11. InAdvancePagedList,
  12. int_or_none,
  13. parse_iso8601,
  14. str_or_none,
  15. urlencode_postdata,
  16. )
  17. class ShahidBaseIE(AWSIE):
  18. _AWS_PROXY_HOST = 'api2.shahid.net'
  19. _AWS_API_KEY = '2RRtuMHx95aNI1Kvtn2rChEuwsCogUd4samGPjLh'
  20. def _handle_error(self, e):
  21. fail_data = self._parse_json(
  22. e.cause.read().decode('utf-8'), None, fatal=False)
  23. if fail_data:
  24. faults = fail_data.get('faults', [])
  25. faults_message = ', '.join([clean_html(fault['userMessage']) for fault in faults if fault.get('userMessage')])
  26. if faults_message:
  27. raise ExtractorError(faults_message, expected=True)
  28. def _call_api(self, path, video_id, request=None):
  29. query = {}
  30. if request:
  31. query['request'] = json.dumps(request)
  32. try:
  33. return self._aws_execute_api({
  34. 'uri': '/proxy/v2/' + path,
  35. 'access_key': 'AKIAI6X4TYCIXM2B7MUQ',
  36. 'secret_key': '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',
  37. }, video_id, query)
  38. except ExtractorError as e:
  39. if isinstance(e.cause, compat_HTTPError):
  40. self._handle_error(e)
  41. raise
  42. class ShahidIE(ShahidBaseIE):
  43. _NETRC_MACHINE = 'shahid'
  44. _VALID_URL = r'https?://shahid\.mbc\.net/ar/(?:serie|show|movie)s/[^/]+/(?P<type>episode|clip|movie)-(?P<id>\d+)'
  45. _TESTS = [{
  46. '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',
  47. 'info_dict': {
  48. 'id': '275286',
  49. 'ext': 'mp4',
  50. 'title': 'مجلس الشباب الموسم 1 كليب 1',
  51. 'timestamp': 1506988800,
  52. 'upload_date': '20171003',
  53. },
  54. 'params': {
  55. # m3u8 download
  56. 'skip_download': True,
  57. }
  58. }, {
  59. 'url': 'https://shahid.mbc.net/ar/movies/%D8%A7%D9%84%D9%82%D9%86%D8%A7%D8%B5%D8%A9/movie-151746',
  60. 'only_matching': True
  61. }, {
  62. # shahid plus subscriber only
  63. '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',
  64. 'only_matching': True
  65. }]
  66. def _real_initialize(self):
  67. email, password = self._get_login_info()
  68. if email is None:
  69. return
  70. try:
  71. user_data = self._download_json(
  72. 'https://shahid.mbc.net/wd/service/users/login',
  73. None, 'Logging in', data=json.dumps({
  74. 'email': email,
  75. 'password': password,
  76. 'basic': 'false',
  77. }).encode('utf-8'), headers={
  78. 'Content-Type': 'application/json; charset=UTF-8',
  79. })['user']
  80. except ExtractorError as e:
  81. if isinstance(e.cause, compat_HTTPError):
  82. self._handle_error(e)
  83. raise
  84. self._download_webpage(
  85. 'https://shahid.mbc.net/populateContext',
  86. None, 'Populate Context', data=urlencode_postdata({
  87. 'firstName': user_data['firstName'],
  88. 'lastName': user_data['lastName'],
  89. 'userName': user_data['email'],
  90. 'csg_user_name': user_data['email'],
  91. 'subscriberId': user_data['id'],
  92. 'sessionId': user_data['sessionId'],
  93. }))
  94. def _real_extract(self, url):
  95. page_type, video_id = re.match(self._VALID_URL, url).groups()
  96. if page_type == 'clip':
  97. page_type = 'episode'
  98. playout = self._call_api(
  99. 'playout/url/' + video_id, video_id)['playout']
  100. if playout.get('drm'):
  101. raise ExtractorError('This video is DRM protected.', expected=True)
  102. formats = self._extract_m3u8_formats(playout['url'], video_id, 'mp4')
  103. self._sort_formats(formats)
  104. # video = self._call_api(
  105. # 'product/id', video_id, {
  106. # 'id': video_id,
  107. # 'productType': 'ASSET',
  108. # 'productSubType': page_type.upper()
  109. # })['productModel']
  110. response = self._download_json(
  111. 'http://api.shahid.net/api/v1_1/%s/%s' % (page_type, video_id),
  112. video_id, 'Downloading video JSON', query={
  113. 'apiKey': 'sh@hid0nlin3',
  114. 'hash': 'b2wMCTHpSmyxGqQjJFOycRmLSex+BpTK/ooxy6vHaqs=',
  115. })
  116. data = response.get('data', {})
  117. error = data.get('error')
  118. if error:
  119. raise ExtractorError(
  120. '%s returned error: %s' % (self.IE_NAME, '\n'.join(error.values())),
  121. expected=True)
  122. video = data[page_type]
  123. title = video['title']
  124. categories = [
  125. category['name']
  126. for category in video.get('genres', []) if 'name' in category]
  127. return {
  128. 'id': video_id,
  129. 'title': title,
  130. 'description': video.get('description'),
  131. 'thumbnail': video.get('thumbnailUrl'),
  132. 'duration': int_or_none(video.get('duration')),
  133. 'timestamp': parse_iso8601(video.get('referenceDate')),
  134. 'categories': categories,
  135. 'series': video.get('showTitle') or video.get('showName'),
  136. 'season': video.get('seasonTitle'),
  137. 'season_number': int_or_none(video.get('seasonNumber')),
  138. 'season_id': str_or_none(video.get('seasonId')),
  139. 'episode_number': int_or_none(video.get('number')),
  140. 'episode_id': video_id,
  141. 'formats': formats,
  142. }
  143. class ShahidShowIE(ShahidBaseIE):
  144. _VALID_URL = r'https?://shahid\.mbc\.net/ar/(?:show|serie)s/[^/]+/(?:show|series)-(?P<id>\d+)'
  145. _TESTS = [{
  146. 'url': 'https://shahid.mbc.net/ar/shows/%D8%B1%D8%A7%D9%85%D8%B2-%D9%82%D8%B1%D8%B4-%D8%A7%D9%84%D8%A8%D8%AD%D8%B1/show-79187',
  147. 'info_dict': {
  148. 'id': '79187',
  149. 'title': 'رامز قرش البحر',
  150. 'description': 'md5:c88fa7e0f02b0abd39d417aee0d046ff',
  151. },
  152. 'playlist_mincount': 32,
  153. }, {
  154. 'url': 'https://shahid.mbc.net/ar/series/How-to-live-Longer-(The-Big-Think)/series-291861',
  155. 'only_matching': True
  156. }]
  157. _PAGE_SIZE = 30
  158. def _real_extract(self, url):
  159. show_id = self._match_id(url)
  160. product = self._call_api(
  161. 'playableAsset', show_id, {'showId': show_id})['productModel']
  162. playlist = product['playlist']
  163. playlist_id = playlist['id']
  164. show = product.get('show', {})
  165. def page_func(page_num):
  166. playlist = self._call_api(
  167. 'product/playlist', show_id, {
  168. 'playListId': playlist_id,
  169. 'pageNumber': page_num,
  170. 'pageSize': 30,
  171. 'sorts': [{
  172. 'order': 'DESC',
  173. 'type': 'SORTDATE'
  174. }],
  175. })
  176. for product in playlist.get('productList', {}).get('products', []):
  177. product_url = product.get('productUrl', []).get('url')
  178. if not product_url:
  179. continue
  180. yield self.url_result(
  181. product_url, 'Shahid',
  182. str_or_none(product.get('id')),
  183. product.get('title'))
  184. entries = InAdvancePagedList(
  185. page_func,
  186. math.ceil(playlist['count'] / self._PAGE_SIZE),
  187. self._PAGE_SIZE)
  188. return self.playlist_result(
  189. entries, show_id, show.get('title'), show.get('description'))