[mangomolo] add support for Mangomolo embeds
[youtube-dl] / youtube_dl / extractor / awaan.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     smuggle_url,
16     unsmuggle_url,
17     urlencode_postdata,
18 )
19
20
21 class AWAANIE(InfoExtractor):
22     _VALID_URL = r'https?://(?:www\.)?(?:awaan|dcndigital)\.ae/(?:#/)?show/(?P<show_id>\d+)/[^/]+(?:/(?P<video_id>\d+)/(?P<season_id>\d+))?'
23
24     def _real_extract(self, url):
25         show_id, video_id, season_id = re.match(self._VALID_URL, url).groups()
26         if video_id and int(video_id) > 0:
27             return self.url_result(
28                 'http://awaan.ae/media/%s' % video_id, 'AWAANVideo')
29         elif season_id and int(season_id) > 0:
30             return self.url_result(smuggle_url(
31                 'http://awaan.ae/program/season/%s' % season_id,
32                 {'show_id': show_id}), 'AWAANSeason')
33         else:
34             return self.url_result(
35                 'http://awaan.ae/program/%s' % show_id, 'AWAANSeason')
36
37
38 class AWAANBaseIE(InfoExtractor):
39     def _parse_video_data(self, video_data, video_id, is_live):
40         title = video_data.get('title_en') or video_data['title_ar']
41         img = video_data.get('img')
42
43         return {
44             'id': video_id,
45             'title': self._live_title(title) if is_live else title,
46             'description': video_data.get('description_en') or video_data.get('description_ar'),
47             'thumbnail': 'http://admin.mangomolo.com/analytics/%s' % img if img else None,
48             'duration': int_or_none(video_data.get('duration')),
49             'timestamp': parse_iso8601(video_data.get('create_time'), ' '),
50             'is_live': is_live,
51         }
52
53
54 class AWAANVideoIE(AWAANBaseIE):
55     IE_NAME = 'awaan:video'
56     _VALID_URL = r'https?://(?:www\.)?(?:awaan|dcndigital)\.ae/(?:#/)?(?:video(?:/[^/]+)?|media|catchup/[^/]+/[^/]+)/(?P<id>\d+)'
57     _TESTS = [{
58         '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',
59         'md5': '5f61c33bfc7794315c671a62d43116aa',
60         'info_dict':
61         {
62             'id': '17375',
63             'ext': 'mp4',
64             'title': 'رحلة العمر : الحلقة 1',
65             'description': 'md5:0156e935d870acb8ef0a66d24070c6d6',
66             'duration': 2041,
67             'timestamp': 1227504126,
68             'upload_date': '20081124',
69         },
70     }, {
71         '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',
72         'only_matching': True,
73     }]
74
75     def _real_extract(self, url):
76         video_id = self._match_id(url)
77
78         video_data = self._download_json(
79             'http://admin.mangomolo.com/analytics/index.php/plus/video?id=%s' % video_id,
80             video_id, headers={'Origin': 'http://awaan.ae'})
81         info = self._parse_video_data(video_data, video_id, False)
82
83         embed_url = 'http://admin.mangomolo.com/analytics/index.php/customers/embed/video?' + compat_urllib_parse_urlencode({
84             'id': video_data['id'],
85             'user_id': video_data['user_id'],
86             'signature': video_data['signature'],
87             'countries': 'Q0M=',
88             'filter': 'DENY',
89         })
90         info.update({
91             '_type': 'url_transparent',
92             'url': embed_url,
93             'ie_key': 'MangomoloVideo',
94         })
95         return info
96
97
98 class AWAANLiveIE(AWAANBaseIE):
99     IE_NAME = 'awaan:live'
100     _VALID_URL = r'https?://(?:www\.)?(?:awaan|dcndigital)\.ae/(?:#/)?live/(?P<id>\d+)'
101     _TEST = {
102         'url': 'http://awaan.ae/live/6/dubai-tv',
103         'info_dict': {
104             'id': '6',
105             'ext': 'mp4',
106             'title': 're:Dubai Al Oula [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
107             'upload_date': '20150107',
108             'timestamp': 1420588800,
109         },
110         'params': {
111             # m3u8 download
112             'skip_download': True,
113         },
114     }
115
116     def _real_extract(self, url):
117         channel_id = self._match_id(url)
118
119         channel_data = self._download_json(
120             'http://admin.mangomolo.com/analytics/index.php/plus/getchanneldetails?channel_id=%s' % channel_id,
121             channel_id, headers={'Origin': 'http://awaan.ae'})
122         info = self._parse_video_data(channel_data, channel_id, True)
123
124         embed_url = 'http://admin.mangomolo.com/analytics/index.php/customers/embed/index?' + compat_urllib_parse_urlencode({
125             'id': base64.b64encode(channel_data['user_id'].encode()).decode(),
126             'channelid': base64.b64encode(channel_data['id'].encode()).decode(),
127             'signature': channel_data['signature'],
128             'countries': 'Q0M=',
129             'filter': 'DENY',
130         })
131         info.update({
132             '_type': 'url_transparent',
133             'url': embed_url,
134             'ie_key': 'MangomoloLive',
135         })
136         return info
137
138
139 class AWAANSeasonIE(InfoExtractor):
140     IE_NAME = 'awaan:season'
141     _VALID_URL = r'https?://(?:www\.)?(?:awaan|dcndigital)\.ae/(?:#/)?program/(?:(?P<show_id>\d+)|season/(?P<season_id>\d+))'
142     _TEST = {
143         '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',
144         'info_dict':
145         {
146             'id': '7910',
147             'title': 'محاضرات الشيخ الشعراوي',
148         },
149         'playlist_mincount': 27,
150     }
151
152     def _real_extract(self, url):
153         url, smuggled_data = unsmuggle_url(url, {})
154         show_id, season_id = re.match(self._VALID_URL, url).groups()
155
156         data = {}
157         if season_id:
158             data['season'] = season_id
159             show_id = smuggled_data.get('show_id')
160             if show_id is None:
161                 season = self._download_json(
162                     'http://admin.mangomolo.com/analytics/index.php/plus/season_info?id=%s' % season_id,
163                     season_id, headers={'Origin': 'http://awaan.ae'})
164                 show_id = season['id']
165         data['show_id'] = show_id
166         show = self._download_json(
167             'http://admin.mangomolo.com/analytics/index.php/plus/show',
168             show_id, data=urlencode_postdata(data), headers={
169                 'Origin': 'http://awaan.ae',
170                 'Content-Type': 'application/x-www-form-urlencoded'
171             })
172         if not season_id:
173             season_id = show['default_season']
174         for season in show['seasons']:
175             if season['id'] == season_id:
176                 title = season.get('title_en') or season['title_ar']
177
178                 entries = []
179                 for video in show['videos']:
180                     video_id = compat_str(video['id'])
181                     entries.append(self.url_result(
182                         'http://awaan.ae/media/%s' % video_id, 'AWAANVideo', video_id))
183
184                 return self.playlist_result(entries, season_id, title)