Added task to scan a channel's storage for videos to import
This commit is contained in:
parent
aea9415bbd
commit
89ebf5c792
5
Rakefile
5
Rakefile
@ -7,6 +7,11 @@ namespace :db do
|
||||
task :migrate do
|
||||
%x{sequel -m 'db/migrations/' 'sqlite://data/stgm.db'}
|
||||
end
|
||||
|
||||
task :import do
|
||||
channel = ENV['channel']
|
||||
puts %x{ruby scan.rb "#{channel}"}
|
||||
end
|
||||
end
|
||||
|
||||
namespace :server do
|
||||
|
54
scan.rb
Executable file
54
scan.rb
Executable file
@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
require 'pathname'
|
||||
require 'sequel'
|
||||
|
||||
require_relative 'lib/config.rb'
|
||||
|
||||
# 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
|
||||
require_relative 'lib/models/channel.rb'
|
||||
require_relative 'lib/models/video.rb'
|
||||
|
||||
unless ARGV.length == 1
|
||||
abort 'You must supply a channel name!'
|
||||
end
|
||||
|
||||
channel = Channel.where(name: ARGV[0]).first()
|
||||
channel_dir = channel.directory_path
|
||||
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[1..-1]
|
||||
|
||||
# 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()
|
||||
|
||||
# add video project to DB if there's no existing project
|
||||
if db_results.length == 0
|
||||
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"
|
||||
)
|
||||
|
||||
puts "Successfully added video ##{video.serial} - #{video.name} for channel '#{channel.name}' to the database."
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user