Class: Subsonic

Inherits:
Object
  • Object
show all
Defined in:
lib/Subsonic.rb

Overview

TODO remove puts from this class; Subcl should handle this

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configs, display) ⇒ Subsonic

Returns a new instance of Subsonic.



16
17
18
19
20
21
# File 'lib/Subsonic.rb', line 16

def initialize(configs, display)
  @configs = configs
  #hash containing procs for displaying songs, artists, albums
  @display = display
  @interactive = true
end

Instance Attribute Details

#interactiveObject

Returns the value of attribute interactive.



14
15
16
# File 'lib/Subsonic.rb', line 14

def interactive
  @interactive
end

Instance Method Details

#albumartObject

should the need arise, this outputs the album art as binary



173
174
175
# File 'lib/Subsonic.rb', line 173

def albumart
  $stderr.puts "Not yet implemented"
end

#albumartUrl(streamUrl, size = nil) ⇒ Object

returns the albumart URL for the song

Raises:

  • (ArgumentError)


162
163
164
165
166
167
168
169
170
# File 'lib/Subsonic.rb', line 162

def albumartUrl(streamUrl, size = nil)
  raise ArgumentError if streamUrl.empty?
  id = CGI.parse(URI.parse(streamUrl).query)['id'][0]
  params = {:id => id};
  params[:size] = size unless size.nil?
  addBasicAuth(
    buildUrl('getCoverArt.view', params)
  )
end

#albumlistObject



177
178
179
180
181
182
# File 'lib/Subsonic.rb', line 177

def albumlist
  doc = query('getAlbumList2.view', {:type => 'random'})
  doc.elements.each('subsonic-response/albumList2/album') do |album|
    puts "#{album.attributes['name']} by #{album.attributes['artist']}"
  end
end

#albums(name) ⇒ Object

returns all albums matching the pattern



101
102
103
# File 'lib/Subsonic.rb', line 101

def albums(name)
  search(name, :album)
end

#albumSongs(name) ⇒ Object

returns an array of songs for the given album name on multiple matches, the user is asked interactively for the wanted match



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/Subsonic.rb', line 38

def albumSongs(name)

  searchResults = search(name, :album)

  if searchResults.length.zero?
    return []
  end

  picks = whichDidYouMean(searchResults, &@display[:album])
  songs = []
  picks.each do |album|
    doc = query('getAlbum.view', {:id => album['id']})
    doc.elements.each('subsonic-response/album/song') do |element|
      songs << Song.new(self, element.attributes)
    end
  end

  songs
end

#allPlaylistsObject

returns all playlists



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/Subsonic.rb', line 111

def allPlaylists
  out = []
  doc = query('getPlaylists.view')
  doc.elements.each('subsonic-response/playlists/playlist') do |playlist|
    item = {
      :id => playlist.attributes['id'],
      :name => playlist.attributes['name'],
      :owner => playlist.attributes['owner']
    }
    out << item
  end
  out
end

#artists(name) ⇒ Object

returns all artists matching the pattern



96
97
98
# File 'lib/Subsonic.rb', line 96

def artists(name)
  search(name, :artist)
end

#artistSongs(name) ⇒ Object

returns an array of song streaming urls for the given artist name on multiple matches, the user is asked interactively for the wanted match



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

def artistSongs(name)
  searchResults = search(name, :artist)

  if searchResults.length.zero?
    return []
  end

  picks = whichDidYouMean(searchResults, &@display[:artist])
  songs = []
  picks.each do |artist|
    doc = query('getArtist.view', {:id => artist['id']})
    doc.elements.each('subsonic-response/artist/album') do |album|
      doc = query('getAlbum.view', {:id => album.attributes['id']})
      doc.elements.each('subsonic-response/album/song') do |element|
        songs << Song.new(self, element.attributes)
      end
    end
  end

  songs
end

#playlists(name = nil) ⇒ Object

returns all playlists matching name



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/Subsonic.rb', line 127

def playlists(name = nil)
  all = allPlaylists
  out = []

  if name
    name.downcase!
    all.each do |playlist|
      if playlist[:name].downcase.include? name
        out << playlist
      end
    end
  end

  whichDidYouMean(out, &@display[:playlist])
end

#playlistSongs(playListName) ⇒ Object

returns all songs from playlist(s) matching the name



144
145
146
147
148
149
150
151
152
153
# File 'lib/Subsonic.rb', line 144

def playlistSongs(playListName)
  out = []
  playlists(playListName).each do |playlist|
    doc = query('getPlaylist.view', {:id => playlist[:id]})
    doc.elements.each('subsonic-response/playlist/entry') do |entry|
      out << Song.new(self, entry.attributes)
    end
  end
  out
end

#randomSongs(count) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/Subsonic.rb', line 184

def randomSongs(count)
  if count.empty?
    count = @configs.randomSongCount
  else
    #throw an exception if it's not an int
    count = Integer(count)
  end
  out = []
  doc = query('getRandomSongs.view', {:size => count})
  doc.elements.each('subsonic-response/randomSongs/song') do |song|
    out << Song.new(self, song.attributes)
  end
  out
end

#song(name) ⇒ Object

returns an array of songs for the given album name on multiple matches, the user is asked interactively for the wanted match



25
26
27
28
29
30
31
32
33
34
# File 'lib/Subsonic.rb', line 25

def song(name)
  searchResults = search(name, :song)

  if searchResults.length.zero?
    return []
  end

  return whichDidYouMean(searchResults, &@display[:song])

end

#songs(name) ⇒ Object

returns all songs matching the pattern



106
107
108
# File 'lib/Subsonic.rb', line 106

def songs(name)
  search(name, :song)
end

#songUrl(songid) ⇒ Object

returns the streaming URL for the song, including basic auth



156
157
158
159
# File 'lib/Subsonic.rb', line 156

def songUrl(songid)
  uri = buildUrl('stream.view', {:id => songid})
  addBasicAuth(uri)
end

#whichDidYouMean(array, &displayProc) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/Subsonic.rb', line 82

def whichDidYouMean(array, &displayProc)
  if array.empty? or array.length == 1
    return array
  end

  if !@interactive
    return [array.first]
  end

  return Picker.new(array).pick(&displayProc)

end