]> git.mdlowis.com Git - proto/atv.git/commitdiff
reworked channel/databse interface to make it easier to rescan
authorMike Lowis <mike.lowis@gentex.com>
Fri, 1 Mar 2024 20:52:15 +0000 (15:52 -0500)
committerMike Lowis <mike.lowis@gentex.com>
Fri, 1 Mar 2024 20:52:15 +0000 (15:52 -0500)
atv/bin/atv
atv/lib/atv/channel.rb
atv/lib/atv/database.rb

index ae1784dad5e8b43375d1ff0d996382e2818c56e4..b45a054a9b9f9e2b5f3e453e4859cc8097dea65c 100755 (executable)
@@ -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"]),
index 2600fa832e3f5581c671ea5683bb9a290aff8b17..42af495baf27ba6ed313ff77ce41290ed24db231 100644 (file)
@@ -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
index d675287b33034e07d632416b5816e2f3ce5fbb77..02f21a7466c6badad76ee3d31034b733bcc4cd9c 100644 (file)
@@ -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