[dcn] Enable dash formats
[youtube-dl] / youtube_dl / extractor / dcn.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import base64
6
7 from .common import InfoExtractor
8 from ..compat import (
9     compat_urllib_parse_urlencode,
10     compat_str,
11 )
12 from ..utils import (
13     int_or_none,
14     parse_iso8601,
15     sanitized_Request,
16     smuggle_url,
17     unsmuggle_url,
18     urlencode_postdata,
19 )
20
21
22 class DCNIE(InfoExtractor):
23     _VALID_URL = r'https?://(?:www\.)?(?:awaan|dcndigital)\.ae/(?:#/)?show/(?P<show_id>\d+)/[^/]+(?:/(?P<video_id>\d+)/(?P<season_id>\d+))?'
24
25     def _real_extract(self, url):
26         show_id, video_id, season_id = re.match(self._VALID_URL, url).groups()
27         if video_id and int(video_id) > 0:
28             return self.url_result(
29                 'http://www.dcndigital.ae/media/%s' % video_id, 'DCNVideo')
30         elif season_id and int(season_id) > 0:
31             return self.url_result(smuggle_url(
32                 'http://www.dcndigital.ae/program/season/%s' % season_id,
33                 {'show_id': show_id}), 'DCNSeason')
34         else:
35             return self.url_result(
36                 'http://www.dcndigital.ae/program/%s' % show_id, 'DCNSeason')
37
38
39 class DCNBaseIE(InfoExtractor):
40     def _extract_video_info(self, video_data, video_id, is_live):
41         title = video_data.get('title_en') or video_data['title_ar']
42         img = video_data.get('img')
43         thumbnail = 'http://admin.mangomolo.com/analytics/%s' % img if img else None
44         duration = int_or_none(video_data.get('duration'))
45         description = video_data.get('description_en') or video_data.get('description_ar')
46         timestamp = parse_iso8601(video_data.get('create_time'), ' ')
47
48         return {
49             'id': video_id,
50             'title': self._live_title(title) if is_live else title,
51             'description': description,
52             'thumbnail': thumbnail,
53             'duration': duration,
54             'timestamp': timestamp,
55             'is_live': is_live,
56         }
57
58     def _extract_video_formats(self, webpage, video_id, m3u8_entry_protocol):
59         formats = []
60         format_url_base = 'http' + self._html_search_regex(
61             [
62                 r'file\s*:\s*"https?(://[^"]+)/playlist.m3u8',
63                 r'<a[^>]+href="rtsp(://[^"]+)"'
64             ], webpage, 'format url')
65         formats.extend(self._extract_mpd_formats(
66             format_url_base + '/manifest.mpd',
67             video_id, mpd_id='dash', fatal=False))
68         formats.extend(self._extract_m3u8_formats(
69             format_url_base + '/playlist.m3u8', video_id, 'mp4',
70             m3u8_entry_protocol, m3u8_id='hls', fatal=False))
71         formats.extend(self._extract_f4m_formats(
72             format_url_base + '/manifest.f4m',
73             video_id, f4m_id='hds', fatal=False))
74         self._sort_formats(formats)
75         return formats
76
77
78 class DCNVideoIE(DCNBaseIE):
79     IE_NAME = 'dcn:video'
80     _VALID_URL = r'https?://(?:www\.)?(?:awaan|dcndigital)\.ae/(?:#/)?(?:video(?:/[^/]+)?|media|catchup/[^/]+/[^/]+)/(?P<id>\d+)'
81     _TESTS = [{
82         'url': 'http://www.dcndigital.ae/#/video/%D8%B1%D8%AD%D9%84%D8%A9-%D8%A7%D9%84%D8%B9%D9%85%D8%B1-%D8%A7%D9%84%D8%AD%D9%84%D9%82%D8%A9-1/17375',
83         'info_dict':
84         {
85             'id': '17375',
86             'ext': 'mp4',
87             'title': 'رحلة العمر : الحلقة 1',
88             'description': 'md5:0156e935d870acb8ef0a66d24070c6d6',
89             'duration': 2041,
90             'timestamp': 1227504126,
91             'upload_date': '20081124',
92         },
93         'params': {
94             # m3u8 download
95             'skip_download': True,
96         },
97     }, {
98         'url': 'http://awaan.ae/video/26723981/%D8%AF%D8%A7%D8%B1-%D8%A7%D9%84%D8%B3%D9%84%D8%A7%D9%85:-%D8%AE%D9%8A%D8%B1-%D8%AF%D9%88%D8%B1-%D8%A7%D9%84%D8%A3%D9%86%D8%B5%D8%A7%D8%B1',
99         'only_matching': True,
100     }]
101
102     def _real_extract(self, url):
103         video_id = self._match_id(url)
104
105         request = sanitized_Request(
106             'http://admin.mangomolo.com/analytics/index.php/plus/video?id=%s' % video_id,
107             headers={'Origin': 'http://www.dcndigital.ae'})
108         video_data = self._download_json(request, video_id)
109         info = self._extract_video_info(video_data, video_id, False)
110
111         webpage = self._download_webpage(
112             'http://admin.mangomolo.com/analytics/index.php/customers/embed/video?' +
113             compat_urllib_parse_urlencode({
114                 'id': video_data['id'],
115                 'user_id': video_data['user_id'],
116                 'signature': video_data['signature'],
117                 'countries': 'Q0M=',
118                 'filter': 'DENY',
119             }), video_id)
120         info['formats'] = self._extract_video_formats(webpage, video_id, 'm3u8_native')
121         return info
122
123
124 class DCNLiveIE(DCNBaseIE):
125     IE_NAME = 'dcn:live'
126     _VALID_URL = r'https?://(?:www\.)?(?:awaan|dcndigital)\.ae/(?:#/)?live/(?P<id>\d+)'
127
128     def _real_extract(self, url):
129         channel_id = self._match_id(url)
130
131         request = sanitized_Request(
132             'http://admin.mangomolo.com/analytics/index.php/plus/getchanneldetails?channel_id=%s' % channel_id,
133             headers={'Origin': 'http://www.dcndigital.ae'})
134
135         channel_data = self._download_json(request, channel_id)
136         info = self._extract_video_info(channel_data, channel_id, True)
137
138         webpage = self._download_webpage(
139             'http://admin.mangomolo.com/analytics/index.php/customers/embed/index?' +
140             compat_urllib_parse_urlencode({
141                 'id': base64.b64encode(channel_data['user_id'].encode()).decode(),
142                 'channelid': base64.b64encode(channel_data['id'].encode()).decode(),
143                 'signature': channel_data['signature'],
144                 'countries': 'Q0M=',
145                 'filter': 'DENY',
146             }), channel_id)
147         info['formats'] = self._extract_video_formats(webpage, channel_id, 'm3u8')
148         return info
149
150
151 class DCNSeasonIE(InfoExtractor):
152     IE_NAME = 'dcn:season'
153     _VALID_URL = r'https?://(?:www\.)?(?:awaan|dcndigital)\.ae/(?:#/)?program/(?:(?P<show_id>\d+)|season/(?P<season_id>\d+))'
154     _TEST = {
155         'url': 'http://dcndigital.ae/#/program/205024/%D9%85%D8%AD%D8%A7%D8%B6%D8%B1%D8%A7%D8%AA-%D8%A7%D9%84%D8%B4%D9%8A%D8%AE-%D8%A7%D9%84%D8%B4%D8%B9%D8%B1%D8%A7%D9%88%D9%8A',
156         'info_dict':
157         {
158             'id': '7910',
159             'title': 'محاضرات الشيخ الشعراوي',
160         },
161         'playlist_mincount': 27,
162     }
163
164     def _real_extract(self, url):
165         url, smuggled_data = unsmuggle_url(url, {})
166         show_id, season_id = re.match(self._VALID_URL, url).groups()
167
168         data = {}
169         if season_id:
170             data['season'] = season_id
171             show_id = smuggled_data.get('show_id')
172             if show_id is None:
173                 request = sanitized_Request(
174                     'http://admin.mangomolo.com/analytics/index.php/plus/season_info?id=%s' % season_id,
175                     headers={'Origin': 'http://www.dcndigital.ae'})
176                 season = self._download_json(request, season_id)
177                 show_id = season['id']
178         data['show_id'] = show_id
179         request = sanitized_Request(
180             'http://admin.mangomolo.com/analytics/index.php/plus/show',
181             urlencode_postdata(data),
182             {
183                 'Origin': 'http://www.dcndigital.ae',
184                 'Content-Type': 'application/x-www-form-urlencoded'
185             })
186
187         show = self._download_json(request, show_id)
188         if not season_id:
189             season_id = show['default_season']
190         for season in show['seasons']:
191             if season['id'] == season_id:
192                 title = season.get('title_en') or season['title_ar']
193
194                 entries = []
195                 for video in show['videos']:
196                     video_id = compat_str(video['id'])
197                     entries.append(self.url_result(
198                         'http://www.dcndigital.ae/media/%s' % video_id, 'DCNVideo', video_id))
199
200                 return self.playlist_result(entries, season_id, title)