You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
4.7 KiB

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