From: Mike Lowis Date: Fri, 1 Mar 2024 20:52:15 +0000 (-0500) Subject: reworked channel/databse interface to make it easier to rescan X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=ff8b195d40ac452f1fa1dac65a3ef176b5d8d277;p=proto%2Fatv.git reworked channel/databse interface to make it easier to rescan --- diff --git a/atv/bin/atv b/atv/bin/atv index ae1784d..b45a054 100755 --- a/atv/bin/atv +++ b/atv/bin/atv @@ -16,11 +16,12 @@ Dir.glob("#{ASSET_DIR}/**/*.*").each do |path| 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"]), diff --git a/atv/lib/atv/channel.rb b/atv/lib/atv/channel.rb index 2600fa8..42af495 100644 --- a/atv/lib/atv/channel.rb +++ b/atv/lib/atv/channel.rb @@ -2,24 +2,22 @@ module ATV 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 @@ -31,14 +29,14 @@ module ATV 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, @@ -48,18 +46,7 @@ module ATV 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() @@ -67,7 +54,17 @@ module ATV 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 diff --git a/atv/lib/atv/database.rb b/atv/lib/atv/database.rb index d675287..02f21a7 100644 --- a/atv/lib/atv/database.rb +++ b/atv/lib/atv/database.rb @@ -12,6 +12,7 @@ module ATV end def load() + @cache = {} if File.exist? @path @data = JSON.parse(File.read(@path)) end @@ -43,6 +44,7 @@ module ATV 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] @@ -51,5 +53,34 @@ module ATV @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