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