zingmp3.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. import functools
  2. import hashlib
  3. import hmac
  4. import json
  5. import urllib.parse
  6. from .common import InfoExtractor
  7. from ..utils import (
  8. OnDemandPagedList,
  9. int_or_none,
  10. traverse_obj,
  11. urljoin,
  12. )
  13. class ZingMp3BaseIE(InfoExtractor):
  14. _VALID_URL_TMPL = r'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<type>(?:%s))/[^/?#]+/(?P<id>\w+)(?:\.html|\?)'
  15. _GEO_COUNTRIES = ['VN']
  16. _DOMAIN = 'https://zingmp3.vn'
  17. _PER_PAGE = 50
  18. _API_SLUGS = {
  19. # Audio/video
  20. 'bai-hat': '/api/v2/page/get/song',
  21. 'embed': '/api/v2/page/get/song',
  22. 'video-clip': '/api/v2/page/get/video',
  23. 'lyric': '/api/v2/lyric/get/lyric',
  24. 'song-streaming': '/api/v2/song/get/streaming',
  25. # Playlist
  26. 'playlist': '/api/v2/page/get/playlist',
  27. 'album': '/api/v2/page/get/playlist',
  28. # Chart
  29. 'zing-chart': '/api/v2/page/get/chart-home',
  30. 'zing-chart-tuan': '/api/v2/page/get/week-chart',
  31. 'moi-phat-hanh': '/api/v2/page/get/newrelease-chart',
  32. 'the-loai-video': '/api/v2/video/get/list',
  33. # User
  34. 'info-artist': '/api/v2/page/get/artist',
  35. 'user-list-song': '/api/v2/song/get/list',
  36. 'user-list-video': '/api/v2/video/get/list',
  37. }
  38. def _api_url(self, url_type, params):
  39. api_slug = self._API_SLUGS[url_type]
  40. params.update({'ctime': '1'})
  41. sha256 = hashlib.sha256(
  42. ''.join(f'{k}={v}' for k, v in sorted(params.items())).encode()).hexdigest()
  43. data = {
  44. **params,
  45. 'apiKey': '88265e23d4284f25963e6eedac8fbfa3',
  46. 'sig': hmac.new(
  47. b'2aa2d1c561e809b267f3638c4a307aab', f'{api_slug}{sha256}'.encode(), hashlib.sha512).hexdigest(),
  48. }
  49. return f'{self._DOMAIN}{api_slug}?{urllib.parse.urlencode(data)}'
  50. def _call_api(self, url_type, params, display_id=None, **kwargs):
  51. resp = self._download_json(
  52. self._api_url(url_type, params), display_id or params.get('id'),
  53. note=f'Downloading {url_type} JSON metadata', **kwargs)
  54. return (resp or {}).get('data') or {}
  55. def _real_initialize(self):
  56. if not self._cookies_passed:
  57. self._request_webpage(
  58. self._api_url('bai-hat', {'id': ''}), None, note='Updating cookies')
  59. def _parse_items(self, items):
  60. for url in traverse_obj(items, (..., 'link')) or []:
  61. yield self.url_result(urljoin(self._DOMAIN, url))
  62. class ZingMp3IE(ZingMp3BaseIE):
  63. _VALID_URL = ZingMp3BaseIE._VALID_URL_TMPL % 'bai-hat|video-clip|embed'
  64. IE_NAME = 'zingmp3'
  65. IE_DESC = 'zingmp3.vn'
  66. _TESTS = [{
  67. 'url': 'https://mp3.zing.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html',
  68. 'md5': 'ead7ae13693b3205cbc89536a077daed',
  69. 'info_dict': {
  70. 'id': 'ZWZB9WAB',
  71. 'title': 'Xa Mãi Xa',
  72. 'ext': 'mp3',
  73. 'thumbnail': r're:^https?://.+\.jpg',
  74. 'subtitles': {
  75. 'origin': [{
  76. 'ext': 'lrc',
  77. }]
  78. },
  79. 'duration': 255,
  80. 'track': 'Xa Mãi Xa',
  81. 'artist': 'Bảo Thy',
  82. 'album': 'Special Album',
  83. 'album_artist': 'Bảo Thy',
  84. },
  85. }, {
  86. 'url': 'https://zingmp3.vn/video-clip/Suong-Hoa-Dua-Loi-K-ICM-RYO/ZO8ZF7C7.html',
  87. 'md5': '3c2081e79471a2f4a3edd90b70b185ea',
  88. 'info_dict': {
  89. 'id': 'ZO8ZF7C7',
  90. 'title': 'Sương Hoa Đưa Lối',
  91. 'ext': 'mp4',
  92. 'thumbnail': r're:^https?://.+\.jpg',
  93. 'duration': 207,
  94. 'track': 'Sương Hoa Đưa Lối',
  95. 'artist': 'K-ICM, RYO',
  96. 'album': 'Sương Hoa Đưa Lối (Single)',
  97. 'album_artist': 'K-ICM, RYO',
  98. },
  99. }, {
  100. 'url': 'https://zingmp3.vn/bai-hat/Nguoi-Yeu-Toi-Lanh-Lung-Sat-Da-Mr-Siro/ZZ6IW7OU.html',
  101. 'md5': '3e9f7a9bd0d965573dbff8d7c68b629d',
  102. 'info_dict': {
  103. 'id': 'ZZ6IW7OU',
  104. 'title': 'Người Yêu Tôi Lạnh Lùng Sắt Đá',
  105. 'ext': 'mp3',
  106. 'thumbnail': r're:^https?://.+\.jpg',
  107. 'duration': 303,
  108. 'track': 'Người Yêu Tôi Lạnh Lùng Sắt Đá',
  109. 'artist': 'Mr. Siro',
  110. 'album': 'Người Yêu Tôi Lạnh Lùng Sắt Đá (Single)',
  111. 'album_artist': 'Mr. Siro',
  112. },
  113. }, {
  114. 'url': 'https://zingmp3.vn/embed/song/ZWZEI76B?start=false',
  115. 'only_matching': True,
  116. }, {
  117. 'url': 'https://zingmp3.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html',
  118. 'only_matching': True,
  119. }]
  120. def _real_extract(self, url):
  121. song_id, url_type = self._match_valid_url(url).group('id', 'type')
  122. item = self._call_api(url_type, {'id': song_id})
  123. item_id = item.get('encodeId') or song_id
  124. if url_type == 'video-clip':
  125. source = item.get('streaming')
  126. source['mp4'] = self._download_json(
  127. 'http://api.mp3.zing.vn/api/mobile/video/getvideoinfo', item_id,
  128. query={'requestdata': json.dumps({'id': item_id})},
  129. note='Downloading mp4 JSON metadata').get('source')
  130. else:
  131. source = self._call_api('song-streaming', {'id': item_id})
  132. formats = []
  133. for k, v in (source or {}).items():
  134. if not v or v == 'VIP':
  135. continue
  136. if k not in ('mp4', 'hls'):
  137. formats.append({
  138. 'ext': 'mp3',
  139. 'format_id': k,
  140. 'tbr': int_or_none(k),
  141. 'url': self._proto_relative_url(v),
  142. 'vcodec': 'none',
  143. })
  144. continue
  145. for res, video_url in v.items():
  146. if not video_url:
  147. continue
  148. if k == 'hls':
  149. formats.extend(self._extract_m3u8_formats(video_url, item_id, 'mp4', m3u8_id=k, fatal=False))
  150. continue
  151. formats.append({
  152. 'format_id': f'mp4-{res}',
  153. 'url': video_url,
  154. 'height': int_or_none(res),
  155. })
  156. if not formats and item.get('msg') == 'Sorry, this content is not available in your country.':
  157. self.raise_geo_restricted(countries=self._GEO_COUNTRIES, metadata_available=True)
  158. lyric = item.get('lyric') or self._call_api('lyric', {'id': item_id}, fatal=False).get('file')
  159. return {
  160. 'id': item_id,
  161. 'title': traverse_obj(item, 'title', 'alias'),
  162. 'thumbnail': traverse_obj(item, 'thumbnail', 'thumbnailM'),
  163. 'duration': int_or_none(item.get('duration')),
  164. 'track': traverse_obj(item, 'title', 'alias'),
  165. 'artist': traverse_obj(item, 'artistsNames', 'artists_names'),
  166. 'album': traverse_obj(item, ('album', ('name', 'title')), get_all=False),
  167. 'album_artist': traverse_obj(item, ('album', ('artistsNames', 'artists_names')), get_all=False),
  168. 'formats': formats,
  169. 'subtitles': {'origin': [{'url': lyric}]} if lyric else None,
  170. }
  171. class ZingMp3AlbumIE(ZingMp3BaseIE):
  172. _VALID_URL = ZingMp3BaseIE._VALID_URL_TMPL % 'album|playlist'
  173. _TESTS = [{
  174. 'url': 'http://mp3.zing.vn/album/Lau-Dai-Tinh-Ai-Bang-Kieu-Minh-Tuyet/ZWZBWDAF.html',
  175. 'info_dict': {
  176. 'id': 'ZWZBWDAF',
  177. 'title': 'Lâu Đài Tình Ái',
  178. },
  179. 'playlist_mincount': 9,
  180. }, {
  181. 'url': 'https://zingmp3.vn/album/Nhung-Bai-Hat-Hay-Nhat-Cua-Mr-Siro-Mr-Siro/ZWZAEZZD.html',
  182. 'info_dict': {
  183. 'id': 'ZWZAEZZD',
  184. 'title': 'Những Bài Hát Hay Nhất Của Mr. Siro',
  185. },
  186. 'playlist_mincount': 49,
  187. }, {
  188. 'url': 'http://mp3.zing.vn/playlist/Duong-Hong-Loan-apollobee/IWCAACCB.html',
  189. 'only_matching': True,
  190. }, {
  191. 'url': 'https://zingmp3.vn/album/Lau-Dai-Tinh-Ai-Bang-Kieu-Minh-Tuyet/ZWZBWDAF.html',
  192. 'only_matching': True,
  193. }]
  194. IE_NAME = 'zingmp3:album'
  195. def _real_extract(self, url):
  196. song_id, url_type = self._match_valid_url(url).group('id', 'type')
  197. data = self._call_api(url_type, {'id': song_id})
  198. return self.playlist_result(
  199. self._parse_items(traverse_obj(data, ('song', 'items'))),
  200. traverse_obj(data, 'id', 'encodeId'), traverse_obj(data, 'name', 'title'))
  201. class ZingMp3ChartHomeIE(ZingMp3BaseIE):
  202. _VALID_URL = r'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<id>(?:zing-chart|moi-phat-hanh))/?(?:[#?]|$)'
  203. _TESTS = [{
  204. 'url': 'https://zingmp3.vn/zing-chart',
  205. 'info_dict': {
  206. 'id': 'zing-chart',
  207. },
  208. 'playlist_mincount': 100,
  209. }, {
  210. 'url': 'https://zingmp3.vn/moi-phat-hanh',
  211. 'info_dict': {
  212. 'id': 'moi-phat-hanh',
  213. },
  214. 'playlist_mincount': 100,
  215. }]
  216. IE_NAME = 'zingmp3:chart-home'
  217. def _real_extract(self, url):
  218. url_type = self._match_id(url)
  219. data = self._call_api(url_type, {'id': url_type})
  220. items = traverse_obj(data, ('RTChart', 'items') if url_type == 'zing-chart' else 'items')
  221. return self.playlist_result(self._parse_items(items), url_type)
  222. class ZingMp3WeekChartIE(ZingMp3BaseIE):
  223. _VALID_URL = ZingMp3BaseIE._VALID_URL_TMPL % 'zing-chart-tuan'
  224. IE_NAME = 'zingmp3:week-chart'
  225. _TESTS = [{
  226. 'url': 'https://zingmp3.vn/zing-chart-tuan/Bai-hat-Viet-Nam/IWZ9Z08I.html',
  227. 'info_dict': {
  228. 'id': 'IWZ9Z08I',
  229. 'title': 'zing-chart-vn',
  230. },
  231. 'playlist_mincount': 10,
  232. }, {
  233. 'url': 'https://zingmp3.vn/zing-chart-tuan/Bai-hat-US-UK/IWZ9Z0BW.html',
  234. 'info_dict': {
  235. 'id': 'IWZ9Z0BW',
  236. 'title': 'zing-chart-us',
  237. },
  238. 'playlist_mincount': 10,
  239. }, {
  240. 'url': 'https://zingmp3.vn/zing-chart-tuan/Bai-hat-KPop/IWZ9Z0BO.html',
  241. 'info_dict': {
  242. 'id': 'IWZ9Z0BO',
  243. 'title': 'zing-chart-korea',
  244. },
  245. 'playlist_mincount': 10,
  246. }]
  247. def _real_extract(self, url):
  248. song_id, url_type = self._match_valid_url(url).group('id', 'type')
  249. data = self._call_api(url_type, {'id': song_id})
  250. return self.playlist_result(
  251. self._parse_items(data['items']), song_id, f'zing-chart-{data.get("country", "")}')
  252. class ZingMp3ChartMusicVideoIE(ZingMp3BaseIE):
  253. _VALID_URL = r'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<type>the-loai-video)/(?P<regions>[^/]+)/(?P<id>[^\.]+)'
  254. IE_NAME = 'zingmp3:chart-music-video'
  255. _TESTS = [{
  256. 'url': 'https://zingmp3.vn/the-loai-video/Viet-Nam/IWZ9Z08I.html',
  257. 'info_dict': {
  258. 'id': 'IWZ9Z08I',
  259. 'title': 'the-loai-video_Viet-Nam',
  260. },
  261. 'playlist_mincount': 400,
  262. }, {
  263. 'url': 'https://zingmp3.vn/the-loai-video/Au-My/IWZ9Z08O.html',
  264. 'info_dict': {
  265. 'id': 'IWZ9Z08O',
  266. 'title': 'the-loai-video_Au-My',
  267. },
  268. 'playlist_mincount': 40,
  269. }, {
  270. 'url': 'https://zingmp3.vn/the-loai-video/Han-Quoc/IWZ9Z08W.html',
  271. 'info_dict': {
  272. 'id': 'IWZ9Z08W',
  273. 'title': 'the-loai-video_Han-Quoc',
  274. },
  275. 'playlist_mincount': 30,
  276. }, {
  277. 'url': 'https://zingmp3.vn/the-loai-video/Khong-Loi/IWZ9Z086.html',
  278. 'info_dict': {
  279. 'id': 'IWZ9Z086',
  280. 'title': 'the-loai-video_Khong-Loi',
  281. },
  282. 'playlist_mincount': 10,
  283. }]
  284. def _fetch_page(self, song_id, url_type, page):
  285. return self._parse_items(self._call_api(url_type, {
  286. 'id': song_id,
  287. 'type': 'genre',
  288. 'page': page + 1,
  289. 'count': self._PER_PAGE
  290. }).get('items'))
  291. def _real_extract(self, url):
  292. song_id, regions, url_type = self._match_valid_url(url).group('id', 'regions', 'type')
  293. return self.playlist_result(
  294. OnDemandPagedList(functools.partial(self._fetch_page, song_id, url_type), self._PER_PAGE),
  295. song_id, f'{url_type}_{regions}')
  296. class ZingMp3UserIE(ZingMp3BaseIE):
  297. _VALID_URL = r'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<user>[^/]+)/(?P<type>bai-hat|single|album|video)/?(?:[?#]|$)'
  298. IE_NAME = 'zingmp3:user'
  299. _TESTS = [{
  300. 'url': 'https://zingmp3.vn/Mr-Siro/bai-hat',
  301. 'info_dict': {
  302. 'id': 'IWZ98609',
  303. 'title': 'Mr. Siro - bai-hat',
  304. 'description': 'md5:85ab29bd7b21725c12bf76fd1d6922e5',
  305. },
  306. 'playlist_mincount': 91,
  307. }, {
  308. 'url': 'https://zingmp3.vn/Mr-Siro/album',
  309. 'info_dict': {
  310. 'id': 'IWZ98609',
  311. 'title': 'Mr. Siro - album',
  312. 'description': 'md5:85ab29bd7b21725c12bf76fd1d6922e5',
  313. },
  314. 'playlist_mincount': 3,
  315. }, {
  316. 'url': 'https://zingmp3.vn/Mr-Siro/single',
  317. 'info_dict': {
  318. 'id': 'IWZ98609',
  319. 'title': 'Mr. Siro - single',
  320. 'description': 'md5:85ab29bd7b21725c12bf76fd1d6922e5',
  321. },
  322. 'playlist_mincount': 20,
  323. }, {
  324. 'url': 'https://zingmp3.vn/Mr-Siro/video',
  325. 'info_dict': {
  326. 'id': 'IWZ98609',
  327. 'title': 'Mr. Siro - video',
  328. 'description': 'md5:85ab29bd7b21725c12bf76fd1d6922e5',
  329. },
  330. 'playlist_mincount': 15,
  331. }]
  332. def _fetch_page(self, user_id, url_type, page):
  333. url_type = 'user-list-song' if url_type == 'bai-hat' else 'user-list-video'
  334. return self._parse_items(self._call_api(url_type, {
  335. 'id': user_id,
  336. 'type': 'artist',
  337. 'page': page + 1,
  338. 'count': self._PER_PAGE
  339. }, query={'sort': 'new', 'sectionId': 'aSong'}).get('items'))
  340. def _real_extract(self, url):
  341. user_alias, url_type = self._match_valid_url(url).group('user', 'type')
  342. if not url_type:
  343. url_type = 'bai-hat'
  344. user_info = self._call_api('info-artist', {}, user_alias, query={'alias': user_alias})
  345. if url_type in ('bai-hat', 'video'):
  346. entries = OnDemandPagedList(
  347. functools.partial(self._fetch_page, user_info['id'], url_type), self._PER_PAGE)
  348. else:
  349. entries = self._parse_items(traverse_obj(user_info, (
  350. 'sections', lambda _, v: v['link'] == f'/{user_alias}/{url_type}', 'items', ...)))
  351. return self.playlist_result(
  352. entries, user_info['id'], f'{user_info.get("name")} - {url_type}', user_info.get('biography'))