[youtube] Fix extraction.
[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|ca))/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         'url': 'https://einthusan.ca/movie/watch/4E9n/?lang=hindi',
41         'only_matching': True,
42     }]
43
44     # reversed from jsoncrypto.prototype.decrypt() in einthusan-PGMovieWatcher.js
45     def _decrypt(self, encrypted_data, video_id):
46         return self._parse_json(compat_b64decode((
47             encrypted_data[:10] + encrypted_data[-1] + encrypted_data[12:-1]
48         )).decode('utf-8'), video_id)
49
50     def _real_extract(self, url):
51         mobj = re.match(self._VALID_URL, url)
52         host = mobj.group('host')
53         video_id = mobj.group('id')
54
55         webpage = self._download_webpage(url, video_id)
56
57         title = self._html_search_regex(r'<h3>([^<]+)</h3>', webpage, 'title')
58
59         player_params = extract_attributes(self._search_regex(
60             r'(<section[^>]+id="UIVideoPlayer"[^>]+>)', webpage, 'player parameters'))
61
62         page_id = self._html_search_regex(
63             '<html[^>]+data-pageid="([^"]+)"', webpage, 'page ID')
64         video_data = self._download_json(
65             'https://%s/ajax/movie/watch/%s/' % (host, video_id), video_id,
66             data=urlencode_postdata({
67                 'xEvent': 'UIVideoPlayer.PingOutcome',
68                 'xJson': json.dumps({
69                     'EJOutcomes': player_params['data-ejpingables'],
70                     'NativeHLS': False
71                 }),
72                 'arcVersion': 3,
73                 'appVersion': 59,
74                 'gorilla.csrf.Token': page_id,
75             }))['Data']
76
77         if isinstance(video_data, compat_str) and video_data.startswith('/ratelimited/'):
78             raise ExtractorError(
79                 'Download rate reached. Please try again later.', expected=True)
80
81         ej_links = self._decrypt(video_data['EJLinks'], video_id)
82
83         formats = []
84
85         m3u8_url = ej_links.get('HLSLink')
86         if m3u8_url:
87             formats.extend(self._extract_m3u8_formats(
88                 m3u8_url, video_id, ext='mp4', entry_protocol='m3u8_native'))
89
90         mp4_url = ej_links.get('MP4Link')
91         if mp4_url:
92             formats.append({
93                 'url': mp4_url,
94             })
95
96         self._sort_formats(formats)
97
98         description = get_elements_by_class('synopsis', webpage)[0]
99         thumbnail = self._html_search_regex(
100             r'''<img[^>]+src=(["'])(?P<url>(?!\1).+?/moviecovers/(?!\1).+?)\1''',
101             webpage, 'thumbnail url', fatal=False, group='url')
102         if thumbnail is not None:
103             thumbnail = compat_urlparse.urljoin(url, thumbnail)
104
105         return {
106             'id': video_id,
107             'title': title,
108             'formats': formats,
109             'thumbnail': thumbnail,
110             'description': description,
111         }