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.

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