Module: Ampt::API

Included in:
Command
Defined in:
lib/ampt.rb,
lib/ampt_api/acoustics.rb

Overview

Provides the API for Acoustics

Instance Method Summary collapse

Instance Method Details

#auth(fatal = true) ⇒ Object

Perform an authenticated request



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ampt_api/acoustics.rb', line 128

def auth fatal = true
    unless @sess
        c = Curl::Easy.new
        c.http_auth_types = Curl::CURLAUTH_GSSNEGOTIATE
        c.userpwd = ":"
        c.url = "#{@base_url}/www-data/auth/"
        c.perform
        sessid = c.header_str[/CGISESSID=(.+);/]
        if sessid.nil?
            puts "Could not authenticate -- maybe you should kinit?"
            abort if fatal
            return
        end
        @sess = sessid.chop
    end
end

#history(cnt = '25') ⇒ Object

Gets the most recently played songs. Takes a string count.



43
44
45
# File 'lib/ampt_api/acoustics.rb', line 43

def history cnt = '25'
    song_list req 'history', 'amount' => cnt || '25'
end

#list(options) ⇒ Object

takes field => value pair of what to list. Only one of the given pairs is used.



72
73
74
75
76
# File 'lib/ampt_api/acoustics.rb', line 72

def list options
    field = options.keys[0]
    value = options[field]
    song_list req 'select', {'field' => field, 'value' => value}
end

#player_status(json) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/ampt_api/acoustics.rb', line 117

def player_status json
    begin
        JSON.parse "{\"json_class\": \"Ampt::Status\", \"data\": " + json + "}"
    rescue JSON::ParserError => e
        puts "Got invalid JSON!"
        p json
        abort
    end
end

#random(amt = '20') ⇒ Object

Returns amt random songs.



79
80
81
# File 'lib/ampt_api/acoustics.rb', line 79

def random amt='20'
    song_list req 'random', 'amount' => amt || '20'
end

#recent(cnt = '50') ⇒ Object

Gets the most recently added songs. Takes a string count.



38
39
40
# File 'lib/ampt_api/acoustics.rb', line 38

def recent cnt = '50'
    song_list req 'recent', 'amount' => cnt || '50'
end

#req(mode = nil, params = nil) ⇒ Object

Performs a request with given mode and params.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ampt_api/acoustics.rb', line 84

def req mode=nil, params = nil
    c = Curl::Easy.new
    c.cookies = @sess
    if params
        param_str = params.collect do |key, value|
            if value.instance_of? Array
                value.collect {|v| "#{URI::escape key}=#{URI::escape v}"}.join(';')
            else
            "#{URI::escape key}=#{URI::escape value}"
            end
        end
        c.url = "#{@base_url}/json.pl?mode=#{mode};" + param_str.join(';')
    elsif mode
        c.url = "#{@base_url}/json.pl?mode=#{mode};"
    else
        c.url = "#{@base_url}/json.pl"
    end
    c.perform
    c.body_str
end

#resetObject

Clears all of your votes.



33
34
35
# File 'lib/ampt_api/acoustics.rb', line 33

def reset
    unvote '0'
end

#search(value, field = 'any') ⇒ Object

Searches the database for the specified value in the given field.



28
29
30
# File 'lib/ampt_api/acoustics.rb', line 28

def search value, field = 'any'
    song_list req 'search', {'field' => field, 'value' => value}
end

#skipObject

Skip the current song. Will probably fail horribly if you aren’t allowed to skip.



60
61
62
63
# File 'lib/ampt_api/acoustics.rb', line 60

def skip
    auth
    player_status req 'skip'
end

#song_list(json) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ampt_api/acoustics.rb', line 105

def song_list json
    begin
        JSON.parse(json).collect do |s|
            Ampt::Song.json_create('data' => s)
        end
    rescue JSON::ParserError => e
        puts "Got invalid JSON!"
        p json
        abort
    end
end

#startObject

Start the player.



54
55
56
57
# File 'lib/ampt_api/acoustics.rb', line 54

def start
    auth
    player_status req 'start'
end

#statusObject

Request the player’s status. gets the session first so we can tell whether or not we can skip the current song, which songs are ours, etc.



10
11
12
13
# File 'lib/ampt_api/acoustics.rb', line 10

def status
    auth false
    player_status req
end

#stopObject

Stop the player.



66
67
68
69
# File 'lib/ampt_api/acoustics.rb', line 66

def stop
    auth
    player_status req 'stop'
end

#unvote(song_id) ⇒ Object

Unvotes a song. Takes a song_id (string)



16
17
18
19
# File 'lib/ampt_api/acoustics.rb', line 16

def unvote song_id
    auth
    player_status req 'unvote', 'song_id' => song_id
end

#volume(level) ⇒ Object

Sets the volume to the specified level. level should be a string.



48
49
50
51
# File 'lib/ampt_api/acoustics.rb', line 48

def volume level
    auth
    player_status req 'volume', 'value' => level
end

#vote(song_id) ⇒ Object

Votes up a song. Takes a song_id (string)



22
23
24
25
# File 'lib/ampt_api/acoustics.rb', line 22

def vote song_id
    auth
    player_status req 'vote', 'song_id' => song_id
end