Class: AmpacheRuby

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

Overview

Class is initialized with Hostname, user and password An auth token is requested on class initialization

To get the artist list from database you can call the method artists(nil) and you’ll get an array of AmpacheArtists.

To get albums from an artist you can use artist_instance.albums or ampache_ruby.instance.albums(artist_instance)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, user, psw) ⇒ AmpacheRuby

Returns a new instance of AmpacheRuby.



23
24
25
26
27
28
29
30
31
32
# File 'lib/lib-ampache.rb', line 23

def initialize(host, user, psw)
  uri = URI.parse(host)
  @host = uri.host
  @path = uri.path
  @port = uri.port
  @user = user
  @psw = psw
  @token = nil
  @token = getAuthToken(user, psw)
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



34
35
36
# File 'lib/lib-ampache.rb', line 34

def host
  @host
end

#pathObject

Returns the value of attribute path.



34
35
36
# File 'lib/lib-ampache.rb', line 34

def path
  @path
end

#playlistObject

Returns the value of attribute playlist.



34
35
36
# File 'lib/lib-ampache.rb', line 34

def playlist
  @playlist
end

#portObject

Returns the value of attribute port.



34
35
36
# File 'lib/lib-ampache.rb', line 34

def port
  @port
end

#pswObject

Returns the value of attribute psw.



34
35
36
# File 'lib/lib-ampache.rb', line 34

def psw
  @psw
end

#tokenObject

Returns the value of attribute token.



34
35
36
# File 'lib/lib-ampache.rb', line 34

def token
  @token
end

#userObject

Returns the value of attribute user.



34
35
36
# File 'lib/lib-ampache.rb', line 34

def user
  @user
end

Instance Method Details

#albums(artist) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/lib-ampache.rb', line 83

def albums(artist)
  albums = []
  args = {'filter' => artist.uid.to_s}
  doc = callApiMethod("artist_albums", args)
  doc.xpath("//album").each do |a|
    albums << AmpacheAlbum.new(self, a['id'], a.at("name").content, artist)
  end
  return albums
end

#artists(name = nil) ⇒ Object

retrive artists lists from database, name is an optional filter



72
73
74
75
76
77
78
79
80
81
# File 'lib/lib-ampache.rb', line 72

def artists(name = nil)
  args = {}
  args = {'filter' => name.to_s} if name # artist search
  artists = []
  doc = callApiMethod("artists", args)
  doc.xpath("//artist").each do |a|
    artists << AmpacheArtist.new(self, a['id'], a.at("name").content)
  end
  return artists
end

#callApiMethod(method, args = {}) ⇒ Object

generic api method call



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lib-ampache.rb', line 57

def callApiMethod(method, args={})
  begin
    args['auth'] ||= token if token
    url = path + "/server/xml.server.php?action=#{method}&#{args.keys.collect { |k| "#{k}=#{args[k]}" }.join('&')}"
    response = Net::HTTP.get_response(host, url, port)
    return Nokogiri::XML(response.body)
  rescue Errno::ECONNREFUSED => e
    warn "Ampache closed with the following error"
    warn e.message
    exit
  end
end

#getAuthToken(user, psw) ⇒ Object

tryies to obtain an auth token



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

def getAuthToken(user, psw)
  begin
    action= "handshake"
    # auth string
    key = Digest::SHA2.new << psw
    time = Time.now.to_i.to_s
    psk = Digest::SHA2.new << (time + key.to_s)

    args = {'auth' => psk, 'timestamp'=> time, 'version' => '350001', 'user' => user}
    doc = callApiMethod(action, args);

    return doc.at("auth").content
  rescue Exception => e 
    warn ""
    warn  "token not valid or expired, check your username and password" 
    warn ""
  end
end

#songs(album) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/lib-ampache.rb', line 93

def songs(album)
  songs = []
  args = {'filter' => album.uid.to_s}
  doc = callApiMethod("album_songs", args)
  doc.xpath("//song").each do |s|
    songs << AmpacheSong.new(self, s['id'], s.at("title").content, album.artist, album, s.at("url").content)
  end
  return songs
end