Sha256: d3156f0d44262d917cd3454f8680936fde5b4870f571952d30174b2085cce346

Contents?: true

Size: 1.82 KB

Versions: 18

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'
require 'ehbrs_ruby_utils/fs/compressed_package'
require 'ehbrs/videos/extract/package_file'

module Ehbrs
  module Videos
    module Extract
      class Package
        enable_simple_cache

        common_constructor :path, :target_dir, :qualities do
          self.path = path.to_pathname
          self.target_dir = target_dir.to_pathname
        end

        delegate :to_s, to: :path

        def run(delete)
          selected_files.each(&:copy_to_selected_dir)
          files.each(&:move_to_quality_dir)
          extract_dir.rmdir
          path.unlink if delete
        end

        private

        def files_uncached
          ::Pathname.glob("#{extract_dir}/**/*").map do |file|
            ::Ehbrs::Videos::Extract::PackageFile.new(self, file)
          end
        end

        def extract_dir_uncached
          r = target_dir / path.basename
          raise "Extract directory \"#{r}\" is a file" if r.file?

          r.rmtree if r.directory?
          ::EhbrsRubyUtils::Fs::CompressedPackage.new(path).extract_to(r)
          r
        end

        def files_qualities_uncached
          qualities_with_default.select { |q| grouped_files.keys.include?(q) }
        end

        def grouped_files_uncached
          r = {}
          files.each do |file|
            r[file.quality] ||= []
            r[file.quality] << file
          end
          r
        end

        def qualities_with_default
          qualities + [::Ehbrs::Videos::Extract::PackageFile::DEFAULT_QUALITY]
        end

        def selected_dir_uncached
          target_dir / 'selected'
        end

        def selected_files
          files.select { |f| f.quality == selected_quality }
        end

        def selected_quality_uncached
          files_qualities.first
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
ehbrs-tools-0.39.0 lib/ehbrs/videos/extract/package.rb
ehbrs-tools-0.38.0 lib/ehbrs/videos/extract/package.rb
ehbrs-tools-0.37.0 lib/ehbrs/videos/extract/package.rb
ehbrs-tools-0.36.0 lib/ehbrs/videos/extract/package.rb
ehbrs-tools-0.35.1 lib/ehbrs/videos/extract/package.rb
ehbrs-tools-0.35.0 lib/ehbrs/videos/extract/package.rb
ehbrs-tools-0.34.0 lib/ehbrs/videos/extract/package.rb
ehbrs-tools-0.33.0 lib/ehbrs/videos/extract/package.rb
ehbrs-tools-0.32.0 lib/ehbrs/videos/extract/package.rb
ehbrs-tools-0.31.1 lib/ehbrs/videos/extract/package.rb
ehbrs-tools-0.31.0 lib/ehbrs/videos/extract/package.rb
ehbrs-tools-0.30.0 lib/ehbrs/videos/extract/package.rb
ehbrs-tools-0.29.0 lib/ehbrs/videos/extract/package.rb
ehbrs-tools-0.28.3 lib/ehbrs/videos/extract/package.rb
ehbrs-tools-0.28.2 lib/ehbrs/videos/extract/package.rb
ehbrs-tools-0.28.1 lib/ehbrs/videos/extract/package.rb
ehbrs-tools-0.28.0 lib/ehbrs/videos/extract/package.rb
ehbrs-tools-0.27.0 lib/ehbrs/videos/extract/package.rb