]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/hungama.py
[cleanup, docs] Misc cleanup
[yt-dlp.git] / yt_dlp / extractor / hungama.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8 int_or_none,
9 try_get,
10 urlencode_postdata,
11 )
12
13
14 class HungamaIE(InfoExtractor):
15 _VALID_URL = r'''(?x)
16 https?://
17 (?:www\.)?hungama\.com/
18 (?:
19 (?:video|movie)/[^/]+/|
20 tv-show/(?:[^/]+/){2}\d+/episode/[^/]+/
21 )
22 (?P<id>\d+)
23 '''
24 _TESTS = [{
25 'url': 'http://www.hungama.com/video/krishna-chants/39349649/',
26 'md5': 'a845a6d1ebd08d80c1035126d49bd6a0',
27 'info_dict': {
28 'id': '2931166',
29 'ext': 'mp4',
30 'title': 'Lucky Ali - Kitni Haseen Zindagi',
31 'track': 'Kitni Haseen Zindagi',
32 'artist': 'Lucky Ali',
33 'album': 'Aks',
34 'release_year': 2000,
35 }
36 }, {
37 'url': 'https://www.hungama.com/movie/kahaani-2/44129919/',
38 'only_matching': True,
39 }, {
40 'url': 'https://www.hungama.com/tv-show/padded-ki-pushup/season-1/44139461/episode/ep-02-training-sasu-pathlaag-karing/44139503/',
41 'only_matching': True,
42 }]
43
44 def _real_extract(self, url):
45 video_id = self._match_id(url)
46
47 webpage = self._download_webpage(url, video_id)
48
49 info = self._search_json_ld(webpage, video_id)
50
51 m3u8_url = self._download_json(
52 'https://www.hungama.com/index.php', video_id,
53 data=urlencode_postdata({'content_id': video_id}), headers={
54 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
55 'X-Requested-With': 'XMLHttpRequest',
56 }, query={
57 'c': 'common',
58 'm': 'get_video_mdn_url',
59 })['stream_url']
60
61 formats = self._extract_m3u8_formats(
62 m3u8_url, video_id, ext='mp4', entry_protocol='m3u8_native',
63 m3u8_id='hls')
64 self._sort_formats(formats)
65
66 info.update({
67 'id': video_id,
68 'formats': formats,
69 })
70 return info
71
72
73 class HungamaSongIE(InfoExtractor):
74 _VALID_URL = r'https?://(?:www\.)?hungama\.com/song/[^/]+/(?P<id>\d+)'
75 _TEST = {
76 'url': 'https://www.hungama.com/song/kitni-haseen-zindagi/2931166/',
77 'md5': 'd4a6a05a394ad0453a9bea3ca00e6024',
78 'info_dict': {
79 'id': '2931166',
80 'ext': 'mp3',
81 'title': 'Lucky Ali - Kitni Haseen Zindagi',
82 'track': 'Kitni Haseen Zindagi',
83 'artist': 'Lucky Ali',
84 'album': None,
85 'release_year': 2000,
86 }
87 }
88
89 def _real_extract(self, url):
90 audio_id = self._match_id(url)
91
92 data = self._download_json(
93 'https://www.hungama.com/audio-player-data/track/%s' % audio_id,
94 audio_id, query={'_country': 'IN'})[0]
95 track = data['song_name']
96 artist = data.get('singer_name')
97 formats = []
98 media_json = self._download_json(data.get('file') or data['preview_link'], audio_id)
99 media_url = try_get(media_json, lambda x: x['response']['media_url'], str)
100 media_type = try_get(media_json, lambda x: x['response']['type'], str)
101
102 if media_url:
103 formats.append({
104 'url': media_url,
105 'ext': media_type,
106 'vcodec': 'none',
107 'acodec': media_type,
108 })
109
110 title = '%s - %s' % (artist, track) if artist else track
111 thumbnail = data.get('img_src') or data.get('album_image')
112
113 return {
114 'id': audio_id,
115 'title': title,
116 'thumbnail': thumbnail,
117 'track': track,
118 'artist': artist,
119 'album': data.get('album_name') or None,
120 'release_year': int_or_none(data.get('date')),
121 'formats': formats,
122 }
123
124
125 class HungamaAlbumPlaylistIE(InfoExtractor):
126 _VALID_URL = r'https?://(?:www\.)?hungama\.com/(?:playlists|album)/[^/]+/(?P<id>\d+)'
127 _TESTS = [{
128 'url': 'https://www.hungama.com/album/bhuj-the-pride-of-india/69481490/',
129 'playlist_mincount': 7,
130 'info_dict': {
131 'id': '69481490',
132 },
133 }, {
134 'url': 'https://www.hungama.com/playlists/hindi-jan-to-june-2021/123063/',
135 'playlist_mincount': 50,
136 'info_dict': {
137 'id': '123063',
138 },
139 }]
140
141 def _real_extract(self, url):
142 video_id = self._match_id(url)
143 webpage = self._download_webpage(url, video_id)
144 ptrn = r'<meta[^>]+?property=[\"\']?music:song:url[\"\']?[^>]+?content=[\"\']?([^\"\']+)'
145 items = re.findall(ptrn, webpage)
146 entries = [self.url_result(item, ie=HungamaSongIE.ie_key()) for item in items]
147 return self.playlist_result(entries, video_id)