[youtube] Skip unsupported adaptive stream type (#18804)
[youtube-dl] / youtube_dl / extractor / einthusan.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5
6 from .common import InfoExtractor
7 from ..compat import (
8     compat_b64decode,
9     compat_str,
10     compat_urlparse,
11 )
12 from ..utils import (
13     extract_attributes,
14     ExtractorError,
15     get_elements_by_class,
16     urlencode_postdata,
17 )
18
19
20 class EinthusanIE(InfoExtractor):
21     _VALID_URL = r'https?://einthusan\.tv/movie/watch/(?P<id>[^/?#&]+)'
22     _TESTS = [{
23         'url': 'https://einthusan.tv/movie/watch/9097/',
24         'md5': 'ff0f7f2065031b8a2cf13a933731c035',
25         'info_dict': {
26             'id': '9097',
27             'ext': 'mp4',
28             'title': 'Ae Dil Hai Mushkil',
29             'description': 'md5:33ef934c82a671a94652a9b4e54d931b',
30             'thumbnail': r're:^https?://.*\.jpg$',
31         }
32     }, {
33         'url': 'https://einthusan.tv/movie/watch/51MZ/?lang=hindi',
34         'only_matching': True,
35     }]
36
37     # reversed from jsoncrypto.prototype.decrypt() in einthusan-PGMovieWatcher.js
38     def _decrypt(self, encrypted_data, video_id):
39         return self._parse_json(compat_b64decode((
40             encrypted_data[:10] + encrypted_data[-1] + encrypted_data[12:-1]
41         )).decode('utf-8'), video_id)
42
43     def _real_extract(self, url):
44         video_id = self._match_id(url)
45
46         webpage = self._download_webpage(url, video_id)
47
48         title = self._html_search_regex(r'<h3>([^<]+)</h3>', webpage, 'title')
49
50         player_params = extract_attributes(self._search_regex(
51             r'(<section[^>]+id="UIVideoPlayer"[^>]+>)', webpage, 'player parameters'))
52
53         page_id = self._html_search_regex(
54             '<html[^>]+data-pageid="([^"]+)"', webpage, 'page ID')
55         video_data = self._download_json(
56             'https://einthusan.tv/ajax/movie/watch/%s/' % video_id, video_id,
57             data=urlencode_postdata({
58                 'xEvent': 'UIVideoPlayer.PingOutcome',
59                 'xJson': json.dumps({
60                     'EJOutcomes': player_params['data-ejpingables'],
61                     'NativeHLS': False
62                 }),
63                 'arcVersion': 3,
64                 'appVersion': 59,
65                 'gorilla.csrf.Token': page_id,
66             }))['Data']
67
68         if isinstance(video_data, compat_str) and video_data.startswith('/ratelimited/'):
69             raise ExtractorError(
70                 'Download rate reached. Please try again later.', expected=True)
71
72         ej_links = self._decrypt(video_data['EJLinks'], video_id)
73
74         formats = []
75
76         m3u8_url = ej_links.get('HLSLink')
77         if m3u8_url:
78             formats.extend(self._extract_m3u8_formats(
79                 m3u8_url, video_id, ext='mp4', entry_protocol='m3u8_native'))
80
81         mp4_url = ej_links.get('MP4Link')
82         if mp4_url:
83             formats.append({
84                 'url': mp4_url,
85             })
86
87         self._sort_formats(formats)
88
89         description = get_elements_by_class('synopsis', webpage)[0]
90         thumbnail = self._html_search_regex(
91             r'''<img[^>]+src=(["'])(?P<url>(?!\1).+?/moviecovers/(?!\1).+?)\1''',
92             webpage, 'thumbnail url', fatal=False, group='url')
93         if thumbnail is not None:
94             thumbnail = compat_urlparse.urljoin(url, thumbnail)
95
96         return {
97             'id': video_id,
98             'title': title,
99             'formats': formats,
100             'thumbnail': thumbnail,
101             'description': description,
102         }