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