[einthusan] Fix extraction (closes #11416)
[youtube-dl] / youtube_dl / extractor / einthusan.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import base64
5 import json
6
7 from .common import InfoExtractor
8 from ..compat import (
9     compat_urlparse,
10     compat_str,
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>[0-9]+)'
22     _TEST = {
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
34     # reversed from jsoncrypto.prototype.decrypt() in einthusan-PGMovieWatcher.js
35     def _decrypt(self, encrypted_data, video_id):
36         return self._parse_json(base64.b64decode((
37             encrypted_data[:10] + encrypted_data[-1] + encrypted_data[12:-1]
38         ).encode('ascii')).decode('utf-8'), video_id)
39
40     def _real_extract(self, url):
41         video_id = self._match_id(url)
42
43         webpage = self._download_webpage(url, video_id)
44
45         title = self._html_search_regex(r'<h3>([^<]+)</h3>', webpage, 'title')
46
47         player_params = extract_attributes(self._search_regex(
48             r'(<section[^>]+id="UIVideoPlayer"[^>]+>)', webpage, 'player parameters'))
49
50         page_id = self._html_search_regex(
51             '<html[^>]+data-pageid="([^"]+)"', webpage, 'page ID')
52         video_data = self._download_json(
53             'https://einthusan.tv/ajax/movie/watch/%s/' % video_id, video_id,
54             data=urlencode_postdata({
55                 'xEvent': 'UIVideoPlayer.PingOutcome',
56                 'xJson': json.dumps({
57                     'EJOutcomes': player_params['data-ejpingables'],
58                     'NativeHLS': False
59                 }),
60                 'arcVersion': 3,
61                 'appVersion': 59,
62                 'gorilla.csrf.Token': page_id,
63             }))['Data']
64
65         if isinstance(video_data, compat_str) and video_data.startswith('/ratelimited/'):
66             raise ExtractorError(
67                 'Download rate reached. Please try again later.', expected=True)
68
69         ej_links = self._decrypt(video_data['EJLinks'], video_id)
70
71         formats = []
72
73         m3u8_url = ej_links.get('HLSLink')
74         if m3u8_url:
75             formats.extend(self._extract_m3u8_formats(
76                 m3u8_url, video_id, ext='mp4', entry_protocol='m3u8_native'))
77
78         mp4_url = ej_links.get('MP4Link')
79         if mp4_url:
80             formats.append({
81                 'url': mp4_url,
82             })
83
84         self._sort_formats(formats)
85
86         description = get_elements_by_class('synopsis', webpage)[0]
87         thumbnail = self._html_search_regex(
88             r'''<img[^>]+src=(["'])(?P<url>(?!\1).+?/moviecovers/(?!\1).+?)\1''',
89             webpage, 'thumbnail url', fatal=False, group='url')
90         if thumbnail is not None:
91             thumbnail = compat_urlparse.urljoin(url, thumbnail)
92
93         return {
94             'id': video_id,
95             'title': title,
96             'formats': formats,
97             'thumbnail': thumbnail,
98             'description': description,
99         }