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.

110 lines
4.0 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..compat import compat_urllib_parse
  5. from ..utils import (
  6. ExtractorError,
  7. int_or_none,
  8. parse_iso8601,
  9. )
  10. class ShahidIE(InfoExtractor):
  11. _VALID_URL = r'https?://shahid\.mbc\.net/ar/episode/(?P<id>\d+)/?'
  12. _TESTS = [{
  13. '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',
  14. 'info_dict': {
  15. 'id': '90574',
  16. 'ext': 'mp4',
  17. 'title': 'الملك عبدالله الإنسان الموسم 1 كليب 3',
  18. 'description': 'الفيلم الوثائقي - الملك عبد الله الإنسان',
  19. 'duration': 2972,
  20. 'timestamp': 1422057420,
  21. 'upload_date': '20150123',
  22. },
  23. 'params': {
  24. # m3u8 download
  25. 'skip_download': True,
  26. }
  27. }, {
  28. # shahid plus subscriber only
  29. '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',
  30. 'only_matching': True
  31. }]
  32. def _handle_error(self, response):
  33. if not isinstance(response, dict):
  34. return
  35. error = response.get('error')
  36. if error:
  37. raise ExtractorError(
  38. '%s returned error: %s' % (self.IE_NAME, '\n'.join(error.values())),
  39. expected=True)
  40. def _download_json(self, url, video_id, note='Downloading JSON metadata'):
  41. response = super(ShahidIE, self)._download_json(url, video_id, note)['data']
  42. self._handle_error(response)
  43. return response
  44. def _real_extract(self, url):
  45. video_id = self._match_id(url)
  46. webpage = self._download_webpage(url, video_id)
  47. api_vars = {
  48. 'id': video_id,
  49. 'type': 'player',
  50. 'url': 'http://api.shahid.net/api/v1_1',
  51. 'playerType': 'episode',
  52. }
  53. flashvars = self._search_regex(
  54. r'var\s+flashvars\s*=\s*({[^}]+})', webpage, 'flashvars', default=None)
  55. if flashvars:
  56. for key in api_vars.keys():
  57. value = self._search_regex(
  58. r'\b%s\s*:\s*(?P<q>["\'])(?P<value>.+?)(?P=q)' % key,
  59. flashvars, 'type', default=None, group='value')
  60. if value:
  61. api_vars[key] = value
  62. player = self._download_json(
  63. 'https://shahid.mbc.net/arContent/getPlayerContent-param-.id-%s.type-%s.html'
  64. % (video_id, api_vars['type']), video_id, 'Downloading player JSON')
  65. if player.get('drm'):
  66. raise ExtractorError('This video is DRM protected.', expected=True)
  67. formats = self._extract_m3u8_formats(player['url'], video_id, 'mp4')
  68. video = self._download_json(
  69. '%s/%s/%s?%s' % (
  70. api_vars['url'], api_vars['playerType'], api_vars['id'],
  71. compat_urllib_parse.urlencode({
  72. 'apiKey': 'sh@hid0nlin3',
  73. 'hash': 'b2wMCTHpSmyxGqQjJFOycRmLSex+BpTK/ooxy6vHaqs=',
  74. })),
  75. video_id, 'Downloading video JSON')
  76. video = video[api_vars['playerType']]
  77. title = video['title']
  78. description = video.get('description')
  79. thumbnail = video.get('thumbnailUrl')
  80. duration = int_or_none(video.get('duration'))
  81. timestamp = parse_iso8601(video.get('referenceDate'))
  82. categories = [
  83. category['name']
  84. for category in video.get('genres', []) if 'name' in category]
  85. return {
  86. 'id': video_id,
  87. 'title': title,
  88. 'description': description,
  89. 'thumbnail': thumbnail,
  90. 'duration': duration,
  91. 'timestamp': timestamp,
  92. 'categories': categories,
  93. 'formats': formats,
  94. }