aboutsummaryrefslogtreecommitdiffhomepage
path: root/youtube_dl/extractor/palcomp3.py
blob: 60f7a4d48db45a6c583bba33b328621f6c573641 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# coding: utf-8
from __future__ import unicode_literals

import re

from .common import InfoExtractor
from ..compat import compat_str
from ..utils import (
    int_or_none,
    str_or_none,
    traverse_obj,
)


class PalcoMP3BaseIE(InfoExtractor):
    _GQL_QUERY_TMPL = '''{
  artist(slug: "%s") {
    %s
  }
}'''
    _ARTIST_FIELDS_TMPL = '''music(slug: "%%s") {
      %s
    }'''
    _MUSIC_FIELDS = '''duration
      hls
      mp3File
      musicID
      plays
      title'''

    def _call_api(self, artist_slug, artist_fields):
        return self._download_json(
            'https://www.palcomp3.com.br/graphql/', artist_slug, query={
                'query': self._GQL_QUERY_TMPL % (artist_slug, artist_fields),
            })['data']

    def _parse_music(self, music):
        music_id = compat_str(music['musicID'])
        title = music['title']

        formats = []
        hls_url = music.get('hls')
        if hls_url:
            formats.append({
                'url': hls_url,
                'protocol': 'm3u8_native',
                'ext': 'mp4',
            })
        mp3_file = music.get('mp3File')
        if mp3_file:
            formats.append({
                'url': mp3_file,
            })

        return {
            'id': music_id,
            'title': title,
            'formats': formats,
            'duration': int_or_none(music.get('duration')),
            'view_count': int_or_none(music.get('plays')),
        }

    def _real_initialize(self):
        self._ARTIST_FIELDS_TMPL = self._ARTIST_FIELDS_TMPL % self._MUSIC_FIELDS

    def _real_extract(self, url):
        artist_slug, music_slug = re.match(self._VALID_URL, url).groups()
        artist_fields = self._ARTIST_FIELDS_TMPL % music_slug
        music = self._call_api(artist_slug, artist_fields)['artist']['music']
        return self._parse_music(music)


class PalcoMP3IE(PalcoMP3BaseIE):
    IE_NAME = 'PalcoMP3:song'
    _VALID_URL = r'https?://(?:www\.)?palcomp3\.com(?:\.br)?/(?P<artist>[^/]+)/(?P<id>[^/?&#]+)'
    _TESTS = [{
        'url': 'https://www.palcomp3.com/maiaraemaraisaoficial/nossas-composicoes-cuida-bem-dela/',
        'md5': '99fd6405b2d8fd589670f6db1ba3b358',
        'info_dict': {
            'id': '3162927',
            'ext': 'mp3',
            'title': 'Nossas Composições - CUIDA BEM DELA',
            'duration': 210,
            'view_count': int,
        }
    }]

    @classmethod
    def suitable(cls, url):
        return False if PalcoMP3VideoIE.suitable(url) else super(PalcoMP3IE, cls).suitable(url)


class PalcoMP3ArtistIE(PalcoMP3BaseIE):
    IE_NAME = 'PalcoMP3:artist'
    _VALID_URL = r'https?://(?:www\.)?palcomp3\.com(?:\.br)?/(?P<id>[^/?&#]+)'
    _TESTS = [{
        'url': 'https://www.palcomp3.com.br/condedoforro/',
        'info_dict': {
            'id': '358396',
            'title': 'Conde do Forró',
        },
        'playlist_mincount': 188,
    }]
    _ARTIST_FIELDS_TMPL = '''artistID
    musics {
      nodes {
        %s
      }
    }
    name'''

    @classmethod
    def suitable(cls, url):
        return False if re.match(PalcoMP3IE._VALID_URL, url) else super(PalcoMP3ArtistIE, cls).suitable(url)

    def _real_extract(self, url):
        artist_slug = self._match_id(url)
        artist = self._call_api(artist_slug, self._ARTIST_FIELDS_TMPL)['artist']

        def entries():
            for music in traverse_obj(artist, (
                    'musics', 'nodes', lambda _, m: m['musicID'])):
                yield self._parse_music(music)

        return self.playlist_result(
            entries(), str_or_none(artist.get('artistID')), artist.get('name'))


class PalcoMP3VideoIE(PalcoMP3BaseIE):
    IE_NAME = 'PalcoMP3:video'
    _VALID_URL = r'https?://(?:www\.)?palcomp3\.com(?:\.br)?/(?P<artist>[^/]+)/(?P<id>[^/?&#]+)/?#clipe'
    _TESTS = [{
        'url': 'https://www.palcomp3.com/maiaraemaraisaoficial/maiara-e-maraisa-voce-faz-falta-aqui-ao-vivo-em-vicosa-mg/#clipe',
        'add_ie': ['Youtube'],
        'info_dict': {
            'id': '_pD1nR2qqPg',
            'ext': 'mp4',
            'title': 'Maiara e Maraisa - Você Faz Falta Aqui - DVD Ao Vivo Em Campo Grande',
            'description': 'md5:7043342c09a224598e93546e98e49282',
            'upload_date': '20161107',
            'uploader_id': '@maiaramaraisaoficial',
            'uploader': 'Maiara e Maraisa',
        }
    }]
    _MUSIC_FIELDS = 'youtubeID'

    def _parse_music(self, music):
        youtube_id = music['youtubeID']
        return self.url_result(youtube_id, 'Youtube', youtube_id)