[dcn] improve extraction
[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         m3u8_url = self._html_search_regex(
58             r'file\s*:\s*"([^"]+)', webpage, 'm3u8 url')
59         formats = self._extract_m3u8_formats(
60             m3u8_url, video_id, 'mp4', entry_protocol, m3u8_id='hls')
61
62         rtsp_url = self._search_regex(
63             r'<a[^>]+href="(rtsp://[^"]+)"', webpage, 'rtsp url', fatal=False)
64         if rtsp_url:
65             formats.append({
66                 'url': rtsp_url,
67                 'format_id': 'rtsp',
68             })
69
70         self._sort_formats(formats)
71         return formats
72
73
74 class DCNVideoIE(DCNBaseIE):
75     IE_NAME = 'dcn:video'
76     _VALID_URL = r'https?://(?:www\.)?dcndigital\.ae/(?:#/)?(?:video/[^/]+|media|catchup/[^/]+/[^/]+)/(?P<id>\d+)'
77     _TEST = {
78         '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',
79         'info_dict':
80         {
81             'id': '17375',
82             'ext': 'mp4',
83             'title': 'رحلة العمر : الحلقة 1',
84             'description': 'md5:0156e935d870acb8ef0a66d24070c6d6',
85             'duration': 2041,
86             'timestamp': 1227504126,
87             'upload_date': '20081124',
88         },
89         'params': {
90             # m3u8 download
91             'skip_download': True,
92         },
93     }
94
95     def _real_extract(self, url):
96         video_id = self._match_id(url)
97
98         request = compat_urllib_request.Request(
99             'http://admin.mangomolo.com/analytics/index.php/plus/video?id=%s' % video_id,
100             headers={'Origin': 'http://www.dcndigital.ae'})
101         video_data = self._download_json(request, video_id)
102         info = self._extract_video_info(video_data, video_id, False)
103
104         webpage = self._download_webpage(
105             'http://admin.mangomolo.com/analytics/index.php/customers/embed/video?' +
106             compat_urllib_parse.urlencode({
107                 'id': video_data['id'],
108                 'user_id': video_data['user_id'],
109                 'signature': video_data['signature'],
110                 'countries': 'Q0M=',
111                 'filter': 'DENY',
112             }), video_id)
113         info['formats'] = self._extract_video_formats(webpage, video_id, 'm3u8_native')
114         return info
115
116
117 class DCNLiveIE(DCNBaseIE):
118     IE_NAME = 'dcn:live'
119     _VALID_URL = r'https?://(?:www\.)?dcndigital\.ae/(?:#/)?live/(?P<id>\d+)'
120     _TEST = {
121         'url': 'http://www.dcndigital.ae/#/live/6/dubai-tv',
122         'info_dict':
123         {
124             'id': '6',
125             'ext': 'mp4',
126             'title': 're:^Dubai Al Oula [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
127             'thumbnail': 're:^https?://.*\.png$',
128             'is_live': True,
129         },
130         'params': {
131             # m3u8 download
132             'skip_download': True,
133         },
134     }
135
136     def _real_extract(self, url):
137         channel_id = self._match_id(url)
138
139         request = compat_urllib_request.Request(
140             'http://admin.mangomolo.com/analytics/index.php/plus/getchanneldetails?channel_id=%s' % channel_id,
141             headers={'Origin': 'http://www.dcndigital.ae'})
142
143         channel_data = self._download_json(request, channel_id)
144         info = self._extract_video_info(channel_data, channel_id, True)
145
146         webpage = self._download_webpage(
147             'http://admin.mangomolo.com/analytics/index.php/customers/embed/index?' +
148             compat_urllib_parse.urlencode({
149                 'id': base64.b64encode(channel_data['user_id'].encode()).decode(),
150                 'channelid': base64.b64encode(channel_data['id'].encode()).decode(),
151                 'signature': channel_data['signature'],
152                 'countries': 'Q0M=',
153                 'filter': 'DENY',
154             }), channel_id)
155         info['formats'] = self._extract_video_formats(webpage, channel_id, 'm3u8')
156         return info
157
158
159 class DCNSeasonIE(InfoExtractor):
160     IE_NAME = 'dcn:season'
161     _VALID_URL = r'https?://(?:www\.)?dcndigital\.ae/(?:#/)?program/(?:(?P<show_id>\d+)|season/(?P<season_id>\d+))'
162     _TEST = {
163         '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',
164         'info_dict':
165         {
166             'id': '7910',
167             'title': 'محاضرات الشيخ الشعراوي',
168         },
169         'playlist_mincount': 27,
170     }
171
172     def _real_extract(self, url):
173         url, smuggled_data = unsmuggle_url(url, {})
174         show_id, season_id = re.match(self._VALID_URL, url).groups()
175
176         data = {}
177         if season_id:
178             data['season'] = season_id
179             show_id = smuggled_data.get('show_id')
180             if show_id is None:
181                 request = compat_urllib_request.Request(
182                     'http://admin.mangomolo.com/analytics/index.php/plus/season_info?id=%s' % season_id,
183                     headers={'Origin': 'http://www.dcndigital.ae'})
184                 season = self._download_json(request, season_id)
185                 show_id = season['id']
186         data['show_id'] = show_id
187         request = compat_urllib_request.Request(
188             'http://admin.mangomolo.com/analytics/index.php/plus/show',
189             compat_urllib_parse.urlencode(data),
190             {
191                 'Origin': 'http://www.dcndigital.ae',
192                 'Content-Type': 'application/x-www-form-urlencoded'
193             })
194
195         show = self._download_json(request, show_id)
196         if not season_id:
197             season_id = show['default_season']
198         for season in show['seasons']:
199             if season['id'] == season_id:
200                 title = season.get('title_en') or season['title_ar']
201
202                 entries = []
203                 for video in show['videos']:
204                     entries.append(self.url_result(
205                         'http://www.dcndigital.ae/media/%s' % video['id'], 'DCNVideo'))
206
207                 return self.playlist_result(entries, season_id, title)