]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/shahid.py
Prepare new release.
[youtubedl] / youtube_dl / extractor / shahid.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import json
6
7 from .common import InfoExtractor
8 from ..compat import compat_HTTPError
9 from ..utils import (
10 ExtractorError,
11 int_or_none,
12 parse_iso8601,
13 str_or_none,
14 urlencode_postdata,
15 clean_html,
16 )
17
18
19 class ShahidIE(InfoExtractor):
20 _NETRC_MACHINE = 'shahid'
21 _VALID_URL = r'https?://shahid\.mbc\.net/ar/(?P<type>episode|movie)/(?P<id>\d+)'
22 _TESTS = [{
23 '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',
24 'info_dict': {
25 'id': '90574',
26 'ext': 'mp4',
27 'title': 'الملك عبدالله الإنسان الموسم 1 كليب 3',
28 'description': 'الفيلم الوثائقي - الملك عبد الله الإنسان',
29 'duration': 2972,
30 'timestamp': 1422057420,
31 'upload_date': '20150123',
32 },
33 'params': {
34 # m3u8 download
35 'skip_download': True,
36 }
37 }, {
38 'url': 'https://shahid.mbc.net/ar/movie/151746/%D8%A7%D9%84%D9%82%D9%86%D8%A7%D8%B5%D8%A9.html',
39 'only_matching': True
40 }, {
41 # shahid plus subscriber only
42 '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',
43 'only_matching': True
44 }]
45
46 def _real_initialize(self):
47 email, password = self._get_login_info()
48 if email is None:
49 return
50
51 try:
52 user_data = self._download_json(
53 'https://shahid.mbc.net/wd/service/users/login',
54 None, 'Logging in', data=json.dumps({
55 'email': email,
56 'password': password,
57 'basic': 'false',
58 }).encode('utf-8'), headers={
59 'Content-Type': 'application/json; charset=UTF-8',
60 })['user']
61 except ExtractorError as e:
62 if isinstance(e.cause, compat_HTTPError):
63 fail_data = self._parse_json(
64 e.cause.read().decode('utf-8'), None, fatal=False)
65 if fail_data:
66 faults = fail_data.get('faults', [])
67 faults_message = ', '.join([clean_html(fault['userMessage']) for fault in faults if fault.get('userMessage')])
68 if faults_message:
69 raise ExtractorError(faults_message, expected=True)
70 raise
71
72 self._download_webpage(
73 'https://shahid.mbc.net/populateContext',
74 None, 'Populate Context', data=urlencode_postdata({
75 'firstName': user_data['firstName'],
76 'lastName': user_data['lastName'],
77 'userName': user_data['email'],
78 'csg_user_name': user_data['email'],
79 'subscriberId': user_data['id'],
80 'sessionId': user_data['sessionId'],
81 }))
82
83 def _get_api_data(self, response):
84 data = response.get('data', {})
85
86 error = data.get('error')
87 if error:
88 raise ExtractorError(
89 '%s returned error: %s' % (self.IE_NAME, '\n'.join(error.values())),
90 expected=True)
91
92 return data
93
94 def _real_extract(self, url):
95 page_type, video_id = re.match(self._VALID_URL, url).groups()
96
97 player = self._get_api_data(self._download_json(
98 'https://shahid.mbc.net/arContent/getPlayerContent-param-.id-%s.type-player.html' % video_id,
99 video_id, 'Downloading player JSON'))
100
101 if player.get('drm'):
102 raise ExtractorError('This video is DRM protected.', expected=True)
103
104 formats = self._extract_m3u8_formats(player['url'], video_id, 'mp4')
105 self._sort_formats(formats)
106
107 video = self._get_api_data(self._download_json(
108 'http://api.shahid.net/api/v1_1/%s/%s' % (page_type, video_id),
109 video_id, 'Downloading video JSON', query={
110 'apiKey': 'sh@hid0nlin3',
111 'hash': 'b2wMCTHpSmyxGqQjJFOycRmLSex+BpTK/ooxy6vHaqs=',
112 }))[page_type]
113
114 title = video['title']
115 categories = [
116 category['name']
117 for category in video.get('genres', []) if 'name' in category]
118
119 return {
120 'id': video_id,
121 'title': title,
122 'description': video.get('description'),
123 'thumbnail': video.get('thumbnailUrl'),
124 'duration': int_or_none(video.get('duration')),
125 'timestamp': parse_iso8601(video.get('referenceDate')),
126 'categories': categories,
127 'series': video.get('showTitle') or video.get('showName'),
128 'season': video.get('seasonTitle'),
129 'season_number': int_or_none(video.get('seasonNumber')),
130 'season_id': str_or_none(video.get('seasonId')),
131 'episode_number': int_or_none(video.get('number')),
132 'episode_id': video_id,
133 'formats': formats,
134 }