Class: SpotifyExporter::Playlists

Inherits:
Thor
  • Object
show all
Includes:
ConfigDependant
Defined in:
lib/spotifyexporter/command/playlists.rb

Constant Summary collapse

SUPPORTED_PLAYLIST_FORMATS =
%w[m3u pls xspf txt yml].freeze

Instance Method Summary collapse

Methods included from Injectable

included

Instance Method Details

#exportObject



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
# File 'lib/spotifyexporter/command/playlists.rb', line 36

def export
  FileUtils.mkdir_p(options.output_directory) unless Dir.exist? options.output_directory
  validate_format!(options.format)

  spotify_playlists = spotify.get_user_playlists(options.user)

  # For each playlist
  spotify_playlists.each do |spotify_pl|
    # Remove non-alphanumeric characters
    # Replace all spaces with underscores
    sanitized_name = spotify_pl.name.gsub(/[^[:alnum:]^[:space:]]/, "").chomp

    playlist = Playlist.new(
      title: sanitized_name,
      description: spotify_pl.description,
      creator: options.user,
      # Use the first image
      image: spotify_pl.images.first["url"]
    )

    # For each track in the Spotify playlist,
    # create a new playlist track with title and contributors, then
    # add it to the playlist.
    spotify_pl.tracks.each do |spotify_track|
      track = Playlist::Track.new(
        title: spotify_track.name,
        duration: spotify_track.duration_ms
      )

      spotify_track.artists.each do |artist|
        track.add_contributor(name: artist.name)
      end

      playlist.add_track(track)
    end

    # Write the playlist to disk
    save_playlist(playlist, options.format)
  end
end

#lsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/spotifyexporter/command/playlists.rb', line 18

def ls
  pl = spotify.get_user_playlists(options.user)

  results = pl.map do |p|
    r = p.name

    r << " | #{p.external_urls["spotify"]}" unless options.url.nil?

    r
  end

  puts results
end