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