stage-manager/scan.rb

58 lines
1.6 KiB
Ruby
Raw Permalink Normal View History

#!/usr/bin/env ruby
# frozen_string_literal: true
require 'pathname'
require 'sequel'
require_relative 'app/config'
# Load configuration file
conf = Config.new(File.join(__dir__, 'data/defaults.yaml'))
# Load the Sequel timestamps plugin
Sequel::Model.plugin(:timestamps)
# Initialize Sequel gem for database actions
DB = Sequel.connect(adapter: conf.get('database.adapter'), database: conf.get('database.database'))
# Load models
Dir.glob('app/models/*.rb').sort().each { |f| require f }
# fail if the script is being called incorrectly
abort('You must supply a channel name!') unless ARGV.length == 1
channel = Channel.where(name: ARGV[0]).first()
channel_dir = File.join(channel.directory_path, 'Main')
subs = Dir["#{channel_dir}/*"]
subs.each do |d|
# get folder basename
dir_name = File.basename(d)
# parse video serial from folder name
serial_raw = dir_name[0..5].strip()
video_serial = serial_raw.slice(0)
# parse video name from folder name
video_name = dir_name.split(' - ')[1]
# check if a video by the same serial number exists for the channel
db_results = Video.where(serial: video_serial, channel_id: channel.id).all()
# skip to next video if the video already exists
next unless db_results.empty?()
# add video project to DB if there's no existing project
video = Video.create(
serial: video_serial,
name: video_name,
channel_id: channel.id,
directory_path: d,
description: 'TODO - imported from storage.',
script: "# Introduction\n\n# Body\n\n# Conclusions"
)
video.import_script()
puts "Successfully added video ##{video.serial} - #{video.name} for channel '#{channel.name}' to the database."
end