From: Mike Lowis Date: Thu, 14 Mar 2024 16:57:28 +0000 (-0400) Subject: added support for multipl sources and a config file X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=4b621bcaf0822d73abb4c6948283a46ceb417f20;p=proto%2Fatv.git added support for multipl sources and a config file --- diff --git a/atv/bin/atv b/atv/bin/atv index f75e788..88ad9ca 100755 --- a/atv/bin/atv +++ b/atv/bin/atv @@ -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) diff --git a/atv/lib/atv/database.rb b/atv/lib/atv/database.rb index eb0d98e..d9342a7 100644 --- a/atv/lib/atv/database.rb +++ b/atv/lib/atv/database.rb @@ -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]