-
Notifications
You must be signed in to change notification settings - Fork 0
/
radio_player2.rb
63 lines (46 loc) · 1.4 KB
/
radio_player2.rb
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
#a/usr/bin/env ruby
# radio_player2.rb
LOG_FILE = '/tmp/radio_player2.log'
PLAYERS = {
'.mp3' => 'mpg321',
'.ogg' => 'ogg123',
'' => 'ls'
}
# these are variables, local to Kernel.
# They work just as well as constants.
play_file_proc = lambda do |filename, time|
ext = File.extname(filename)
system("#{PLAYERS[ext]} #{filename}") if PLAYERS[ext]
File.open(LOG_FILE, 'a') do |log|
log.puts([time, filename].join("\t") + "\n")
end
end
dont_play_file_proc = lambda do |filename, time|
puts "I'm not playing #{filename}. So there."
end
class RadioPlayer
DIRS_TO_IGNORE = ['.', '..', 'CVS']
PICK_FROM_DIR_PROC = lambda do |dir, callback_proc, dir_filter|
(Dir.open(dir).entries - DIRS_TO_IGNORE).sort.each do |filename|
if ((filename =~ dir_filter) or not dir_filter)
item = "#{dir}/#{filename}"
if File.directory?(item)
PICK_FROM_DIR_PROC.call(
item, callback_proc, dir_filter
)
else
callback_proc.call(item, Time.now)
end
end
end
end
def self.walk(dir, callback_proc, dir_filter=nil)
puts
PICK_FROM_DIR_PROC.call(dir, callback_proc, dir_filter)
end
end
dir = 'extras/soundfiles'
callback = (ARGV[0] == 'play') ? play_file_proc : dont_play_file_proc
dir_filter = ARGV[1] ? Regexp.new(ARGV[1]) : nil
RadioPlayer.walk(dir, callback, dir_filter)
puts