logo

youtube-dl

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

shahid.py (8448B)


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