class FYT::Storage

Manages file downloads and storage

Public Class Methods

new(path, format_options, output_format, proxy_manager) click to toggle source
# File lib/fyt/storage.rb, line 6
def initialize(path, format_options, output_format, proxy_manager)
  @path = path || ''
  @format_options = format_options
  @output_format = output_format
  @proxy_manager = proxy_manager
  @known_files = []
end

Public Instance Methods

add(item) click to toggle source
# File lib/fyt/storage.rb, line 14
def add(item)
  url = item.link.href

  path = path_for(item)
  download_file!(url, path)
  @known_files << File.basename(path)
  File.basename(path)
end
add_feed(name, feed) click to toggle source
# File lib/fyt/storage.rb, line 23
def add_feed(name, feed)
  logger.debug feed.to_s
  logger.debug @known_files

  File.join(@path, "#{name}.feed.rss20.xml").tap do |path|
    File.write(path, feed)

    @known_files << File.basename(path)
  end
end
cleanup!() click to toggle source
# File lib/fyt/storage.rb, line 34
def cleanup!
  logger.debug 'Files to delete:'
  logger.debug files_to_delete

  files_to_delete.each do |filename|
    delete_file! filename
  end
end
mtime(filename) click to toggle source
# File lib/fyt/storage.rb, line 43
def mtime(filename)
  File.mtime(File.join(@path, filename))
end
size(filename) click to toggle source
# File lib/fyt/storage.rb, line 47
def size(filename)
  File.size(File.join(@path, filename))
end

Private Instance Methods

delete_file!(filename) click to toggle source
# File lib/fyt/storage.rb, line 83
def delete_file!(filename)
  File.join(@path, filename).tap do |path|
    logger.debug "Deleting file: #{path}"
    File.delete(path)
  end
end
download_file!(url, output_path) click to toggle source
# File lib/fyt/storage.rb, line 53
def download_file!(url, output_path)
  proxy = @proxy_manager.get!

  options_string = [
    "-f '#{@format_options}'",
    "--merge-output-format '#{@output_format}'",
    "-o '#{output_path}'",
    "--proxy 'http://#{proxy.url}'",
    "'#{url}'"
  ].join(' ')

  logger.debug "Executing: youtube-dl #{options_string}"

  execute "timeout --preserve-status 300 youtube-dl #{options_string}"
rescue
  @proxy_manager.remove(proxy)

  download_file!(url, output_path) unless @proxy_manager.proxies.empty?
end
execute(command_string) click to toggle source
# File lib/fyt/storage.rb, line 73
def execute(command_string)
  IO.pipe do |read_io, write_io|
    break if system(command_string, out: write_io, err: write_io)

    write_io.close
    logger.debug read_io.read
    raise
  end
end
filename_for(item) click to toggle source
# File lib/fyt/storage.rb, line 90
def filename_for(item)
  "#{item.id.content}.mp4"
end
files_on_disk() click to toggle source
# File lib/fyt/storage.rb, line 98
def files_on_disk
  Dir.entries(@path).reject { |filename| filename.start_with? '.' }
end
files_to_delete() click to toggle source
# File lib/fyt/storage.rb, line 102
def files_to_delete
  files_on_disk - @known_files
end
path_for(item) click to toggle source
# File lib/fyt/storage.rb, line 94
def path_for(item)
  File.join(@path, filename_for(item))
end