-
Notifications
You must be signed in to change notification settings - Fork 13
/
streamer.rb
79 lines (67 loc) · 2.2 KB
/
streamer.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/ruby
# Stream all the files given on the commandline to the Icecast server on
# localhost.
require 'rubygems'
require 'shout'
require 'sequel'
$CONFIG = YAML.load_file('settings.yaml')
@station = $CONFIG[:shout_station]
# Connect to db
case $CONFIG[:database][:type]
when 'mysql'
DB = Sequel.connect($CONFIG[:database][:connect_string])
else
DB = Sequel.sqlite($CONFIG[:database][:file_name])
end
BLOCKSIZE = 16384
s = Shout.new
s.host = @station[:host]
s.port = @station[:port]
s.mount = @station[:mount]
s.user = @station[:user]
s.pass = @station[:pass]
s.name = @station[:name]
s.description = @station[:description]
s.genre = @station[:genre]
s.format = Shout::MP3
s.connect
loop do
songs = DB[:votes].select(:song_id, :filename, :title, :artist, :genre, :album, :year, :COUNT.sql_function(:song_id), :MIN.sql_function(:voted_at).as(:voted_at)).join(:songs, :id => :song_id).group(:song_id).order(:COUNT.sql_function(:song_id).desc, :voted_at.asc)
case $CONFIG[:database][:type]
when 'mysql'
songs = DB[:songs].select(:id.as(:song_id), :filename, :title, :artist, :album, :genre, :year).order(:RAND.sql_function()) if songs.empty?
else
songs = DB[:songs].select(:id.as(:song_id), :filename, :title, :artist, :album, :genre, :year).order(:RANDOM.sql_function()) if songs.empty?
end
song = songs.first
if not song
puts 'Database is empty, please run indexer.rb'
Process.exit
end
votes = DB[:votes].filter(:song_id => song[:song_id])
if votes
votes.each do |v|
DB[:votes_archives].insert(:song_id => v[:song_id], :user_id => v[:user_id], :voted_at => v[:voted_at])
end
votes.delete
end
plays = DB[:plays]
plays.insert(:song_id => song[:song_id], :played_at => Time.now)
puts "sending data from #{song[:filename]}"
m = ShoutMetadata.new
m.add 'filename', song[:filename]
m.add 'title', song[:title]
m.add 'artist', song[:artist]
m.add 'genre', song[:genre]
m.add 'album', song[:album]
m.add 'year', song[:year]
s.metadata = m
filename = song[:filename]
File.open(filename) do |file|
while data = file.read(BLOCKSIZE)
s.send data
s.sync
end
end
end
s.disconnect