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.

120 lines
3.9 KiB

  1. # coding=utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import ExtractorError
  6. class ZingMp3BaseInfoExtractor(InfoExtractor):
  7. def _extract_item(self, item, fatal=True):
  8. error_message = item.find('./errormessage').text
  9. if error_message:
  10. if not fatal:
  11. return
  12. raise ExtractorError(
  13. '%s returned error: %s' % (self.IE_NAME, error_message),
  14. expected=True)
  15. title = item.find('./title').text.strip()
  16. source = item.find('./source').text
  17. extension = item.attrib['type']
  18. thumbnail = item.find('./backimage').text
  19. return {
  20. 'title': title,
  21. 'url': source,
  22. 'ext': extension,
  23. 'thumbnail': thumbnail,
  24. }
  25. def _extract_player_xml(self, player_xml_url, id, playlist_title=None):
  26. player_xml = self._download_xml(player_xml_url, id, 'Downloading Player XML')
  27. items = player_xml.findall('./item')
  28. if len(items) == 1:
  29. # one single song
  30. data = self._extract_item(items[0])
  31. data['id'] = id
  32. return data
  33. else:
  34. # playlist of songs
  35. entries = []
  36. for i, item in enumerate(items, 1):
  37. entry = self._extract_item(item, fatal=False)
  38. if not entry:
  39. continue
  40. entry['id'] = '%s-%d' % (id, i)
  41. entries.append(entry)
  42. return {
  43. '_type': 'playlist',
  44. 'id': id,
  45. 'title': playlist_title,
  46. 'entries': entries,
  47. }
  48. class ZingMp3SongIE(ZingMp3BaseInfoExtractor):
  49. _VALID_URL = r'https?://mp3\.zing\.vn/bai-hat/(?P<slug>[^/]+)/(?P<song_id>\w+)\.html'
  50. _TESTS = [{
  51. 'url': 'http://mp3.zing.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html',
  52. 'md5': 'ead7ae13693b3205cbc89536a077daed',
  53. 'info_dict': {
  54. 'id': 'ZWZB9WAB',
  55. 'title': 'Xa Mãi Xa',
  56. 'ext': 'mp3',
  57. 'thumbnail': 're:^https?://.*\.jpg$',
  58. },
  59. }]
  60. IE_NAME = 'zingmp3:song'
  61. IE_DESC = 'mp3.zing.vn songs'
  62. def _real_extract(self, url):
  63. matched = re.match(self._VALID_URL, url)
  64. slug = matched.group('slug')
  65. song_id = matched.group('song_id')
  66. webpage = self._download_webpage(
  67. 'http://mp3.zing.vn/bai-hat/%s/%s.html' % (slug, song_id), song_id)
  68. player_xml_url = self._search_regex(
  69. r'&amp;xmlURL=(?P<xml_url>[^&]+)&', webpage, 'player xml url')
  70. return self._extract_player_xml(player_xml_url, song_id)
  71. class ZingMp3AlbumIE(ZingMp3BaseInfoExtractor):
  72. _VALID_URL = r'https?://mp3\.zing\.vn/(?:album|playlist)/(?P<slug>[^/]+)/(?P<album_id>\w+)\.html'
  73. _TESTS = [{
  74. 'url': 'http://mp3.zing.vn/album/Lau-Dai-Tinh-Ai-Bang-Kieu-Minh-Tuyet/ZWZBWDAF.html',
  75. 'info_dict': {
  76. '_type': 'playlist',
  77. 'id': 'ZWZBWDAF',
  78. 'title': 'Lâu Đài Tình Ái - Bằng Kiều ft. Minh Tuyết | Album 320 lossless',
  79. },
  80. 'playlist_count': 10,
  81. }, {
  82. 'url': 'http://mp3.zing.vn/playlist/Duong-Hong-Loan-apollobee/IWCAACCB.html',
  83. 'only_matching': True,
  84. }]
  85. IE_NAME = 'zingmp3:album'
  86. IE_DESC = 'mp3.zing.vn albums'
  87. def _real_extract(self, url):
  88. matched = re.match(self._VALID_URL, url)
  89. slug = matched.group('slug')
  90. album_id = matched.group('album_id')
  91. webpage = self._download_webpage(
  92. 'http://mp3.zing.vn/album/%s/%s.html' % (slug, album_id), album_id)
  93. player_xml_url = self._search_regex(
  94. r'&amp;xmlURL=(?P<xml_url>[^&]+)&', webpage, 'player xml url')
  95. return self._extract_player_xml(
  96. player_xml_url, album_id,
  97. playlist_title=self._og_search_title(webpage))