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.

107 lines
3.9 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': 'm3u8',
  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. formats = self._extract_m3u8_formats(player['url'], video_id, 'mp4')
  66. video = self._download_json(
  67. '%s/%s/%s?%s' % (
  68. api_vars['url'], api_vars['playerType'], api_vars['id'],
  69. compat_urllib_parse.urlencode({
  70. 'apiKey': 'sh@hid0nlin3',
  71. 'hash': 'b2wMCTHpSmyxGqQjJFOycRmLSex+BpTK/ooxy6vHaqs=',
  72. }).encode('utf-8')),
  73. video_id, 'Downloading video JSON')
  74. video = video[api_vars['playerType']]
  75. title = video['title']
  76. description = video.get('description')
  77. thumbnail = video.get('thumbnailUrl')
  78. duration = int_or_none(video.get('duration'))
  79. timestamp = parse_iso8601(video.get('referenceDate'))
  80. categories = [
  81. category['name']
  82. for category in video.get('genres', []) if 'name' in category]
  83. return {
  84. 'id': video_id,
  85. 'title': title,
  86. 'description': description,
  87. 'thumbnail': thumbnail,
  88. 'duration': duration,
  89. 'timestamp': timestamp,
  90. 'categories': categories,
  91. 'formats': formats,
  92. }