FileUtils.cp(path, "#{ATV_ROOT}/#{file}")
end
-pp JSON.parse(File.read("#{ATV_ROOT}/config.json"))
-exit 1
+#JSON.parse(File.read("#{ATV_ROOT}/config.json"))
+#exit 1
# Then run the server
db = ATV::Database.new(ATV_ROOT)
+
channels = [
ATV::Channel.new('Everything', db,
["Movies", "Shorts", "Shows"]),
class Channel
def initialize(name, db, selectors = [])
@name = name
+ @db = db
+ @selectors = selectors
+
@time = 0
@index = 0
- @db = db
- files = db.files
- @items = selectors.map do |sel|
- files.select {|f| f["path"].start_with? sel }
- end.flatten.shuffle
- @item_map = @items.map{|e| [e["path"], e]}.to_h
@queue = []
- @current = @items[@index] if @items.length > 0
+ @current = items[@index] if items.length > 0
end
+
+
def update(playing)
- return false if !@current || @items.length == 0
+ return false if !@current || items.length == 0
@time += 1 if playing
if @time >= @current["duration"] then
next_vid()
- pp @current
true
else
false
if @queue.length > 0
@current = @queue.shift
else
- @index = ((@index+1) >= @items.length ? 0 : (@index+1))
- @current = @items[@index]
+ @index = ((@index+1) >= items.length ? 0 : (@index+1))
+ @current = items[@index]
end
end
def state()
- next_index = ((@index+1) >= @items.length ? 0 : (@index+1))
- next_item = (@queue.length > 0 ? @queue.first : @items[next_index])
+ next_index = ((@index+1) >= items.length ? 0 : (@index+1))
+ next_item = (@queue.length > 0 ? @queue.first : items[next_index])
{
"chan" => @name,
"time" => @time,
end
def items()
- tree = {}
- @items.each do |item|
- node = tree
- path = item["path"].split("/")
- path[0..-2].each do |folder|
- node[folder] ||= {}
- node = node[folder]
- end
- node[path.last] = item
- end
-
- { "items" => tree }
+ { "items" => filedata[:tree] }
end
def queue()
end
def enqueue(path)
- @queue << @item_map[path] if @item_map[path]
+ @queue << filedata[:map][path] if filedata[:map][path]
+ end
+
+ private
+
+ def filedata()
+ @db.select_files(@name, @selectors)
+ end
+
+ def items()
+ filedata[:items].values
end
end
end
end
def load()
+ @cache = {}
if File.exist? @path
@data = JSON.parse(File.read(@path))
end
end
def scan()
+ # scan for videos
Dir.glob("#{@root}/**/*.{mp4,webm,ogg}").each do |f|
short_path = f.sub("#{@root}/", '')
next if @data[short_path]
@data[short_path] = { "duration" => duration }
end
end
+
+ def select_files(channel, selectors)
+
+ if @cache[channel].nil? then
+ @cache[channel] = { name: channel, items: {} }
+
+ tree = {}
+ @cache[channel][:items] = selectors.map do |sel|
+ @data.keys.select {|f| f.start_with? sel }
+ end.flatten.uniq.map do |e|
+ # Add item to the virtual directory tree
+ node = tree
+ path = e.split("/")
+ path[0..-2].each do |folder|
+ node[folder] ||= {}
+ node = node[folder]
+ end
+ node[path.last] = e
+
+ # Return the key/value pair
+ [e, { "path" => e, "duration" => @data[e]["duration"]}]
+ end.to_h
+
+ # save off the tree
+ @cache[channel][:tree] = tree
+ end
+ @cache[channel]
+ end
+
end
end