palcomp3.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. from .common import InfoExtractor
  2. from ..compat import compat_str
  3. from ..utils import (
  4. int_or_none,
  5. str_or_none,
  6. try_get,
  7. )
  8. class PalcoMP3BaseIE(InfoExtractor):
  9. _GQL_QUERY_TMPL = '''{
  10. artist(slug: "%s") {
  11. %s
  12. }
  13. }'''
  14. _ARTIST_FIELDS_TMPL = '''music(slug: "%%s") {
  15. %s
  16. }'''
  17. _MUSIC_FIELDS = '''duration
  18. hls
  19. mp3File
  20. musicID
  21. plays
  22. title'''
  23. def _call_api(self, artist_slug, artist_fields):
  24. return self._download_json(
  25. 'https://www.palcomp3.com.br/graphql/', artist_slug, query={
  26. 'query': self._GQL_QUERY_TMPL % (artist_slug, artist_fields),
  27. })['data']
  28. def _parse_music(self, music):
  29. music_id = compat_str(music['musicID'])
  30. title = music['title']
  31. formats = []
  32. hls_url = music.get('hls')
  33. if hls_url:
  34. formats.append({
  35. 'url': hls_url,
  36. 'protocol': 'm3u8_native',
  37. 'ext': 'mp4',
  38. })
  39. mp3_file = music.get('mp3File')
  40. if mp3_file:
  41. formats.append({
  42. 'url': mp3_file,
  43. })
  44. return {
  45. 'id': music_id,
  46. 'title': title,
  47. 'formats': formats,
  48. 'duration': int_or_none(music.get('duration')),
  49. 'view_count': int_or_none(music.get('plays')),
  50. }
  51. def _real_initialize(self):
  52. self._ARTIST_FIELDS_TMPL = self._ARTIST_FIELDS_TMPL % self._MUSIC_FIELDS
  53. def _real_extract(self, url):
  54. artist_slug, music_slug = self._match_valid_url(url).groups()
  55. artist_fields = self._ARTIST_FIELDS_TMPL % music_slug
  56. music = self._call_api(artist_slug, artist_fields)['artist']['music']
  57. return self._parse_music(music)
  58. class PalcoMP3IE(PalcoMP3BaseIE):
  59. IE_NAME = 'PalcoMP3:song'
  60. _VALID_URL = r'https?://(?:www\.)?palcomp3\.com(?:\.br)?/(?P<artist>[^/]+)/(?P<id>[^/?&#]+)'
  61. _TESTS = [{
  62. 'url': 'https://www.palcomp3.com/maiaraemaraisaoficial/nossas-composicoes-cuida-bem-dela/',
  63. 'md5': '99fd6405b2d8fd589670f6db1ba3b358',
  64. 'info_dict': {
  65. 'id': '3162927',
  66. 'ext': 'mp3',
  67. 'title': 'Nossas Composições - CUIDA BEM DELA',
  68. 'duration': 210,
  69. 'view_count': int,
  70. }
  71. }]
  72. @classmethod
  73. def suitable(cls, url):
  74. return False if PalcoMP3VideoIE.suitable(url) else super(PalcoMP3IE, cls).suitable(url)
  75. class PalcoMP3ArtistIE(PalcoMP3BaseIE):
  76. IE_NAME = 'PalcoMP3:artist'
  77. _VALID_URL = r'https?://(?:www\.)?palcomp3\.com(?:\.br)?/(?P<id>[^/?&#]+)'
  78. _TESTS = [{
  79. 'url': 'https://www.palcomp3.com.br/condedoforro/',
  80. 'info_dict': {
  81. 'id': '358396',
  82. 'title': 'Conde do Forró',
  83. },
  84. 'playlist_mincount': 188,
  85. }]
  86. _ARTIST_FIELDS_TMPL = '''artistID
  87. musics {
  88. nodes {
  89. %s
  90. }
  91. }
  92. name'''
  93. @classmethod
  94. def suitable(cls, url):
  95. return False if PalcoMP3IE._match_valid_url(url) else super(PalcoMP3ArtistIE, cls).suitable(url)
  96. def _real_extract(self, url):
  97. artist_slug = self._match_id(url)
  98. artist = self._call_api(artist_slug, self._ARTIST_FIELDS_TMPL)['artist']
  99. def entries():
  100. for music in (try_get(artist, lambda x: x['musics']['nodes'], list) or []):
  101. yield self._parse_music(music)
  102. return self.playlist_result(
  103. entries(), str_or_none(artist.get('artistID')), artist.get('name'))
  104. class PalcoMP3VideoIE(PalcoMP3BaseIE):
  105. IE_NAME = 'PalcoMP3:video'
  106. _VALID_URL = r'https?://(?:www\.)?palcomp3\.com(?:\.br)?/(?P<artist>[^/]+)/(?P<id>[^/?&#]+)/?#clipe'
  107. _TESTS = [{
  108. 'url': 'https://www.palcomp3.com/maiaraemaraisaoficial/maiara-e-maraisa-voce-faz-falta-aqui-ao-vivo-em-vicosa-mg/#clipe',
  109. 'add_ie': ['Youtube'],
  110. 'info_dict': {
  111. 'id': '_pD1nR2qqPg',
  112. 'ext': 'mp4',
  113. 'title': 'Maiara e Maraisa - Você Faz Falta Aqui - DVD Ao Vivo Em Campo Grande',
  114. 'description': 'md5:7043342c09a224598e93546e98e49282',
  115. 'upload_date': '20161107',
  116. 'uploader_id': 'maiaramaraisaoficial',
  117. 'uploader': 'Maiara e Maraisa',
  118. }
  119. }]
  120. _MUSIC_FIELDS = 'youtubeID'
  121. def _parse_music(self, music):
  122. youtube_id = music['youtubeID']
  123. return self.url_result(youtube_id, 'Youtube', youtube_id)