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