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