[hungama] Fix code and extract more metadata (closes #18771)
[youtube-dl] / youtube_dl / extractor / hungama.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import int_or_none
6
7
8 class HungamaIE(InfoExtractor):
9     _VALID_URL = r'https?://(?:www\.)?hungama\.com/song/[^/]+/(?P<id>[0-9]+)'
10     _TEST = {
11         'url': 'https://www.hungama.com/song/kitni-haseen-zindagi/2931166/',
12         'md5': 'a845a6d1ebd08d80c1035126d49bd6a0',
13         'info_dict': {
14             'id': '2931166',
15             'ext': 'mp4',
16             'title': 'Lucky Ali - Kitni Haseen Zindagi',
17             'track': 'Kitni Haseen Zindagi',
18             'artist': 'Lucky Ali',
19             'album': 'Aks',
20             'release_year': 2000,
21         }
22     }
23
24     def _real_extract(self, url):
25         video_id = self._match_id(url)
26
27         data = self._download_json(
28             'https://www.hungama.com/audio-player-data/track/%s' % video_id,
29             video_id, query={'_country': 'IN'})[0]
30
31         track = data['song_name']
32         artist = data.get('singer_name')
33
34         m3u8_url = self._download_json(
35             data.get('file') or data['preview_link'],
36             video_id)['response']['media_url']
37
38         formats = self._extract_m3u8_formats(
39             m3u8_url, video_id, ext='mp4', entry_protocol='m3u8_native',
40             m3u8_id='hls')
41         self._sort_formats(formats)
42
43         title = '%s - %s' % (artist, track) if artist else track
44         thumbnail = data.get('img_src') or data.get('album_image')
45
46         return {
47             'id': video_id,
48             'title': title,
49             'thumbnail': thumbnail,
50             'track': track,
51             'artist': artist,
52             'album': data.get('album_name'),
53             'release_year': int_or_none(data.get('date')),
54             'formats': formats,
55         }