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.

107 lines
3.3 KiB

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