]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/zingmp3.py
[zingmp3:album] Style
[yt-dlp.git] / youtube_dl / extractor / zingmp3.py
CommitLineData
c66bdc48
DHS
1# coding=utf-8
2from __future__ import unicode_literals
3
4import re
5
6from .common import InfoExtractor
51156528 7from ..utils import ExtractorError
c66bdc48
DHS
8
9
10class ZingMp3BaseInfoExtractor(InfoExtractor):
11
51156528
S
12 def _extract_item(self, item):
13 error_message = item.find('./errormessage').text
14 if error_message:
15 raise ExtractorError(
16 '%s returned error: %s' % (self.IE_NAME, error_message),
17 expected=True)
18
c66bdc48
DHS
19 title = item.find('./title').text.strip()
20 source = item.find('./source').text
21 extension = item.attrib['type']
22 thumbnail = item.find('./backimage').text
23
24 return {
25 'title': title,
26 'url': source,
27 'ext': extension,
28 'thumbnail': thumbnail,
29 }
30
31 def _extract_player_xml(self, player_xml_url, id, playlist_title=None):
32 player_xml = self._download_xml(player_xml_url, id, 'Downloading Player XML')
c66bdc48
DHS
33 items = player_xml.findall('./item')
34
35 if len(items) == 1:
36 # one single song
37 data = self._extract_item(items[0])
38 data['id'] = id
39
40 return data
41 else:
42 # playlist of songs
43 entries = []
44
45 for i, item in enumerate(items, 1):
46 entry = self._extract_item(item)
47 entry['id'] = '%s-%d' % (id, i)
48 entries.append(entry)
49
50 return {
51 '_type': 'playlist',
52 'id': id,
53 'title': playlist_title,
54 'entries': entries,
55 }
56
57
58class ZingMp3SongIE(ZingMp3BaseInfoExtractor):
59 _VALID_URL = r'https?://mp3\.zing\.vn/bai-hat/(?P<slug>[^/]+)/(?P<song_id>\w+)\.html'
60 _TESTS = [{
61 'url': 'http://mp3.zing.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html',
4ffc3103 62 'md5': 'ead7ae13693b3205cbc89536a077daed',
c66bdc48
DHS
63 'info_dict': {
64 'id': 'ZWZB9WAB',
4ffc3103 65 'title': 'Xa Mãi Xa',
c66bdc48 66 'ext': 'mp3',
4ffc3103 67 'thumbnail': 're:^https?://.*\.jpg$',
c66bdc48 68 },
c66bdc48
DHS
69 }]
70 IE_NAME = 'zingmp3:song'
71 IE_DESC = 'mp3.zing.vn songs'
72
73 def _real_extract(self, url):
74 matched = re.match(self._VALID_URL, url)
75 slug = matched.group('slug')
76 song_id = matched.group('song_id')
77
4ffc3103
PH
78 webpage = self._download_webpage(
79 'http://mp3.zing.vn/bai-hat/%s/%s.html' % (slug, song_id), song_id)
c66bdc48 80
4ffc3103
PH
81 player_xml_url = self._search_regex(
82 r'&amp;xmlURL=(?P<xml_url>[^&]+)&', webpage, 'player xml url')
c66bdc48
DHS
83
84 return self._extract_player_xml(player_xml_url, song_id)
85
86
87class ZingMp3AlbumIE(ZingMp3BaseInfoExtractor):
97ae4d16 88 _VALID_URL = r'https?://mp3\.zing\.vn/(?:album|playlist)/(?P<slug>[^/]+)/(?P<album_id>\w+)\.html'
43abd799
S
89 _TESTS = [{
90 'url': 'http://mp3.zing.vn/album/Lau-Dai-Tinh-Ai-Bang-Kieu-Minh-Tuyet/ZWZBWDAF.html',
91 'info_dict': {
92 '_type': 'playlist',
93 'id': 'ZWZBWDAF',
94 'title': 'Lâu Đài Tình Ái - Bằng Kiều ft. Minh Tuyết | Album 320 lossless',
c66bdc48 95 },
43abd799
S
96 'playlist_count': 10,
97 }, {
98 'url': 'http://mp3.zing.vn/playlist/Duong-Hong-Loan-apollobee/IWCAACCB.html',
99 'only_matching': True,
100 }]
c66bdc48
DHS
101 IE_NAME = 'zingmp3:album'
102 IE_DESC = 'mp3.zing.vn albums'
103
104 def _real_extract(self, url):
105 matched = re.match(self._VALID_URL, url)
106 slug = matched.group('slug')
107 album_id = matched.group('album_id')
108
4ffc3103
PH
109 webpage = self._download_webpage(
110 'http://mp3.zing.vn/album/%s/%s.html' % (slug, album_id), album_id)
111 player_xml_url = self._search_regex(
112 r'&amp;xmlURL=(?P<xml_url>[^&]+)&', webpage, 'player xml url')
c66bdc48 113
4ffc3103
PH
114 return self._extract_player_xml(
115 player_xml_url, album_id,
116 playlist_title=self._og_search_title(webpage))