Module: Exportify::Spotify

Defined in:
lib/exportify/spotify.rb

Class Method Summary collapse

Class Method Details

.enrich_with_genres(tracks, token) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/exportify/spotify.rb', line 81

def enrich_with_genres(tracks, token)
  artist_ids = tracks.map { |t| t[:artist_id] }.uniq.compact
  genres_by_id = {}

  artist_ids.each do |artist_id|
    uri = URI("https://api.spotify.com/v1/artists/#{artist_id}")
    req = Net::HTTP::Get.new(uri)
    req['Authorization'] = "Bearer #{token}"
    res  = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |h| h.request(req) }
    data = JSON.parse(res.body)
    handle_error!(data['error'], 'artistas')
    genres_by_id[data['id']] = (data['genres'] || []).first || ''
  end

  tracks.map { |t| t.merge(genre: genres_by_id[t[:artist_id]] || '') }
end

.handle_error!(error, context) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/exportify/spotify.rb', line 61

def handle_error!(error, context)
  return unless error

  status  = error['status']
  message = error['message']

  case status
  when 401
    abort "Erro #{status}: token inválido ou expirado. Apague ~/.exportify_token.json e tente novamente."
  when 403
    abort "Erro 403 ao buscar #{context}: acesso negado pelo Spotify.\n" \
          "Isso ocorre em playlists de artistas ou gravadoras com conteúdo protegido.\n" \
          'Tente com uma playlist pessoal ou pública de outro usuário.'
  when 404
    abort 'Erro 404: playlist não encontrada. Verifique se o link está correto.'
  else
    abort "Erro da API Spotify (#{status}): #{message}"
  end
end

.playlist_name(playlist_id, token) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/exportify/spotify.rb', line 11

def playlist_name(playlist_id, token)
  uri = URI("https://api.spotify.com/v1/playlists/#{playlist_id}?fields=name")
  req = Net::HTTP::Get.new(uri)
  req['Authorization'] = "Bearer #{token}"
  res  = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |h| h.request(req) }
  data = JSON.parse(res.body)
  handle_error!(data['error'], 'playlist')
  data['name']
end

.playlist_tracks(playlist_id, token) ⇒ Object



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
# File 'lib/exportify/spotify.rb', line 21

def playlist_tracks(playlist_id, token)
  tracks = []
  url    = "https://api.spotify.com/v1/playlists/#{playlist_id}/items?limit=100"

  while url
    uri = URI(url)
    req = Net::HTTP::Get.new(uri)
    req['Authorization'] = "Bearer #{token}"
    res  = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |h| h.request(req) }
    data = JSON.parse(res.body)
    handle_error!(data['error'], 'tracks')

    data['items'].each do |item|
      track = item['item']
      next if track.nil? || track['name'].nil?

      clean_name = track['name']
                   .gsub(/\s*[(\[].*?[)\]]/, '')
                   .gsub(/\s*-\s*(feat|ft)\.?.*/i, '')
                   .strip

      tracks << {
        artist: track['artists'].first['name'],
        artist_id: track['artists'].first['id'],
        all_artists: track['artists'].map { |a| a['name'] }.join(', '),
        name: clean_name,
        raw_name: track['name'],
        album: track.dig('album', 'name') || '',
        year: (track.dig('album', 'release_date') || '')[0..3],
        track_number: track['track_number'] || 0,
        genre: ''
      }
    end

    url = data['next']
  end

  tracks
end