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.

134 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/(?P<type>episode|movie)/(?P<id>\d+)'
  18. _TESTS = [{
  19. 'url': 'https://shahid.mbc.net/ar/episode/90574/%D8%A7%D9%84%D9%85%D9%84%D9%83-%D8%B9%D8%A8%D8%AF%D8%A7%D9%84%D9%84%D9%87-%D8%A7%D9%84%D8%A5%D9%86%D8%B3%D8%A7%D9%86-%D8%A7%D9%84%D9%85%D9%88%D8%B3%D9%85-1-%D9%83%D9%84%D9%8A%D8%A8-3.html',
  20. 'info_dict': {
  21. 'id': '90574',
  22. 'ext': 'mp4',
  23. 'title': 'الملك عبدالله الإنسان الموسم 1 كليب 3',
  24. 'description': 'الفيلم الوثائقي - الملك عبد الله الإنسان',
  25. 'duration': 2972,
  26. 'timestamp': 1422057420,
  27. 'upload_date': '20150123',
  28. },
  29. 'params': {
  30. # m3u8 download
  31. 'skip_download': True,
  32. }
  33. }, {
  34. 'url': 'https://shahid.mbc.net/ar/movie/151746/%D8%A7%D9%84%D9%82%D9%86%D8%A7%D8%B5%D8%A9.html',
  35. 'only_matching': True
  36. }, {
  37. # shahid plus subscriber only
  38. 'url': 'https://shahid.mbc.net/ar/episode/90511/%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.html',
  39. 'only_matching': True
  40. }]
  41. def _real_initialize(self):
  42. email, password = self._get_login_info()
  43. if email is None:
  44. return
  45. try:
  46. user_data = self._download_json(
  47. 'https://shahid.mbc.net/wd/service/users/login',
  48. None, 'Logging in', data=json.dumps({
  49. 'email': email,
  50. 'password': password,
  51. 'basic': 'false',
  52. }).encode('utf-8'), headers={
  53. 'Content-Type': 'application/json; charset=UTF-8',
  54. })['user']
  55. except ExtractorError as e:
  56. if isinstance(e.cause, compat_HTTPError):
  57. fail_data = self._parse_json(
  58. e.cause.read().decode('utf-8'), None, fatal=False)
  59. if fail_data:
  60. faults = fail_data.get('faults', [])
  61. faults_message = ', '.join([clean_html(fault['userMessage']) for fault in faults if fault.get('userMessage')])
  62. if faults_message:
  63. raise ExtractorError(faults_message, expected=True)
  64. raise
  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. player = self._get_api_data(self._download_json(
  86. 'https://shahid.mbc.net/arContent/getPlayerContent-param-.id-%s.type-player.html' % video_id,
  87. video_id, 'Downloading player JSON'))
  88. if player.get('drm'):
  89. raise ExtractorError('This video is DRM protected.', expected=True)
  90. formats = self._extract_m3u8_formats(player['url'], video_id, 'mp4')
  91. self._sort_formats(formats)
  92. video = self._get_api_data(self._download_json(
  93. 'http://api.shahid.net/api/v1_1/%s/%s' % (page_type, video_id),
  94. video_id, 'Downloading video JSON', query={
  95. 'apiKey': 'sh@hid0nlin3',
  96. 'hash': 'b2wMCTHpSmyxGqQjJFOycRmLSex+BpTK/ooxy6vHaqs=',
  97. }))[page_type]
  98. title = video['title']
  99. categories = [
  100. category['name']
  101. for category in video.get('genres', []) if 'name' in category]
  102. return {
  103. 'id': video_id,
  104. 'title': title,
  105. 'description': video.get('description'),
  106. 'thumbnail': video.get('thumbnailUrl'),
  107. 'duration': int_or_none(video.get('duration')),
  108. 'timestamp': parse_iso8601(video.get('referenceDate')),
  109. 'categories': categories,
  110. 'series': video.get('showTitle') or video.get('showName'),
  111. 'season': video.get('seasonTitle'),
  112. 'season_number': int_or_none(video.get('seasonNumber')),
  113. 'season_id': str_or_none(video.get('seasonId')),
  114. 'episode_number': int_or_none(video.get('number')),
  115. 'episode_id': video_id,
  116. 'formats': formats,
  117. }