-
Notifications
You must be signed in to change notification settings - Fork 2
/
autoplay.rb
58 lines (53 loc) · 1.46 KB
/
autoplay.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
require 'resque'
require 'json'
require 'excon'
require_relative 'jobs/audio_extract_job'
require_relative 'jobs/audio_play_job'
require_relative 'jobs/search_job'
class Queue
def self.queue
queue = (0...Resque.size('playlist')).map do |i|
Resque.peek('playlist', i)['args']
end
end
end
QUEUE_HOST = ENV['QUEUE_HOST'] || 'https://curlyq.herokuapp.com'
CONTEXT = ENV['CONTEXT'] || 'default'
APP_HOST = ENV['APP_HOST'] || 'http://barchord.app'
while true do
# Autoplay
begin
puts "checking queue for tracks"
queue = Queue.queue
puts "found: #{queue}"
puts "checking queue size"
if queue.empty?
puts "queue empty"
tracks = Dir["./*.mp3"]
puts "tracks: #{tracks}"
track = tracks.sample
puts "track: #{track}"
track_id = track.split('.')[1].split('/')[1]
puts "track_id: #{track_id}"
track = nil
`touch db.txt`
File.open("db.txt", "r").each_line do |line|
record = JSON.parse(line)
if record['id'] == track_id
track = record
break
end
end
if track
Excon.post("#{APP_HOST}/api/playlist", query: {
context: CONTEXT, fulltitle: track['fulltitle'], id: track['id'], img: track['img']
})
Resque.enqueue(AudioPlayJob, track['id'], Time.now.to_i, track['fulltitle'], track['img'])
end
end
rescue => error
puts 'an error occurred attempting random autoplay:'
puts error
end
sleep 2
end