132 lines
2.8 KiB
Ruby
132 lines
2.8 KiB
Ruby
class BotController < ApplicationController
|
|
require 'daemons'
|
|
|
|
PIDS_PATH = "#{Rails.root.to_s}/tmp/pids"
|
|
|
|
@@accounts = YAML.load_file("#{Rails.root.to_s}/config/vk_accounts.yml")
|
|
@error = ""
|
|
|
|
def list
|
|
@bots = []
|
|
@@accounts.each do |bot_name, data|
|
|
bot = Bot.new(bot_name)
|
|
@bots << {
|
|
:name => bot_name,
|
|
:started => bot.running?,
|
|
:current_track => bot.getCurrentTrack,
|
|
:started_at => bot.running? ? bot.startedAt : nil
|
|
}
|
|
end
|
|
pp @bots
|
|
end
|
|
|
|
def stats
|
|
end
|
|
|
|
|
|
def queue
|
|
bot_name = params['bot_name']
|
|
group_by_artist = params['group_by_artist']
|
|
limit = params['limit']
|
|
|
|
if not bot_name.nil?
|
|
if not @@accounts.include?(bot_name)
|
|
throw "Wrong bot name."
|
|
end
|
|
end
|
|
if not group_by_artist.nil?
|
|
group_by_artist = true
|
|
end
|
|
if params['limit'].to_i != 0
|
|
limit = params['limit'].to_i
|
|
end
|
|
|
|
statuses = [0, 1, 4]
|
|
data = ParseQueue.any_in(status: statuses)
|
|
if not bot_name.nil?
|
|
data = data.where(:bot_name => bot_name)
|
|
end
|
|
# if group_by_artist
|
|
# data = data.only(:artist_name).group
|
|
# end
|
|
if not limit.nil?
|
|
data = data.limit(limit)
|
|
end
|
|
@queue = []
|
|
unless data.nil?
|
|
data.each do |track|
|
|
@queue << track
|
|
end
|
|
end
|
|
pp @queue
|
|
end
|
|
|
|
def start
|
|
name = params['name']
|
|
begin
|
|
if name.nil? or name.strip.empty?
|
|
throw "Empty bot name"
|
|
end
|
|
|
|
bot = Bot.new(name)
|
|
bot.start
|
|
rescue
|
|
return render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
|
|
end
|
|
end
|
|
|
|
def stop
|
|
name = params['name']
|
|
begin
|
|
if name.nil? or name.strip.empty?
|
|
throw "Empty bot name"
|
|
end
|
|
bot = Bot.new(name)
|
|
bot.stop
|
|
rescue
|
|
return render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
|
|
end
|
|
end
|
|
|
|
# Add artist to parse_queue
|
|
def add
|
|
name = params['name']
|
|
if name.nil? or name.strip.empty?
|
|
redirect_to '/'
|
|
end
|
|
|
|
artist = Artist.getByName(name)
|
|
if artist.nil?
|
|
@error = "There is no such artist."
|
|
else
|
|
data = ParseQueue.where(:artist_id => artist.id).first
|
|
if not data.nil?
|
|
throw "This artist already exist in queue."
|
|
else
|
|
tracks = []
|
|
artist.albums.each do |album|
|
|
unless album.releases.empty?
|
|
album.releases.first.tracks.each do |track|
|
|
tracks << track
|
|
end
|
|
end
|
|
end
|
|
tracks.each do |track|
|
|
ParseQueue.collection.insert({
|
|
:track_id => track.id,
|
|
:artist_id => artist.id,
|
|
:artist_name => artist.name,
|
|
:track_name => track.name,
|
|
:bot_name => nil,
|
|
:status => 0,
|
|
:date => nil,
|
|
:times_failed => 0
|
|
})
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
end
|
|
|