zingmp3.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. import hashlib
  2. import hmac
  3. import itertools
  4. import json
  5. import urllib.parse
  6. from .common import InfoExtractor
  7. from ..utils import (
  8. ExtractorError,
  9. int_or_none,
  10. join_nonempty,
  11. try_call,
  12. url_or_none,
  13. urljoin,
  14. )
  15. from ..utils.traversal import traverse_obj
  16. class ZingMp3BaseIE(InfoExtractor):
  17. _VALID_URL_TMPL = r'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<type>(?:%s))/[^/?#]+/(?P<id>\w+)(?:\.html|\?)'
  18. _GEO_COUNTRIES = ['VN']
  19. _DOMAIN = 'https://zingmp3.vn'
  20. _PER_PAGE = 50
  21. _API_SLUGS = {
  22. # Audio/video
  23. 'bai-hat': '/api/v2/page/get/song',
  24. 'embed': '/api/v2/page/get/song',
  25. 'video-clip': '/api/v2/page/get/video',
  26. 'lyric': '/api/v2/lyric/get/lyric',
  27. 'song-streaming': '/api/v2/song/get/streaming',
  28. 'liveradio': '/api/v2/livestream/get/info',
  29. 'eps': '/api/v2/page/get/podcast-episode',
  30. 'episode-streaming': '/api/v2/podcast/episode/get/streaming',
  31. # Playlist
  32. 'playlist': '/api/v2/page/get/playlist',
  33. 'album': '/api/v2/page/get/playlist',
  34. 'pgr': '/api/v2/page/get/podcast-program',
  35. 'pgr-list': '/api/v2/podcast/episode/get/list',
  36. 'cgr': '/api/v2/page/get/podcast-category',
  37. 'cgr-list': '/api/v2/podcast/program/get/list-by-cate',
  38. 'cgrs': '/api/v2/page/get/podcast-categories',
  39. # Chart
  40. 'zing-chart': '/api/v2/page/get/chart-home',
  41. 'zing-chart-tuan': '/api/v2/page/get/week-chart',
  42. 'moi-phat-hanh': '/api/v2/page/get/newrelease-chart',
  43. 'the-loai-video': '/api/v2/video/get/list',
  44. # User
  45. 'info-artist': '/api/v2/page/get/artist',
  46. 'user-list-song': '/api/v2/song/get/list',
  47. 'user-list-video': '/api/v2/video/get/list',
  48. 'hub': '/api/v2/page/get/hub-detail',
  49. 'new-release': '/api/v2/chart/get/new-release',
  50. 'top100': '/api/v2/page/get/top-100',
  51. 'podcast-new': '/api/v2/podcast/program/get/list-by-type',
  52. 'top-podcast': '/api/v2/podcast/program/get/top-episode',
  53. }
  54. def _api_url(self, url_type, params):
  55. api_slug = self._API_SLUGS[url_type]
  56. params.update({'ctime': '1'})
  57. sha256 = hashlib.sha256(
  58. ''.join(f'{k}={v}' for k, v in sorted(params.items())).encode()).hexdigest()
  59. data = {
  60. **params,
  61. 'apiKey': 'X5BM3w8N7MKozC0B85o4KMlzLZKhV00y',
  62. 'sig': hmac.new(b'acOrvUS15XRW2o9JksiK1KgQ6Vbds8ZW',
  63. f'{api_slug}{sha256}'.encode(), hashlib.sha512).hexdigest(),
  64. }
  65. return f'{self._DOMAIN}{api_slug}?{urllib.parse.urlencode(data)}'
  66. def _call_api(self, url_type, params, display_id=None, **kwargs):
  67. resp = self._download_json(
  68. self._api_url(url_type, params), display_id or params.get('id'),
  69. note=f'Downloading {url_type} JSON metadata', **kwargs)
  70. return (resp or {}).get('data') or {}
  71. def _real_initialize(self):
  72. if not self._cookies_passed:
  73. self._request_webpage(
  74. self._api_url('bai-hat', {'id': ''}), None, note='Updating cookies')
  75. def _parse_items(self, items):
  76. for url in traverse_obj(items, (..., 'link')) or []:
  77. yield self.url_result(urljoin(self._DOMAIN, url))
  78. def _fetch_page(self, id_, url_type, page):
  79. raise NotImplementedError('This method must be implemented by subclasses')
  80. def _paged_list(self, _id, url_type):
  81. count = 0
  82. for page in itertools.count(1):
  83. data = self._fetch_page(_id, url_type, page)
  84. entries = list(self._parse_items(data.get('items')))
  85. count += len(entries)
  86. yield from entries
  87. if not data.get('hasMore') or try_call(lambda: count > data['total']):
  88. break
  89. class ZingMp3IE(ZingMp3BaseIE):
  90. _VALID_URL = ZingMp3BaseIE._VALID_URL_TMPL % 'bai-hat|video-clip|embed|eps'
  91. IE_NAME = 'zingmp3'
  92. IE_DESC = 'zingmp3.vn'
  93. _TESTS = [{
  94. 'url': 'https://mp3.zing.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html',
  95. 'md5': 'ead7ae13693b3205cbc89536a077daed',
  96. 'info_dict': {
  97. 'id': 'ZWZB9WAB',
  98. 'title': 'Xa Mãi Xa',
  99. 'ext': 'mp3',
  100. 'thumbnail': r're:^https?://.+\.jpg',
  101. 'subtitles': {
  102. 'origin': [{
  103. 'ext': 'lrc',
  104. }],
  105. },
  106. 'duration': 255,
  107. 'track': 'Xa Mãi Xa',
  108. 'artist': 'Bảo Thy',
  109. 'album': 'Special Album',
  110. 'album_artist': 'Bảo Thy',
  111. },
  112. }, {
  113. 'url': 'https://zingmp3.vn/video-clip/Suong-Hoa-Dua-Loi-K-ICM-RYO/ZO8ZF7C7.html',
  114. 'md5': '92c6e7a019f06b4682a6c35ae5785fab',
  115. 'info_dict': {
  116. 'id': 'ZO8ZF7C7',
  117. 'title': 'Sương Hoa Đưa Lối',
  118. 'ext': 'mp4',
  119. 'thumbnail': r're:^https?://.+\.jpg',
  120. 'duration': 207,
  121. 'track': 'Sương Hoa Đưa Lối',
  122. 'artist': 'K-ICM, RYO',
  123. 'album': 'Sương Hoa Đưa Lối (Single)',
  124. 'album_artist': 'K-ICM, RYO',
  125. },
  126. }, {
  127. 'url': 'https://zingmp3.vn/bai-hat/Nguoi-Yeu-Toi-Lanh-Lung-Sat-Da-Mr-Siro/ZZ6IW7OU.html',
  128. 'md5': '3e9f7a9bd0d965573dbff8d7c68b629d',
  129. 'info_dict': {
  130. 'id': 'ZZ6IW7OU',
  131. 'title': 'Người Yêu Tôi Lạnh Lùng Sắt Đá',
  132. 'ext': 'mp3',
  133. 'thumbnail': r're:^https?://.+\.jpg',
  134. 'duration': 303,
  135. 'track': 'Người Yêu Tôi Lạnh Lùng Sắt Đá',
  136. 'artist': 'Mr. Siro',
  137. 'album': 'Người Yêu Tôi Lạnh Lùng Sắt Đá (Single)',
  138. 'album_artist': 'Mr. Siro',
  139. },
  140. }, {
  141. 'url': 'https://zingmp3.vn/eps/Cham-x-Ban-Noi-Goi-La-Nha/ZZD9ACWI.html',
  142. 'md5': 'd52f9f63e2631e004e4f15188eedcf80',
  143. 'info_dict': {
  144. 'id': 'ZZD9ACWI',
  145. 'title': 'Chạm x Bạn - Nơi Gọi Là Nhà',
  146. 'ext': 'mp3',
  147. 'duration': 3716,
  148. 'thumbnail': r're:^https?://.+\.jpg',
  149. 'track': 'Chạm x Bạn - Nơi Gọi Là Nhà',
  150. 'artist': 'On Air',
  151. 'album': 'Top Podcast',
  152. 'album_artist': 'On Air',
  153. },
  154. }, {
  155. 'url': 'https://zingmp3.vn/embed/song/ZWZEI76B?start=false',
  156. 'only_matching': True,
  157. }, {
  158. 'url': 'https://zingmp3.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html',
  159. 'only_matching': True,
  160. }]
  161. def _real_extract(self, url):
  162. song_id, url_type = self._match_valid_url(url).group('id', 'type')
  163. item = self._call_api(url_type, {'id': song_id})
  164. item_id = item.get('encodeId') or song_id
  165. if url_type == 'video-clip':
  166. source = item.get('streaming')
  167. source['mp4'] = self._download_json(
  168. 'http://api.mp3.zing.vn/api/mobile/video/getvideoinfo', item_id,
  169. query={'requestdata': json.dumps({'id': item_id})},
  170. note='Downloading mp4 JSON metadata').get('source')
  171. elif url_type == 'eps':
  172. source = self._call_api('episode-streaming', {'id': item_id})
  173. else:
  174. source = self._call_api('song-streaming', {'id': item_id})
  175. formats = []
  176. for k, v in (source or {}).items():
  177. if not v or v == 'VIP':
  178. continue
  179. if k not in ('mp4', 'hls'):
  180. formats.append({
  181. 'ext': 'mp3',
  182. 'format_id': k,
  183. 'tbr': int_or_none(k),
  184. 'url': self._proto_relative_url(v),
  185. 'vcodec': 'none',
  186. })
  187. continue
  188. for res, video_url in v.items():
  189. if not video_url:
  190. continue
  191. if k == 'hls':
  192. formats.extend(self._extract_m3u8_formats(video_url, item_id, 'mp4', m3u8_id=k, fatal=False))
  193. continue
  194. formats.append({
  195. 'format_id': f'mp4-{res}',
  196. 'url': video_url,
  197. 'height': int_or_none(res),
  198. })
  199. if not formats:
  200. if item.get('msg') == 'Sorry, this content is not available in your country.':
  201. self.raise_geo_restricted(countries=self._GEO_COUNTRIES, metadata_available=True)
  202. else:
  203. self.raise_no_formats('The song is only for VIP accounts.')
  204. lyric = item.get('lyric') or self._call_api('lyric', {'id': item_id}, fatal=False).get('file')
  205. return {
  206. 'id': item_id,
  207. 'title': traverse_obj(item, 'title', 'alias'),
  208. 'thumbnail': traverse_obj(item, 'thumbnail', 'thumbnailM'),
  209. 'duration': int_or_none(item.get('duration')),
  210. 'track': traverse_obj(item, 'title', 'alias'),
  211. 'artist': traverse_obj(item, 'artistsNames', 'artists_names', ('artists', 0, 'name')),
  212. 'album': traverse_obj(item, ('album', ('name', 'title')), ('genres', 0, 'name'), get_all=False),
  213. 'album_artist': traverse_obj(item, ('album', ('artistsNames', 'artists_names')),
  214. ('artists', 0, 'name'), get_all=False),
  215. 'formats': formats,
  216. 'subtitles': {'origin': [{'url': lyric}]} if lyric else None,
  217. }
  218. class ZingMp3AlbumIE(ZingMp3BaseIE):
  219. _VALID_URL = ZingMp3BaseIE._VALID_URL_TMPL % 'album|playlist'
  220. _TESTS = [{
  221. 'url': 'https://zingmp3.vn/album/Ca-Phe-Quan-Quen-Hoang-Dung-My-Anh-Da-LAB-Thinh-Suy/ZOC7WUZC.html',
  222. 'info_dict': {
  223. 'id': 'ZOC7WUZC',
  224. 'title': 'Cà Phê Quán Quen',
  225. },
  226. 'playlist_mincount': 10,
  227. }, {
  228. 'url': 'https://zingmp3.vn/album/Nhung-Bai-Hat-Hay-Nhat-Cua-Mr-Siro-Mr-Siro/ZWZAEZZD.html',
  229. 'info_dict': {
  230. 'id': 'ZWZAEZZD',
  231. 'title': 'Những Bài Hát Hay Nhất Của Mr. Siro',
  232. },
  233. 'playlist_mincount': 20,
  234. }, {
  235. 'url': 'http://mp3.zing.vn/playlist/Duong-Hong-Loan-apollobee/IWCAACCB.html',
  236. 'only_matching': True,
  237. }, {
  238. 'url': 'https://zingmp3.vn/album/Lau-Dai-Tinh-Ai-Bang-Kieu-Minh-Tuyet/ZWZBWDAF.html',
  239. 'only_matching': True,
  240. }]
  241. IE_NAME = 'zingmp3:album'
  242. def _real_extract(self, url):
  243. song_id, url_type = self._match_valid_url(url).group('id', 'type')
  244. data = self._call_api(url_type, {'id': song_id})
  245. return self.playlist_result(
  246. self._parse_items(traverse_obj(data, ('song', 'items'))),
  247. traverse_obj(data, 'id', 'encodeId'), traverse_obj(data, 'name', 'title'))
  248. class ZingMp3ChartHomeIE(ZingMp3BaseIE):
  249. _VALID_URL = r'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<id>(?:zing-chart|moi-phat-hanh|top100|podcast-discover))/?(?:[#?]|$)'
  250. _TESTS = [{
  251. 'url': 'https://zingmp3.vn/zing-chart',
  252. 'info_dict': {
  253. 'id': 'zing-chart',
  254. },
  255. 'playlist_mincount': 100,
  256. }, {
  257. 'url': 'https://zingmp3.vn/moi-phat-hanh',
  258. 'info_dict': {
  259. 'id': 'moi-phat-hanh',
  260. },
  261. 'playlist_mincount': 100,
  262. }, {
  263. 'url': 'https://zingmp3.vn/top100',
  264. 'info_dict': {
  265. 'id': 'top100',
  266. },
  267. 'playlist_mincount': 50,
  268. }, {
  269. 'url': 'https://zingmp3.vn/podcast-discover',
  270. 'info_dict': {
  271. 'id': 'podcast-discover',
  272. },
  273. 'playlist_mincount': 4,
  274. }]
  275. IE_NAME = 'zingmp3:chart-home'
  276. def _real_extract(self, url):
  277. url_type = self._match_id(url)
  278. params = {'id': url_type}
  279. if url_type == 'podcast-discover':
  280. params['type'] = 'discover'
  281. data = self._call_api(url_type, params)
  282. items = []
  283. if url_type == 'top100':
  284. items.extend(traverse_obj(data, (..., 'items', ..., {dict})))
  285. elif url_type == 'zing-chart':
  286. items.extend(traverse_obj(data, ('RTChart', 'items', ..., {dict})))
  287. else:
  288. items.extend(traverse_obj(data, ('items', ..., {dict})))
  289. return self.playlist_result(self._parse_items(items), url_type)
  290. class ZingMp3WeekChartIE(ZingMp3BaseIE):
  291. _VALID_URL = ZingMp3BaseIE._VALID_URL_TMPL % 'zing-chart-tuan'
  292. IE_NAME = 'zingmp3:week-chart'
  293. _TESTS = [{
  294. 'url': 'https://zingmp3.vn/zing-chart-tuan/Bai-hat-Viet-Nam/IWZ9Z08I.html',
  295. 'info_dict': {
  296. 'id': 'IWZ9Z08I',
  297. 'title': 'zing-chart-vn',
  298. },
  299. 'playlist_mincount': 10,
  300. }, {
  301. 'url': 'https://zingmp3.vn/zing-chart-tuan/Bai-hat-US-UK/IWZ9Z0BW.html',
  302. 'info_dict': {
  303. 'id': 'IWZ9Z0BW',
  304. 'title': 'zing-chart-us',
  305. },
  306. 'playlist_mincount': 10,
  307. }, {
  308. 'url': 'https://zingmp3.vn/zing-chart-tuan/Bai-hat-KPop/IWZ9Z0BO.html',
  309. 'info_dict': {
  310. 'id': 'IWZ9Z0BO',
  311. 'title': 'zing-chart-korea',
  312. },
  313. 'playlist_mincount': 10,
  314. }]
  315. def _real_extract(self, url):
  316. song_id, url_type = self._match_valid_url(url).group('id', 'type')
  317. data = self._call_api(url_type, {'id': song_id})
  318. return self.playlist_result(
  319. self._parse_items(data['items']), song_id, f'zing-chart-{data.get("country", "")}')
  320. class ZingMp3ChartMusicVideoIE(ZingMp3BaseIE):
  321. _VALID_URL = r'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<type>the-loai-video)/(?P<regions>[^/]+)/(?P<id>[^\.]+)'
  322. IE_NAME = 'zingmp3:chart-music-video'
  323. _TESTS = [{
  324. 'url': 'https://zingmp3.vn/the-loai-video/Viet-Nam/IWZ9Z08I.html',
  325. 'info_dict': {
  326. 'id': 'IWZ9Z08I',
  327. 'title': 'the-loai-video_Viet-Nam',
  328. },
  329. 'playlist_mincount': 400,
  330. }, {
  331. 'url': 'https://zingmp3.vn/the-loai-video/Au-My/IWZ9Z08O.html',
  332. 'info_dict': {
  333. 'id': 'IWZ9Z08O',
  334. 'title': 'the-loai-video_Au-My',
  335. },
  336. 'playlist_mincount': 40,
  337. }, {
  338. 'url': 'https://zingmp3.vn/the-loai-video/Han-Quoc/IWZ9Z08W.html',
  339. 'info_dict': {
  340. 'id': 'IWZ9Z08W',
  341. 'title': 'the-loai-video_Han-Quoc',
  342. },
  343. 'playlist_mincount': 30,
  344. }, {
  345. 'url': 'https://zingmp3.vn/the-loai-video/Khong-Loi/IWZ9Z086.html',
  346. 'info_dict': {
  347. 'id': 'IWZ9Z086',
  348. 'title': 'the-loai-video_Khong-Loi',
  349. },
  350. 'playlist_mincount': 1,
  351. }]
  352. def _fetch_page(self, song_id, url_type, page):
  353. return self._call_api(url_type, {
  354. 'id': song_id,
  355. 'type': 'genre',
  356. 'page': page,
  357. 'count': self._PER_PAGE,
  358. })
  359. def _real_extract(self, url):
  360. song_id, regions, url_type = self._match_valid_url(url).group('id', 'regions', 'type')
  361. return self.playlist_result(self._paged_list(song_id, url_type), song_id, f'{url_type}_{regions}')
  362. class ZingMp3UserIE(ZingMp3BaseIE):
  363. _VALID_URL = r'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<user>[^/]+)/(?P<type>bai-hat|single|album|video|song)/?(?:[?#]|$)'
  364. IE_NAME = 'zingmp3:user'
  365. _TESTS = [{
  366. 'url': 'https://zingmp3.vn/Mr-Siro/bai-hat',
  367. 'info_dict': {
  368. 'id': 'IWZ98609',
  369. 'title': 'Mr. Siro - bai-hat',
  370. 'description': 'md5:5bdcf45e955dc1b8d7f518f322ffef36',
  371. },
  372. 'playlist_mincount': 91,
  373. }, {
  374. 'url': 'https://zingmp3.vn/Mr-Siro/album',
  375. 'info_dict': {
  376. 'id': 'IWZ98609',
  377. 'title': 'Mr. Siro - album',
  378. 'description': 'md5:5bdcf45e955dc1b8d7f518f322ffef36',
  379. },
  380. 'playlist_mincount': 3,
  381. }, {
  382. 'url': 'https://zingmp3.vn/Mr-Siro/single',
  383. 'info_dict': {
  384. 'id': 'IWZ98609',
  385. 'title': 'Mr. Siro - single',
  386. 'description': 'md5:5bdcf45e955dc1b8d7f518f322ffef36',
  387. },
  388. 'playlist_mincount': 20,
  389. }, {
  390. 'url': 'https://zingmp3.vn/Mr-Siro/video',
  391. 'info_dict': {
  392. 'id': 'IWZ98609',
  393. 'title': 'Mr. Siro - video',
  394. 'description': 'md5:5bdcf45e955dc1b8d7f518f322ffef36',
  395. },
  396. 'playlist_mincount': 15,
  397. }, {
  398. 'url': 'https://zingmp3.vn/new-release/song',
  399. 'info_dict': {
  400. 'id': 'new-release-song',
  401. },
  402. 'playlist_mincount': 50,
  403. }, {
  404. 'url': 'https://zingmp3.vn/new-release/album',
  405. 'info_dict': {
  406. 'id': 'new-release-album',
  407. },
  408. 'playlist_mincount': 20,
  409. }]
  410. def _fetch_page(self, user_id, url_type, page):
  411. url_type = 'user-list-song' if url_type == 'bai-hat' else 'user-list-video'
  412. return self._call_api(url_type, {
  413. 'id': user_id,
  414. 'type': 'artist',
  415. 'page': page,
  416. 'count': self._PER_PAGE,
  417. })
  418. def _real_extract(self, url):
  419. alias, url_type = self._match_valid_url(url).group('user', 'type')
  420. if not url_type:
  421. url_type = 'bai-hat'
  422. user_info = self._call_api('info-artist', {}, alias, query={'alias': alias})
  423. # Handle for new-release
  424. if alias == 'new-release' and url_type in ('song', 'album'):
  425. _id = f'{alias}-{url_type}'
  426. return self.playlist_result(self._parse_items(
  427. self._call_api('new-release', params={'type': url_type}, display_id=_id)), _id)
  428. else:
  429. # Handle for user/artist
  430. if url_type in ('bai-hat', 'video'):
  431. entries = self._paged_list(user_info['id'], url_type)
  432. else:
  433. section_id = 'aAlbum' if url_type == 'album' else 'aSingle'
  434. entries = self._parse_items(traverse_obj(user_info, (
  435. 'sections', lambda _, v: v['sectionId'] == section_id, 'items', ...)))
  436. return self.playlist_result(
  437. entries, user_info['id'], join_nonempty(user_info.get('name'), url_type, delim=' - '),
  438. user_info.get('biography'))
  439. class ZingMp3HubIE(ZingMp3BaseIE):
  440. IE_NAME = 'zingmp3:hub'
  441. _VALID_URL = r'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<type>hub)/(?P<regions>[^/]+)/(?P<id>[^\.]+)'
  442. _TESTS = [{
  443. 'url': 'https://zingmp3.vn/hub/Nhac-Moi/IWZ9Z0CA.html',
  444. 'info_dict': {
  445. 'id': 'IWZ9Z0CA',
  446. 'title': 'BXH Nhạc Mới',
  447. 'description': 'md5:1cc31b68a6f746427b07b2756c22a558',
  448. },
  449. 'playlist_mincount': 20,
  450. }, {
  451. 'url': 'https://zingmp3.vn/hub/Nhac-Viet/IWZ9Z087.html',
  452. 'info_dict': {
  453. 'id': 'IWZ9Z087',
  454. 'title': 'Nhạc Việt',
  455. 'description': 'md5:acc976c8bdde64d5c6ee4a92c39f7a77',
  456. },
  457. 'playlist_mincount': 30,
  458. }]
  459. def _real_extract(self, url):
  460. song_id, regions, url_type = self._match_valid_url(url).group('id', 'regions', 'type')
  461. hub_detail = self._call_api(url_type, {'id': song_id})
  462. entries = self._parse_items(traverse_obj(hub_detail, (
  463. 'sections', lambda _, v: v['sectionId'] == 'hub', 'items', ...)))
  464. return self.playlist_result(
  465. entries, song_id, hub_detail.get('title'), hub_detail.get('description'))
  466. class ZingMp3LiveRadioIE(ZingMp3BaseIE):
  467. IE_NAME = 'zingmp3:liveradio'
  468. _VALID_URL = r'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<type>(?:liveradio))/(?P<id>\w+)(?:\.html|\?)'
  469. _TESTS = [{
  470. 'url': 'https://zingmp3.vn/liveradio/IWZ979UB.html',
  471. 'info_dict': {
  472. 'id': 'IWZ979UB',
  473. 'title': r're:^V\-POP',
  474. 'description': 'md5:aa857f8a91dc9ce69e862a809e4bdc10',
  475. 'ext': 'mp4',
  476. 'view_count': int,
  477. 'thumbnail': r're:^https?://.*\.jpg',
  478. 'like_count': int,
  479. 'live_status': 'is_live',
  480. },
  481. 'params': {
  482. 'skip_download': True,
  483. },
  484. }, {
  485. 'url': 'https://zingmp3.vn/liveradio/IWZ97CWB.html',
  486. 'info_dict': {
  487. 'id': 'IWZ97CWB',
  488. 'title': r're:^Live\s247',
  489. 'description': 'md5:d41d8cd98f00b204e9800998ecf8427e',
  490. 'ext': 'm4a',
  491. 'view_count': int,
  492. 'thumbnail': r're:^https?://.*\.jpg',
  493. 'like_count': int,
  494. 'live_status': 'is_live',
  495. },
  496. 'params': {
  497. 'skip_download': True,
  498. },
  499. }]
  500. def _real_extract(self, url):
  501. url_type, live_radio_id = self._match_valid_url(url).group('type', 'id')
  502. info = self._call_api(url_type, {'id': live_radio_id})
  503. manifest_url = info.get('streaming')
  504. if not manifest_url:
  505. raise ExtractorError('This radio is offline.', expected=True)
  506. fmts, subtitles = self._extract_m3u8_formats_and_subtitles(manifest_url, live_radio_id, fatal=False)
  507. return {
  508. 'id': live_radio_id,
  509. 'is_live': True,
  510. 'formats': fmts,
  511. 'subtitles': subtitles,
  512. **traverse_obj(info, {
  513. 'title': 'title',
  514. 'thumbnail': (('thumbnail', 'thumbnailM', 'thumbnailV', 'thumbnailH'), {url_or_none}),
  515. 'view_count': ('activeUsers', {int_or_none}),
  516. 'like_count': ('totalReaction', {int_or_none}),
  517. 'description': 'description',
  518. }, get_all=False),
  519. }
  520. class ZingMp3PodcastEpisodeIE(ZingMp3BaseIE):
  521. IE_NAME = 'zingmp3:podcast-episode'
  522. _VALID_URL = ZingMp3BaseIE._VALID_URL_TMPL % 'pgr|cgr'
  523. _TESTS = [{
  524. 'url': 'https://zingmp3.vn/pgr/Nhac-Moi-Moi-Ngay/68Z9W66B.html',
  525. 'info_dict': {
  526. 'id': '68Z9W66B',
  527. 'title': 'Nhạc Mới Mỗi Ngày',
  528. 'description': 'md5:2875dfa951f8e5356742f1610cf20691',
  529. },
  530. 'playlist_mincount': 20,
  531. }, {
  532. 'url': 'https://zingmp3.vn/cgr/Am-nhac/IWZ980AO.html',
  533. 'info_dict': {
  534. 'id': 'IWZ980AO',
  535. 'title': 'Âm nhạc',
  536. },
  537. 'playlist_mincount': 2,
  538. }]
  539. def _fetch_page(self, eps_id, url_type, page):
  540. return self._call_api(url_type, {
  541. 'id': eps_id,
  542. 'page': page,
  543. 'count': self._PER_PAGE,
  544. })
  545. def _real_extract(self, url):
  546. podcast_id, url_type = self._match_valid_url(url).group('id', 'type')
  547. podcast_info = self._call_api(url_type, {'id': podcast_id})
  548. entries = self._paged_list(podcast_id, 'pgr-list' if url_type == 'pgr' else 'cgr-list')
  549. return self.playlist_result(
  550. entries, podcast_id, podcast_info.get('title'), podcast_info.get('description'))
  551. class ZingMp3PodcastIE(ZingMp3BaseIE):
  552. IE_NAME = 'zingmp3:podcast'
  553. _VALID_URL = r'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<id>(?:cgr|top-podcast|podcast-new))/?(?:[#?]|$)'
  554. _TESTS = [{
  555. 'url': 'https://zingmp3.vn/cgr',
  556. 'info_dict': {
  557. 'id': 'cgr',
  558. },
  559. 'playlist_mincount': 5,
  560. }, {
  561. 'url': 'https://zingmp3.vn/top-podcast',
  562. 'info_dict': {
  563. 'id': 'top-podcast',
  564. },
  565. 'playlist_mincount': 7,
  566. }, {
  567. 'url': 'https://zingmp3.vn/podcast-new',
  568. 'info_dict': {
  569. 'id': 'podcast-new',
  570. },
  571. 'playlist_mincount': 4,
  572. }]
  573. def _real_extract(self, url):
  574. url_type = self._match_id(url)
  575. params = {'id': url_type}
  576. if url_type == 'podcast-new':
  577. params['type'] = 'new'
  578. items = self._call_api('cgrs' if url_type == 'cgr' else url_type, params)['items']
  579. return self.playlist_result(self._parse_items(items), url_type)