logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

zingmp3.py (5546B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. ExtractorError,
  6. int_or_none,
  7. )
  8. class ZingMp3BaseIE(InfoExtractor):
  9. _VALID_URL_TMPL = r'https?://(?:mp3\.zing|zingmp3)\.vn/(?:%s)/[^/]+/(?P<id>\w+)\.html'
  10. _GEO_COUNTRIES = ['VN']
  11. def _extract_item(self, item, fatal):
  12. item_id = item['id']
  13. title = item.get('name') or item['title']
  14. formats = []
  15. for k, v in (item.get('source') or {}).items():
  16. if not v:
  17. continue
  18. if k in ('mp4', 'hls'):
  19. for res, video_url in v.items():
  20. if not video_url:
  21. continue
  22. if k == 'hls':
  23. formats.extend(self._extract_m3u8_formats(
  24. video_url, item_id, 'mp4',
  25. 'm3u8_native', m3u8_id=k, fatal=False))
  26. elif k == 'mp4':
  27. formats.append({
  28. 'format_id': 'mp4-' + res,
  29. 'url': video_url,
  30. 'height': int_or_none(self._search_regex(
  31. r'^(\d+)p', res, 'resolution', default=None)),
  32. })
  33. else:
  34. formats.append({
  35. 'ext': 'mp3',
  36. 'format_id': k,
  37. 'tbr': int_or_none(k),
  38. 'url': self._proto_relative_url(v),
  39. 'vcodec': 'none',
  40. })
  41. if not formats:
  42. if not fatal:
  43. return
  44. msg = item['msg']
  45. if msg == 'Sorry, this content is not available in your country.':
  46. self.raise_geo_restricted(countries=self._GEO_COUNTRIES)
  47. raise ExtractorError(msg, expected=True)
  48. self._sort_formats(formats)
  49. subtitles = None
  50. lyric = item.get('lyric')
  51. if lyric:
  52. subtitles = {
  53. 'origin': [{
  54. 'url': lyric,
  55. }],
  56. }
  57. album = item.get('album') or {}
  58. return {
  59. 'id': item_id,
  60. 'title': title,
  61. 'formats': formats,
  62. 'thumbnail': item.get('thumbnail'),
  63. 'subtitles': subtitles,
  64. 'duration': int_or_none(item.get('duration')),
  65. 'track': title,
  66. 'artist': item.get('artists_names'),
  67. 'album': album.get('name') or album.get('title'),
  68. 'album_artist': album.get('artists_names'),
  69. }
  70. def _real_extract(self, url):
  71. page_id = self._match_id(url)
  72. webpage = self._download_webpage(
  73. url.replace('://zingmp3.vn/', '://mp3.zing.vn/'),
  74. page_id, query={'play_song': 1})
  75. data_path = self._search_regex(
  76. r'data-xml="([^"]+)', webpage, 'data path')
  77. return self._process_data(self._download_json(
  78. 'https://mp3.zing.vn/xhr' + data_path, page_id)['data'])
  79. class ZingMp3IE(ZingMp3BaseIE):
  80. _VALID_URL = ZingMp3BaseIE._VALID_URL_TMPL % 'bai-hat|video-clip'
  81. _TESTS = [{
  82. 'url': 'http://mp3.zing.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html',
  83. 'md5': 'ead7ae13693b3205cbc89536a077daed',
  84. 'info_dict': {
  85. 'id': 'ZWZB9WAB',
  86. 'title': 'Xa Mãi Xa',
  87. 'ext': 'mp3',
  88. 'thumbnail': r're:^https?://.+\.jpg',
  89. 'subtitles': {
  90. 'origin': [{
  91. 'ext': 'lrc',
  92. }]
  93. },
  94. 'duration': 255,
  95. 'track': 'Xa Mãi Xa',
  96. 'artist': 'Bảo Thy',
  97. 'album': 'Special Album',
  98. 'album_artist': 'Bảo Thy',
  99. },
  100. }, {
  101. 'url': 'https://mp3.zing.vn/video-clip/Suong-Hoa-Dua-Loi-K-ICM-RYO/ZO8ZF7C7.html',
  102. 'md5': 'e9c972b693aa88301ef981c8151c4343',
  103. 'info_dict': {
  104. 'id': 'ZO8ZF7C7',
  105. 'title': 'Sương Hoa Đưa Lối',
  106. 'ext': 'mp4',
  107. 'thumbnail': r're:^https?://.+\.jpg',
  108. 'duration': 207,
  109. 'track': 'Sương Hoa Đưa Lối',
  110. 'artist': 'K-ICM, RYO',
  111. },
  112. }, {
  113. 'url': 'https://zingmp3.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html',
  114. 'only_matching': True,
  115. }]
  116. IE_NAME = 'zingmp3'
  117. IE_DESC = 'mp3.zing.vn'
  118. def _process_data(self, data):
  119. return self._extract_item(data, True)
  120. class ZingMp3AlbumIE(ZingMp3BaseIE):
  121. _VALID_URL = ZingMp3BaseIE._VALID_URL_TMPL % 'album|playlist'
  122. _TESTS = [{
  123. 'url': 'http://mp3.zing.vn/album/Lau-Dai-Tinh-Ai-Bang-Kieu-Minh-Tuyet/ZWZBWDAF.html',
  124. 'info_dict': {
  125. '_type': 'playlist',
  126. 'id': 'ZWZBWDAF',
  127. 'title': 'Lâu Đài Tình Ái',
  128. },
  129. 'playlist_count': 10,
  130. }, {
  131. 'url': 'http://mp3.zing.vn/playlist/Duong-Hong-Loan-apollobee/IWCAACCB.html',
  132. 'only_matching': True,
  133. }, {
  134. 'url': 'https://zingmp3.vn/album/Lau-Dai-Tinh-Ai-Bang-Kieu-Minh-Tuyet/ZWZBWDAF.html',
  135. 'only_matching': True,
  136. }]
  137. IE_NAME = 'zingmp3:album'
  138. def _process_data(self, data):
  139. def entries():
  140. for item in (data.get('items') or []):
  141. entry = self._extract_item(item, False)
  142. if entry:
  143. yield entry
  144. info = data.get('info') or {}
  145. return self.playlist_result(
  146. entries(), info.get('id'), info.get('name') or info.get('title'))