]> git.mdlowis.com Git - proto/atv.git/commitdiff
added support for multipl sources and a config file
authorMike Lowis <mike.lowis@gentex.com>
Thu, 14 Mar 2024 16:57:28 +0000 (12:57 -0400)
committerMike Lowis <mike.lowis@gentex.com>
Thu, 14 Mar 2024 16:57:28 +0000 (12:57 -0400)
atv/bin/atv
atv/lib/atv/database.rb

index f75e788811afe18203d5d6fe1135d696a4a9b236..88ad9caaa0a1b93cc19c0f0dbee1b3e2e6c12d21 100755 (executable)
@@ -8,6 +8,41 @@ require 'fileutils'
 
 ATV_ROOT = (ENV["ATV_ROOT"] || "/var/www/atv")
 ASSET_DIR = File.join(File.dirname(__FILE__), "../assets")
+CONFIG_PATH = "#{ATV_ROOT}/config.json"
+DEFAULT_CONFIG = {
+  "sources" => [
+    "local",
+    "nas"
+  ],
+
+ "channels" => [
+   {
+     "name" => "Everything",
+     "randomize" => false,
+     "play_ads"  => true,
+     "selectors" => [
+       "Movies",
+       "Shorts",
+       "Shows",
+       "Photos",
+     ]
+   },
+   {
+     "name" => "Christmas",
+     "randomize" => true,
+     "selectors" => [
+       "Christmas"
+     ]
+   },
+ ]
+}
+
+# load the config or use teh default config
+if File.exist? CONFIG_PATH
+  CONFIG = JSON.parse(File.read(CONFIG_PATH))
+else
+  CONFIG = DEFAULT_CONFIG
+end
 
 # Update website files first
 Dir.glob("#{ASSET_DIR}/**/*.*").each do |path|
@@ -17,13 +52,11 @@ Dir.glob("#{ASSET_DIR}/**/*.*").each do |path|
 end
 
 # Then run the server
-db = ATV::Database.new(ATV_ROOT)
-
-channels = [
-  ATV::Channel.new('Everything', db,
-    ["Movies", "Shorts", "Shows"]),
-  ATV::Channel.new('Christmas', db,
-    ["Christmas"]),
-]
+db = ATV::Database.new(ATV_ROOT, CONFIG["sources"])
+
+channels = CONFIG["channels"].map do |c|
+  ATV::Channel.new(c["name"], db, c["selectors"])
+end
+
 player = ATV::Player.new(db, channels)
 ATV::Server.start(player)
index eb0d98ebc804493c6c354dcd51add221ac5d568b..d9342a7e61babe85a8346791d430176e5daee627 100644 (file)
@@ -4,10 +4,11 @@ module ATV
   class Database
     CMD="ffprobe -show_entries format=duration -v quiet -of csv=\"p=0\" -i"
 
-    def initialize(root)
+    def initialize(root, scan_dirs = [])
       @root = root
       @path = "#{root}/index.json"
       @data = {}
+      @dirs = scan_dirs
       load()
     end
 
@@ -45,27 +46,28 @@ module ATV
 
     def scan()
       # scan for videos
-      Dir.glob("#{@root}/**/*.{mp4,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 }
+      @dirs.each do |dir|
+        Dir.glob("#{@root}/#{dir}/**/*.{mp4,webm,ogg}").each do |f|
+          short_path = f.sub(%r{[^/]+/}, '').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.start_with? sel }
+          @data.keys.select {|f| f.sub(%r{[^/]+/}, '').start_with? sel }
         end.flatten.uniq.map do |e|
           # Add item to the virtual directory tree
           node = tree
-          path = e.split("/")
+          path = e.sub(%r{[^/]+/}, '').split("/")
           path[0..-2].each do |folder|
             node[folder] ||= {}
             node = node[folder]