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



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/ampt_api/acoustics.rb', line 146

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.



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

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.



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

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

#player_status(json) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/ampt_api/acoustics.rb', line 135

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

#purge(user) ⇒ Object

Purges a user’s votes. Need to be admin to do this.



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

def purge user
    auth
    player_status req 'unvote', 'purge' => user
end

#random(amt = '20') ⇒ Object

Returns amt random songs.



85
86
87
# File 'lib/ampt_api/acoustics.rb', line 85

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.



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

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.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ampt_api/acoustics.rb', line 102

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.



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

def reset
    unvote '0'
end

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

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



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

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

#shuffle_votesObject

Shuffles your votes. Great for bogosort.



96
97
98
99
# File 'lib/ampt_api/acoustics.rb', line 96

def shuffle_votes
    auth
    player_status req 'shuffle_votes'
end

#skipObject

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



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

def skip
    auth
    player_status req 'skip'
end

#song_list(json) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ampt_api/acoustics.rb', line 123

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.



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

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.



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

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.



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

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

#vote(song_id) ⇒ Object

Votes up a song. Takes a song_id (string)



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

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

#vote_to_top(id) ⇒ Object

Brings song to the top.



90
91
92
93
# File 'lib/ampt_api/acoustics.rb', line 90

def vote_to_top id
    auth
    player_status req 'vote_to_top', 'song_id' => id
end