| 
									
										
										
										
											2016-10-02 13:39:18 +02:00
										 |  |  | # coding: utf-8 | 
					
						
							| 
									
										
										
										
											2014-11-24 03:25:47 +07:00
										 |  |  | from __future__ import unicode_literals | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import re | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from .common import InfoExtractor | 
					
						
							| 
									
										
										
										
											2016-08-21 14:09:18 +01:00
										 |  |  | from ..utils import ( | 
					
						
							|  |  |  |     ExtractorError, | 
					
						
							|  |  |  |     int_or_none, | 
					
						
							|  |  |  |     update_url_query, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2014-11-24 03:25:47 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ZingMp3BaseInfoExtractor(InfoExtractor): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-21 14:09:18 +01:00
										 |  |  |     def _extract_item(self, item, page_type, fatal=True): | 
					
						
							|  |  |  |         error_message = item.get('msg') | 
					
						
							| 
									
										
										
										
											2015-05-11 21:31:36 +06:00
										 |  |  |         if error_message: | 
					
						
							| 
									
										
										
										
											2015-10-10 22:08:38 +06:00
										 |  |  |             if not fatal: | 
					
						
							|  |  |  |                 return | 
					
						
							| 
									
										
										
										
											2015-05-11 21:31:36 +06:00
										 |  |  |             raise ExtractorError( | 
					
						
							|  |  |  |                 '%s returned error: %s' % (self.IE_NAME, error_message), | 
					
						
							|  |  |  |                 expected=True) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-21 14:09:18 +01:00
										 |  |  |         formats = [] | 
					
						
							|  |  |  |         for quality, source_url in zip(item.get('qualities') or item.get('quality', []), item.get('source_list') or item.get('source', [])): | 
					
						
							|  |  |  |             if not source_url or source_url == 'require vip': | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             if not re.match(r'https?://', source_url): | 
					
						
							|  |  |  |                 source_url = '//' + source_url | 
					
						
							|  |  |  |             source_url = self._proto_relative_url(source_url, 'http:') | 
					
						
							|  |  |  |             quality_num = int_or_none(quality) | 
					
						
							|  |  |  |             f = { | 
					
						
							|  |  |  |                 'format_id': quality, | 
					
						
							|  |  |  |                 'url': source_url, | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if page_type == 'video': | 
					
						
							|  |  |  |                 f.update({ | 
					
						
							|  |  |  |                     'height': quality_num, | 
					
						
							|  |  |  |                     'ext': 'mp4', | 
					
						
							|  |  |  |                 }) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 f.update({ | 
					
						
							|  |  |  |                     'abr': quality_num, | 
					
						
							|  |  |  |                     'ext': 'mp3', | 
					
						
							|  |  |  |                 }) | 
					
						
							|  |  |  |             formats.append(f) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         cover = item.get('cover') | 
					
						
							| 
									
										
										
										
											2014-11-24 03:25:47 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return { | 
					
						
							| 
									
										
										
										
											2016-08-21 14:09:18 +01:00
										 |  |  |             'title': (item.get('name') or item.get('title')).strip(), | 
					
						
							|  |  |  |             'formats': formats, | 
					
						
							|  |  |  |             'thumbnail': 'http:/' + cover if cover else None, | 
					
						
							|  |  |  |             'artist': item.get('artist'), | 
					
						
							| 
									
										
										
										
											2014-11-24 03:25:47 +07:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-21 14:09:18 +01:00
										 |  |  |     def _extract_player_json(self, player_json_url, id, page_type, playlist_title=None): | 
					
						
							|  |  |  |         player_json = self._download_json(player_json_url, id, 'Downloading Player JSON') | 
					
						
							|  |  |  |         items = player_json['data'] | 
					
						
							|  |  |  |         if 'item' in items: | 
					
						
							|  |  |  |             items = items['item'] | 
					
						
							| 
									
										
										
										
											2014-11-24 03:25:47 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if len(items) == 1: | 
					
						
							|  |  |  |             # one single song | 
					
						
							| 
									
										
										
										
											2016-08-21 14:09:18 +01:00
										 |  |  |             data = self._extract_item(items[0], page_type) | 
					
						
							| 
									
										
										
										
											2014-11-24 03:25:47 +07:00
										 |  |  |             data['id'] = id | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return data | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             # playlist of songs | 
					
						
							|  |  |  |             entries = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             for i, item in enumerate(items, 1): | 
					
						
							| 
									
										
										
										
											2016-08-21 14:09:18 +01:00
										 |  |  |                 entry = self._extract_item(item, page_type, fatal=False) | 
					
						
							| 
									
										
										
										
											2015-10-10 22:09:21 +06:00
										 |  |  |                 if not entry: | 
					
						
							|  |  |  |                     continue | 
					
						
							| 
									
										
										
										
											2014-11-24 03:25:47 +07:00
										 |  |  |                 entry['id'] = '%s-%d' % (id, i) | 
					
						
							|  |  |  |                 entries.append(entry) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return { | 
					
						
							|  |  |  |                 '_type': 'playlist', | 
					
						
							|  |  |  |                 'id': id, | 
					
						
							|  |  |  |                 'title': playlist_title, | 
					
						
							|  |  |  |                 'entries': entries, | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-21 14:09:18 +01:00
										 |  |  | class ZingMp3IE(ZingMp3BaseInfoExtractor): | 
					
						
							|  |  |  |     _VALID_URL = r'https?://mp3\.zing\.vn/(?:bai-hat|album|playlist|video-clip)/[^/]+/(?P<id>\w+)\.html' | 
					
						
							| 
									
										
										
										
											2014-11-24 03:25:47 +07:00
										 |  |  |     _TESTS = [{ | 
					
						
							|  |  |  |         'url': 'http://mp3.zing.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html', | 
					
						
							| 
									
										
										
										
											2014-11-23 22:00:25 +01:00
										 |  |  |         'md5': 'ead7ae13693b3205cbc89536a077daed', | 
					
						
							| 
									
										
										
										
											2014-11-24 03:25:47 +07:00
										 |  |  |         'info_dict': { | 
					
						
							|  |  |  |             'id': 'ZWZB9WAB', | 
					
						
							| 
									
										
										
										
											2014-11-23 22:00:25 +01:00
										 |  |  |             'title': 'Xa Mãi Xa', | 
					
						
							| 
									
										
										
										
											2014-11-24 03:25:47 +07:00
										 |  |  |             'ext': 'mp3', | 
					
						
							| 
									
										
										
										
											2014-11-23 22:00:25 +01:00
										 |  |  |             'thumbnail': 're:^https?://.*\.jpg$', | 
					
						
							| 
									
										
										
										
											2014-11-24 03:25:47 +07:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2016-08-21 14:09:18 +01:00
										 |  |  |     }, { | 
					
						
							|  |  |  |         'url': 'http://mp3.zing.vn/video-clip/Let-It-Go-Frozen-OST-Sungha-Jung/ZW6BAEA0.html', | 
					
						
							|  |  |  |         'md5': '870295a9cd8045c0e15663565902618d', | 
					
						
							|  |  |  |         'info_dict': { | 
					
						
							|  |  |  |             'id': 'ZW6BAEA0', | 
					
						
							|  |  |  |             'title': 'Let It Go (Frozen OST)', | 
					
						
							|  |  |  |             'ext': 'mp4', | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |     }, { | 
					
						
							| 
									
										
										
										
											2015-10-10 22:05:01 +06:00
										 |  |  |         'url': 'http://mp3.zing.vn/album/Lau-Dai-Tinh-Ai-Bang-Kieu-Minh-Tuyet/ZWZBWDAF.html', | 
					
						
							|  |  |  |         'info_dict': { | 
					
						
							|  |  |  |             '_type': 'playlist', | 
					
						
							|  |  |  |             'id': 'ZWZBWDAF', | 
					
						
							| 
									
										
										
										
											2016-08-21 14:09:18 +01:00
										 |  |  |             'title': 'Lâu Đài Tình Ái - Bằng Kiều,Minh Tuyết | Album 320 lossless', | 
					
						
							| 
									
										
										
										
											2014-11-24 03:25:47 +07:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2015-10-10 22:05:01 +06:00
										 |  |  |         'playlist_count': 10, | 
					
						
							| 
									
										
										
										
											2016-08-21 14:09:18 +01:00
										 |  |  |         'skip': 'removed at the request of the owner', | 
					
						
							| 
									
										
										
										
											2015-10-10 22:05:01 +06:00
										 |  |  |     }, { | 
					
						
							|  |  |  |         'url': 'http://mp3.zing.vn/playlist/Duong-Hong-Loan-apollobee/IWCAACCB.html', | 
					
						
							|  |  |  |         'only_matching': True, | 
					
						
							|  |  |  |     }] | 
					
						
							| 
									
										
										
										
											2016-08-21 14:09:18 +01:00
										 |  |  |     IE_NAME = 'zingmp3' | 
					
						
							|  |  |  |     IE_DESC = 'mp3.zing.vn' | 
					
						
							| 
									
										
										
										
											2014-11-24 03:25:47 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def _real_extract(self, url): | 
					
						
							| 
									
										
										
										
											2016-08-21 14:09:18 +01:00
										 |  |  |         page_id = self._match_id(url) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         webpage = self._download_webpage(url, page_id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         player_json_url = self._search_regex([ | 
					
						
							|  |  |  |             r'data-xml="([^"]+)', | 
					
						
							|  |  |  |             r'&xmlURL=([^&]+)&' | 
					
						
							|  |  |  |         ], webpage, 'player xml url') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         playlist_title = None | 
					
						
							|  |  |  |         page_type = self._search_regex(r'/(?:html5)?xml/([^/-]+)', player_json_url, 'page type') | 
					
						
							|  |  |  |         if page_type == 'video': | 
					
						
							|  |  |  |             player_json_url = update_url_query(player_json_url, {'format': 'json'}) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             player_json_url = player_json_url.replace('/xml/', '/html5xml/') | 
					
						
							|  |  |  |             if page_type == 'album': | 
					
						
							|  |  |  |                 playlist_title = self._og_search_title(webpage) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return self._extract_player_json(player_json_url, page_id, page_type, playlist_title) |