-
Notifications
You must be signed in to change notification settings - Fork 5
/
dacpclient
executable file
·382 lines (331 loc) · 8.39 KB
/
dacpclient
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!/usr/bin/env ruby
$LOAD_PATH.unshift(File.join(__dir__, '../lib/'))
require 'dacpclient'
require 'English'
require 'socket'
require 'yaml'
require 'fileutils'
require 'io/console'
require 'fuzzy_match'
require 'thor'
# This is the CLI DACP Client. Normally installed as `dacpclient`
class CLIClient < Thor
package_name 'DACPClient'
include Thor::Actions
def initialize(*)
@config = {}
@config['client_name'] ||= "DACPClient (#{Socket.gethostname})"
@config['host'] ||= 'localhost'
@config['known_services'] ||= []
load_config
if @config['guid'].nil? || @config['guid'] !~ /^[A-F0-9]{16}$/
guid = Digest::SHA2.hexdigest(@config['client_name'])[0..15].upcase
@config['guid'] = guid
save_config
end
browser = DACPClient::Browser.new
database = nil
3.times do
browser.browse
database = browser.services.find do |service|
@config['known_services'].include? service.name
end
break if database
sleep 0.5
end
pin = @config['pin'].to_s.chars.map(&:to_i)
unless database
pin = 4.times.map { Random.rand(10) } if pin.empty?
puts 'Cannot find paired Libraries, waiting for a pair request..'
puts "Pincode: #{pin}"
pairserver = DACPClient::PairingServer.new(@config['client_name'],
@config['guid'])
pairserver.pin = pin
database = pairserver.start
@config['known_services'] << database.name
save_config
end
@client = DACPClient::Client.new(@config['client_name'], database.host,
database.port)
@client.guid = @config['guid']
@login = false
super
end
desc :status, 'Shows the status of the DACP server'
method_options ticker: :boolean
def status
login
return status_ticker if options[:ticker]
show_status
end
desc :hostname, 'Set the hostname'
def hostname
@config['host'] = ask('Please enter a new hostname to connect to:').strip
save_config
@client = DACPClient::Client.new(@config['client_name'], @config['host'],
3689)
status
end
desc :play, 'Start playing'
def play
login
@client.play
show_status
end
desc :pause, 'Pause Playing'
def pause
login
@client.pause
show_status
end
desc :playpause, 'Toggle Playing'
def playpause
login
@client.playpause
show_status
end
desc :next, 'Go to next item'
def next
login
@client.next
show_status
end
desc :prev, 'Go to previous item'
map previous: :prev
def prev
login
@client.prev
show_status
end
desc :databases, 'Show the databases'
def databases
login
database_items = @client.databases.items
puts 'Databases:'
puts '----------'
count = 0
num = Math.log10(database_items.length).floor + 1
database_items.each do |database|
printf("%#{num}d. ", count += 1)
puts "#{database.name} (#{database.item_id})"
end
puts
end
desc :playlists, 'Show the playlists'
def playlists
login
playlist_items = @client.playlists
puts 'Playlists:'
puts '----------'
count = 0
num = Math.log10(playlist_items.length).floor + 1
playlist_items.each do |playlist|
printf("%#{num}d. ", count += 1) unless playlist.base_playlist?
puts "#{playlist.name} (#{playlist.count})"
end
puts
end
desc :artists, 'Show the artists'
def artists
login
artists = @client.artists
puts 'Artists:'
puts '----------'
count = 0
num = Math.log10(artists.length).floor + 1
artists.each do |artist|
printf("%#{num}d. ", count += 1)
puts "#{artist.name} (#{artist.count} songs, #{artist.album_count} albums)"
end
puts
end
desc :albums, 'Show the albums'
def albums
login
albums = @client.albums
puts 'Albums:'
puts '----------'
count = 0
num = Math.log10(albums.length).floor + 1
albums.each do |album|
printf("%#{num}d. ", count += 1)
puts "#{album.name} [#{album.album_artist}] (#{album.count} songs)"
end
puts
end
desc :search, 'Search for a song'
def search(query)
login
songs = @client.search query
puts 'Songs:'
puts '----------'
count = 0
num = Math.log10(songs.length).floor + 1
songs.each do |song|
printf("%#{num}d. ", count += 1)
puts "#{song.name} - #{song.artist} - #{song.album}"
end
puts
end
desc :play_playlist, 'Plays a playlist'
def play_playlist(playlist)
login
playlists = @client.playlists
if playlist =~ /^\d+$/
playlist = playlists[playlist.to_i]
else
fz = FuzzyMatch.new(playlists, read: :name)
playlist = fz.find(playlist)
end
@client.playspec(playlist)
end
desc :upnext, 'Show what\'s up next'
def upnext
login
items = @client.list_queue.items
puts 'Up next:'
puts '--------'
puts
items.each do |item|
title = item.title
artist = item.artist
album = item.album
puts "#{title} - #{artist} (#{album}) [#{format_time(item.song_time)}]"
end
puts
show_status
end
desc :stop, 'Stop playing'
def stop
login
@client.stop
show_status
end
desc :volume, 'Get or set volume'
def volume(vol = nil)
login
@client.volume = vol.to_i unless vol.nil?
puts "Volume: #{@client.volume}"
show_status
end
# rubocop:disable Debugger
desc :debug, 'Debuggin\''
def debug
login
begin
require 'pry'
binding.pry
rescue
puts 'Please install PRY to be able to debug things.'
end
end
# rubocop:enable Debugger
desc :version, 'Show DACPClient Version'
def version
puts "DACPClient v#{DACPClient::VERSION}"
puts "using DMAPParser v#{DMAPParser::VERSION}"
print 'DACPClient and DMAPParser are Copyright (c) '
puts "#{Time.now.year} Jurriaan Pruis <email@jurriaanpruis.nl>"
end
default_task :banner
desc :help, 'Display all possible commands'
def banner
puts "DACPClient v#{DACPClient::VERSION}"
puts "(c) #{Time.now.year} Jurriaan Pruis <email@jurriaanpruis.nl>"
puts
help
end
private
def show_status(status = @client.status, start_time = nil)
name = status.title
artist = status.artist
album = status.album
playstatus = status.playing? ? '▶ ' : '❙❙'
current = 0
total = 0
extra_time = 0
extra_time = Time.now.to_f * 1000.0 - start_time if start_time
if status.song_length? && status.song_remaining_time?
total = status.song_length
current = status.song_position + extra_time
end
print "[#{format_time(current)}/#{format_time(total)}]"
puts " #{playstatus} #{name} - #{artist} (#{album})"
end
def status_ticker
status = nil
start_time = nil
repeat_every(1) do
unless status.nil?
if status.stopped?
print "\r\033[K[STOPPED]"
else
show_status(status, start_time)
end
end
end
loop do
status = @client.status true
start_time = Time.now.to_f * 1000.0
end
end
def login
return if @login
@client.hsgid = @config['hsgid']
if @client.hsgid.nil?
@client.pair_and_login
else
@client.login
end
@login = true
return if @client.host == @config['host']
@config['host'] = @client.host
save_config
end
def format_time(millis)
seconds, _ = millis.divmod(1000)
minutes, seconds = seconds.divmod(60)
hours, minutes = minutes.divmod(60)
if hours == 0
format('%02d:%02d', minutes, seconds)
else
format('%02d:%02d:%02d', hours, minutes, seconds)
end
end
def repeat_every(interval)
Thread.new do
loop do
start_time = Time.now
yield
elapsed = Time.now - start_time
sleep([interval - elapsed, 0].max)
end
end
end
def config_dir
File.join(ENV['HOME'], '.dacpclient')
end
def config_file
File.join(config_dir, 'config.yml')
end
def load_config
FileUtils.mkdir_p(config_dir)
if File.exist? config_file
data = YAML.load_file(config_file)
@config.merge!(data) if data.is_a?(Hash)
else
save_config
end
end
def clean_config
@config['known_services'].delete('')
@config['known_services'].uniq!
end
def save_config
clean_config
File.open(File.join(config_dir, 'config.yml'), 'w') do |out|
YAML.dump(@config, out)
end
end
end
CLIClient.start