[dcn] make m3u8 formats extraction non fatal
[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,
10     compat_urllib_request,
11 )
12 from ..utils import (
13     int_or_none,
14     parse_iso8601,
15     smuggle_url,
16     unsmuggle_url,
17 )
18
19
20 class DCNIE(InfoExtractor):
21     _VALID_URL = r'https?://(?:www\.)?dcndigital\.ae/(?:#/)?show/(?P<show_id>\d+)/[^/]+(?:/(?P<video_id>\d+)/(?P<season_id>\d+))?'
22
23     def _real_extract(self, url):
24         show_id, video_id, season_id = re.match(self._VALID_URL, url).groups()
25         if video_id and int(video_id) > 0:
26             return self.url_result(
27                 'http://www.dcndigital.ae/media/%s' % video_id, 'DCNVideo')
28         elif season_id and int(season_id) > 0:
29             return self.url_result(smuggle_url(
30                 'http://www.dcndigital.ae/program/season/%s' % season_id,
31                 {'show_id': show_id}), 'DCNSeason')
32         else:
33             return self.url_result(
34                 'http://www.dcndigital.ae/program/%s' % show_id, 'DCNSeason')
35
36
37 class DCNBaseIE(InfoExtractor):
38     def _extract_video_info(self, video_data, video_id, is_live):
39         title = video_data.get('title_en') or video_data['title_ar']
40         img = video_data.get('img')
41         thumbnail = 'http://admin.mangomolo.com/analytics/%s' % img if img else None
42         duration = int_or_none(video_data.get('duration'))
43         description = video_data.get('description_en') or video_data.get('description_ar')
44         timestamp = parse_iso8601(video_data.get('create_time'), ' ')
45
46         return {
47             'id': video_id,
48             'title': self._live_title(title) if is_live else title,
49             'description': description,
50             'thumbnail': thumbnail,
51             'duration': duration,
52             'timestamp': timestamp,
53             'is_live': is_live,
54         }
55
56     def _extract_video_formats(self, webpage, video_id, entry_protocol):
57         formats = []
58         m3u8_url = self._html_search_regex(
59             r'file\s*:\s*"([^"]+)', webpage, 'm3u8 url', fatal=False)
60         if m3u8_url:
61             m3u8_formats = self._extract_m3u8_formats(
62                 m3u8_url, video_id, 'mp4', entry_protocol, m3u8_id='hls', fatal=None)
63             if m3u8_formats:
64                 formats.extend(m3u8_formats)
65
66         rtsp_url = self._search_regex(
67             r'<a[^>]+href="(rtsp://[^"]+)"', webpage, 'rtsp url', fatal=False)
68         if rtsp_url:
69             formats.append({
70                 'url': rtsp_url,
71                 'format_id': 'rtsp',
72             })
73
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\.)?dcndigital\.ae/(?:#/)?(?:video/[^/]+|media|catchup/[^/]+/[^/]+)/(?P<id>\d+)'
81     _TEST = {
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
99     def _real_extract(self, url):
100         video_id = self._match_id(url)
101
102         request = compat_urllib_request.Request(
103             'http://admin.mangomolo.com/analytics/index.php/plus/video?id=%s' % video_id,
104             headers={'Origin': 'http://www.dcndigital.ae'})
105         video_data = self._download_json(request, video_id)
106         info = self._extract_video_info(video_data, video_id, False)
107
108         webpage = self._download_webpage(
109             'http://admin.mangomolo.com/analytics/index.php/customers/embed/video?' +
110             compat_urllib_parse.urlencode({
111                 'id': video_data['id'],
112                 'user_id': video_data['user_id'],
113                 'signature': video_data['signature'],
114                 'countries': 'Q0M=',
115                 'filter': 'DENY',
116             }), video_id)
117         info['formats'] = self._extract_video_formats(webpage, video_id, 'm3u8_native')
118         return info
119
120
121 class DCNLiveIE(DCNBaseIE):
122     IE_NAME = 'dcn:live'
123     _VALID_URL = r'https?://(?:www\.)?dcndigital\.ae/(?:#/)?live/(?P<id>\d+)'
124
125     def _real_extract(self, url):
126         channel_id = self._match_id(url)
127
128         request = compat_urllib_request.Request(
129             'http://admin.mangomolo.com/analytics/index.php/plus/getchanneldetails?channel_id=%s' % channel_id,
130             headers={'Origin': 'http://www.dcndigital.ae'})
131
132         channel_data = self._download_json(request, channel_id)
133         info = self._extract_video_info(channel_data, channel_id, True)
134
135         webpage = self._download_webpage(
136             'http://admin.mangomolo.com/analytics/index.php/customers/embed/index?' +
137             compat_urllib_parse.urlencode({
138                 'id': base64.b64encode(channel_data['user_id'].encode()).decode(),
139                 'channelid': base64.b64encode(channel_data['id'].encode()).decode(),
140                 'signature': channel_data['signature'],
141                 'countries': 'Q0M=',
142                 'filter': 'DENY',
143             }), channel_id)
144         info['formats'] = self._extract_video_formats(webpage, channel_id, 'm3u8')
145         return info
146
147
148 class DCNSeasonIE(InfoExtractor):
149     IE_NAME = 'dcn:season'
150     _VALID_URL = r'https?://(?:www\.)?dcndigital\.ae/(?:#/)?program/(?:(?P<show_id>\d+)|season/(?P<season_id>\d+))'
151     _TEST = {
152         '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',
153         'info_dict':
154         {
155             'id': '7910',
156             'title': 'محاضرات الشيخ الشعراوي',
157         },
158         'playlist_mincount': 27,
159     }
160
161     def _real_extract(self, url):
162         url, smuggled_data = unsmuggle_url(url, {})
163         show_id, season_id = re.match(self._VALID_URL, url).groups()
164
165         data = {}
166         if season_id:
167             data['season'] = season_id
168             show_id = smuggled_data.get('show_id')
169             if show_id is None:
170                 request = compat_urllib_request.Request(
171                     'http://admin.mangomolo.com/analytics/index.php/plus/season_info?id=%s' % season_id,
172                     headers={'Origin': 'http://www.dcndigital.ae'})
173                 season = self._download_json(request, season_id)
174                 show_id = season['id']
175         data['show_id'] = show_id
176         request = compat_urllib_request.Request(
177             'http://admin.mangomolo.com/analytics/index.php/plus/show',
178             compat_urllib_parse.urlencode(data),
179             {
180                 'Origin': 'http://www.dcndigital.ae',
181                 'Content-Type': 'application/x-www-form-urlencoded'
182             })
183
184         show = self._download_json(request, show_id)
185         if not season_id:
186             season_id = show['default_season']
187         for season in show['seasons']:
188             if season['id'] == season_id:
189                 title = season.get('title_en') or season['title_ar']
190
191                 entries = []
192                 for video in show['videos']:
193                     entries.append(self.url_result(
194                         'http://www.dcndigital.ae/media/%s' % video['id'], 'DCNVideo'))
195
196                 return self.playlist_result(entries, season_id, title)