]> git.mdlowis.com Git - proto/atv.git/commitdiff
switched sources over ro URIs, added an indexing script, and updated player to use...
authorMike Lowis <mike.lowis@gentex.com>
Mon, 1 Apr 2024 17:05:48 +0000 (13:05 -0400)
committerMike Lowis <mike.lowis@gentex.com>
Mon, 1 Apr 2024 17:05:48 +0000 (13:05 -0400)
atv/assets/index.html
atv/bin/atv-index
atv/lib/atv/database.rb

index 01f261b0b6a69e05ad370737f8627070e0cba174..b7315b5c2decc894db9170f5fe17e508b242ad62 100644 (file)
@@ -67,8 +67,7 @@ const updatePlayer = (prev)=>{
 
     // Check if we need to refresh the URI
     const diffTooBig = Math.abs(Math.floor(Video.currentTime) - elapsed) > 3;
-    const srcChanged = Video.src.replace(/#t=[0-9]+$/, '') != encodeURI(document.location + current["curr"]["path"]);
-
+    const srcChanged = Video.src.replace(/#t=[0-9]+$/, '') != current["curr"]["path"];
     const playStateChanged = prev["playing"] != current["playing"];
     const chanChanged = prev["chan"] != current["chan"];
 
index 094ced7b647811d5cb4e8142002879166f8cfb17..1970ebe6dfae03482a5fac84bb73675bad71c2c0 100644 (file)
@@ -1,7 +1,27 @@
 #!/bin/env ruby
 
+require 'json'
+
 TYPES = "{mp4,ogg,webm}"
-FILES = ARGV.map {|f| Dir.glob("#{f}/**/*.#{TYPES}") }.flatten
-FILES.each do |f|
-end
+FILES = []
+CMD="ffprobe -show_entries format=duration -v quiet -of csv=\"p=0\" -i"
 
+ARGV.each do |root|
+  # Generate the index
+  index = []
+  Dir.glob("#{root}/**/*.#{TYPES}").each do |path|
+    short_path = path.sub("#{root}/", '')
+    puts short_path
+    duration = `#{CMD} \"#{path}\"`.chomp.to_f
+    entry = {
+      "path" => short_path,
+      "duration" => duration
+    }
+    index << entry
+  end
+
+  # Save the index to disc
+  File.open("#{root}/index.json", "wb") do |f|
+    f.write JSON.dump(index)
+  end
+end
index 829376c5aef1d88432cb9ded11fbbc215f91cb9c..d96bfeea00523446efbc89eb191e1a4434a2bd43 100644 (file)
@@ -1,36 +1,36 @@
 require 'json'
+require 'open-uri'
 
 module ATV
   class Database
     CMD="ffprobe -show_entries format=duration -v quiet -of csv=\"p=0\" -i"
 
-    def initialize(root, scan_dirs = [])
+    def initialize(root, sources = [])
       @root = root
       @path = "#{root}/index.json"
       @data = {}
-      @dirs = scan_dirs
-      pp scan_dirs
+      @cache = {}
+      @sources = sources
       load()
     end
 
     def load()
-      @cache = {}
-      if File.exist? @path
-        @data = JSON.parse(File.read(@path))
+      @sources.each do |source|
+        uri = "#{source}/index.json"
+        begin
+          index = JSON.parse(URI.open(uri).read)
+          index.each do |item|
+            short_path   = item["path"]
+            item["path"] = URI::Parser.new.escape("#{source}/#{short_path}")
+            @data[short_path] = item
+          end
+        rescue Exception => e
+          puts "Failed to open URI #{uri}: #{e}"
+        end
       end
-      cleanup()
-      scan()
-      save()
       pp @data
     end
 
-    def save()
-      File.open(@path, "wb") do |f|
-        puts f
-        f.write JSON.dump(@data)
-      end
-    end
-
     def files
       @data.keys.map do |f|
         @data[f].merge({ "path" => f })
@@ -41,35 +41,13 @@ module ATV
       @data[key]
     end
 
-    def cleanup()
-      files.each do |f|
-        if not File.exist?("#{ATV_ROOT}/#{f["path"]}")
-          puts "Cleanup #{f["path"]}"
-          @data.delete(f["path"])
-        end
-      end
-    end
-
-    def scan()
-      # scan for videos
-      @dirs.each do |dir|
-        Dir.glob("#{@root}/#{dir}/**/*.{mp4,m4v,webm,ogg}").each do |f|
-          short_path = f.sub("#{@root}/", '')
-          next if @data[short_path]
-          puts f
-          duration = `#{CMD} \"#{f}\"`.chomp.to_f
-          @data[short_path] = { "duration" => duration }
-        end
-      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.sub(%r{[^/]+/}, '').start_with? sel }
+          @data.keys.select {|f| f.start_with? sel }
         end.flatten.uniq.map do |e|
           # Add item to the virtual directory tree
           node = tree
@@ -79,15 +57,16 @@ module ATV
             node = node[folder]
           end
 
-          obj = { "path" => e, "duration" => @data[e]["duration"]}
-          node[path.last] = obj
+          node[path.last] = @data[e]
 
           # Return the key/value pair
-          [e, obj]
+          [e, @data[e]]
         end.to_h
 
         # save off the tree
         @cache[channel][:tree] = tree
+        pp @cache[channel]
+
       end
       @cache[channel]
     end