]>
Commit | Line | Data |
---|---|---|
dcdb292f | 1 | # coding: utf-8 |
c66bdc48 DHS |
2 | from __future__ import unicode_literals |
3 | ||
4 | import re | |
5 | ||
6 | from .common import InfoExtractor | |
3d47ee0a RA |
7 | from ..utils import ( |
8 | ExtractorError, | |
9 | int_or_none, | |
10 | update_url_query, | |
11 | ) | |
c66bdc48 DHS |
12 | |
13 | ||
14 | class ZingMp3BaseInfoExtractor(InfoExtractor): | |
15 | ||
3d47ee0a RA |
16 | def _extract_item(self, item, page_type, fatal=True): |
17 | error_message = item.get('msg') | |
51156528 | 18 | if error_message: |
8119597d S |
19 | if not fatal: |
20 | return | |
51156528 S |
21 | raise ExtractorError( |
22 | '%s returned error: %s' % (self.IE_NAME, error_message), | |
23 | expected=True) | |
24 | ||
3d47ee0a RA |
25 | formats = [] |
26 | for quality, source_url in zip(item.get('qualities') or item.get('quality', []), item.get('source_list') or item.get('source', [])): | |
27 | if not source_url or source_url == 'require vip': | |
28 | continue | |
29 | if not re.match(r'https?://', source_url): | |
30 | source_url = '//' + source_url | |
31 | source_url = self._proto_relative_url(source_url, 'http:') | |
32 | quality_num = int_or_none(quality) | |
33 | f = { | |
34 | 'format_id': quality, | |
35 | 'url': source_url, | |
36 | } | |
37 | if page_type == 'video': | |
38 | f.update({ | |
39 | 'height': quality_num, | |
40 | 'ext': 'mp4', | |
41 | }) | |
42 | else: | |
43 | f.update({ | |
44 | 'abr': quality_num, | |
45 | 'ext': 'mp3', | |
46 | }) | |
47 | formats.append(f) | |
48 | ||
49 | cover = item.get('cover') | |
c66bdc48 DHS |
50 | |
51 | return { | |
3d47ee0a RA |
52 | 'title': (item.get('name') or item.get('title')).strip(), |
53 | 'formats': formats, | |
54 | 'thumbnail': 'http:/' + cover if cover else None, | |
55 | 'artist': item.get('artist'), | |
c66bdc48 DHS |
56 | } |
57 | ||
3d47ee0a RA |
58 | def _extract_player_json(self, player_json_url, id, page_type, playlist_title=None): |
59 | player_json = self._download_json(player_json_url, id, 'Downloading Player JSON') | |
60 | items = player_json['data'] | |
61 | if 'item' in items: | |
62 | items = items['item'] | |
c66bdc48 DHS |
63 | |
64 | if len(items) == 1: | |
65 | # one single song | |
3d47ee0a | 66 | data = self._extract_item(items[0], page_type) |
c66bdc48 DHS |
67 | data['id'] = id |
68 | ||
69 | return data | |
70 | else: | |
71 | # playlist of songs | |
72 | entries = [] | |
73 | ||
74 | for i, item in enumerate(items, 1): | |
3d47ee0a | 75 | entry = self._extract_item(item, page_type, fatal=False) |
f790c43f S |
76 | if not entry: |
77 | continue | |
c66bdc48 DHS |
78 | entry['id'] = '%s-%d' % (id, i) |
79 | entries.append(entry) | |
80 | ||
81 | return { | |
82 | '_type': 'playlist', | |
83 | 'id': id, | |
84 | 'title': playlist_title, | |
85 | 'entries': entries, | |
86 | } | |
87 | ||
88 | ||
3d47ee0a RA |
89 | class ZingMp3IE(ZingMp3BaseInfoExtractor): |
90 | _VALID_URL = r'https?://mp3\.zing\.vn/(?:bai-hat|album|playlist|video-clip)/[^/]+/(?P<id>\w+)\.html' | |
c66bdc48 DHS |
91 | _TESTS = [{ |
92 | 'url': 'http://mp3.zing.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html', | |
4ffc3103 | 93 | 'md5': 'ead7ae13693b3205cbc89536a077daed', |
c66bdc48 DHS |
94 | 'info_dict': { |
95 | 'id': 'ZWZB9WAB', | |
4ffc3103 | 96 | 'title': 'Xa Mãi Xa', |
c66bdc48 | 97 | 'ext': 'mp3', |
ec85ded8 | 98 | 'thumbnail': r're:^https?://.*\.jpg$', |
c66bdc48 | 99 | }, |
3d47ee0a RA |
100 | }, { |
101 | 'url': 'http://mp3.zing.vn/video-clip/Let-It-Go-Frozen-OST-Sungha-Jung/ZW6BAEA0.html', | |
102 | 'md5': '870295a9cd8045c0e15663565902618d', | |
103 | 'info_dict': { | |
104 | 'id': 'ZW6BAEA0', | |
105 | 'title': 'Let It Go (Frozen OST)', | |
106 | 'ext': 'mp4', | |
107 | }, | |
108 | }, { | |
43abd799 S |
109 | 'url': 'http://mp3.zing.vn/album/Lau-Dai-Tinh-Ai-Bang-Kieu-Minh-Tuyet/ZWZBWDAF.html', |
110 | 'info_dict': { | |
111 | '_type': 'playlist', | |
112 | 'id': 'ZWZBWDAF', | |
3d47ee0a | 113 | 'title': 'Lâu Đài Tình Ái - Bằng Kiều,Minh Tuyết | Album 320 lossless', |
c66bdc48 | 114 | }, |
43abd799 | 115 | 'playlist_count': 10, |
3d47ee0a | 116 | 'skip': 'removed at the request of the owner', |
43abd799 S |
117 | }, { |
118 | 'url': 'http://mp3.zing.vn/playlist/Duong-Hong-Loan-apollobee/IWCAACCB.html', | |
119 | 'only_matching': True, | |
120 | }] | |
3d47ee0a RA |
121 | IE_NAME = 'zingmp3' |
122 | IE_DESC = 'mp3.zing.vn' | |
c66bdc48 DHS |
123 | |
124 | def _real_extract(self, url): | |
3d47ee0a RA |
125 | page_id = self._match_id(url) |
126 | ||
127 | webpage = self._download_webpage(url, page_id) | |
128 | ||
129 | player_json_url = self._search_regex([ | |
130 | r'data-xml="([^"]+)', | |
131 | r'&xmlURL=([^&]+)&' | |
132 | ], webpage, 'player xml url') | |
133 | ||
134 | playlist_title = None | |
135 | page_type = self._search_regex(r'/(?:html5)?xml/([^/-]+)', player_json_url, 'page type') | |
136 | if page_type == 'video': | |
137 | player_json_url = update_url_query(player_json_url, {'format': 'json'}) | |
138 | else: | |
139 | player_json_url = player_json_url.replace('/xml/', '/html5xml/') | |
140 | if page_type == 'album': | |
141 | playlist_title = self._og_search_title(webpage) | |
142 | ||
143 | return self._extract_player_json(player_json_url, page_id, page_type, playlist_title) |