Skip to content

Instantly share code, notes, and snippets.

@keithrbennett
Last active October 11, 2019 01:12
Show Gist options
  • Save keithrbennett/4d9953e66ea35e2c52abae52650ebb1b to your computer and use it in GitHub Desktop.
Save keithrbennett/4d9953e66ea35e2c52abae52650ebb1b to your computer and use it in GitHub Desktop.

Revisions

  1. keithrbennett revised this gist Oct 7, 2019. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions organize-av-files
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,13 @@
    #!/usr/bin/env ruby

    # organize-av-files - Organizes files playable by mplayer
    # into 'saves', 'deletes', and 'undecideds' subdirectories.
    # into 'saves', 'deletes', and 'undecideds' subdirectories
    # of the current working directory.
    #
    # Be careful, if you specify files to process in multiple directories,
    # they will all be moved to the same subdirectories, so they will no
    # longer be organized by directory, and if there are multiple files
    # of the same name, some may be lost if overwritten.
    #
    # stored at:
    # https://gist.github.com/keithrbennett/4d9953e66ea35e2c52abae52650ebb1b
    @@ -84,7 +90,7 @@ def greeting
    Run `man mplayer` for more on mplayer.
    Assumes all files in the current directory are files playable by mplayer.
    Assumes all files specified are playable by mplayer.
    Creates subdirectories in the current directory: deletes, saves, undecideds.
    Logs to file '#{LOG_FILESPEC}'
  2. keithrbennett revised this gist Sep 25, 2019. 1 changed file with 30 additions and 18 deletions.
    48 changes: 30 additions & 18 deletions organize-av-files
    Original file line number Diff line number Diff line change
    @@ -1,30 +1,34 @@
    #!/usr/bin/env ruby

    # organize-av-files - Organizes files playable by mplayer
    # into 'saves', 'deletes', and 'undecideds' subdirectories.
    #
    # stored at:
    # https://gist.github.com/keithrbennett/4d9953e66ea35e2c52abae52650ebb1b


    require 'date'
    require 'fileutils'
    require 'set'

    LOG_FILESPEC = 'organize-av-files.log'

    def create_dirs
    %w{deletes saves undecideds}.each { |dir| FileUtils.mkdir_p(dir) }
    end


    def prompt(filename)
    "Playing #{filename}. Use cursor keys to navigate, 'q' to finish viewing/listening to file."
    end


    def check_presence_of_mplayer
    if `which mplayer`.chomp.size == 0
    raise "mplayer not detected. Please install it (with apt, brew, yum, etc.)"
    raise "mplayer not detected. "
    "Please install it (with apt, brew, yum, etc.)"
    end
    end


    # Takes all ARGV elements, expands any wildcards, converts to normalized (absolute) form,
    # Takes all ARGV elements, expands any wildcards,
    # converts to normalized (absolute) form,
    # and eliminates duplicates.
    # Note: Ruby's `Dir#[]` does not understand `~`
    def files_to_process

    # Dir[] does not understand ~, need to process it ourselves.
    @@ -35,14 +39,17 @@ def files_to_process
    : filespec
    end

    # When Dir[] gets a directory it returns no files. Need to add '/*' to it.
    # When Dir[] gets a directory it returns no files.
    # Need to add '/*' to it.
    add_star_to_dirspec_if_needed = ->(filespec) do
    File.directory?(filespec) \
    ? File.join(filespec, '*') \
    : filespec
    end

    ARGV[0] ||= '*' # default to all files in current directory but not its subdirectories
    # Default to all nonhidden files in current directory
    # but not its subdirectories.
    ARGV[0] ||= '*'

    all_filespecs = ARGV.each_with_object(Set.new) do |filemask, all_filespecs|
    filemask = replace_tilde_if_needed.(filemask)
    @@ -61,7 +68,7 @@ end

    def greeting
    puts <<~GREETING
    process-av-files
    organize-av-files
    Enables the vetting of audio and video files.
    @@ -79,41 +86,47 @@ def greeting
    Assumes all files in the current directory are files playable by mplayer.
    Creates subdirectories in the current directory: deletes, saves, undecideds.
    Logs to hidden directory '.process-av-files.log'
    Logs to file '#{LOG_FILESPEC}'
    GREETING
    end


    def play_file(filespec)
    # If you have problems with mplayer, remove the redirection ("2> /dev/null")
    # If you have mplayer problems, remove the redirection ("2> /dev/null")
    # to see any errors.
    `mplayer #{filespec} 2> /dev/null`
    end


    def disposition_prompt(filespec)
    "#{filespec}: s = save, d = delete, u = undecided: "
    "\n\n#{filespec}:\ns = save, d = delete, u = undecided, q = quit: "
    end


    def get_disposition_from_user
    loop do
    response = $stdin.gets.chomp.downcase
    if %w(s d u).include?(response)

    if response == 'q'
    exit
    elsif %w(s d u).include?(response)
    return {
    's' => 'saves',
    'd' => 'deletes',
    'u' => 'undecideds'
    }[response]
    else
    print "s = save, d = delete, u = undecided: "
    print "s = save, d = delete, u = undecided, q = quit: "
    end
    end
    end


    def log(filespec, destination_subdir)
    `echo "#{destination_subdir[0].upcase} #{Time.now} #{filespec}" >> process-av-files.log`
    dest_abbrev = destination_subdir[0].upcase # 'S' for saves, etc.
    log_message = "#{dest_abbrev} #{Time.now} #{filespec}"
    `echo #{log_message} >> #{LOG_FILESPEC}`
    end


    @@ -132,4 +145,3 @@ end


    main

  3. keithrbennett renamed this gist Sep 24, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. keithrbennett revised this gist Sep 24, 2019. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions organize-av-files.rb
    Original file line number Diff line number Diff line change
    @@ -113,9 +113,7 @@ def get_disposition_from_user


    def log(filespec, destination_subdir)
    File.open('.process-av-files.log', 'w+') do |file|
    file << "#{destination_subdir[0].upcase} #{Date.iso8601} #{filespec}\n"
    end
    `echo "#{destination_subdir[0].upcase} #{Time.now} #{filespec}" >> process-av-files.log`
    end


    @@ -134,3 +132,4 @@ def main


    main

  5. keithrbennett created this gist Sep 24, 2019.
    136 changes: 136 additions & 0 deletions organize-av-files.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,136 @@
    #!/usr/bin/env ruby

    require 'date'
    require 'fileutils'
    require 'set'


    def create_dirs
    %w{deletes saves undecideds}.each { |dir| FileUtils.mkdir_p(dir) }
    end


    def prompt(filename)
    "Playing #{filename}. Use cursor keys to navigate, 'q' to finish viewing/listening to file."
    end


    def check_presence_of_mplayer
    if `which mplayer`.chomp.size == 0
    raise "mplayer not detected. Please install it (with apt, brew, yum, etc.)"
    end
    end


    # Takes all ARGV elements, expands any wildcards, converts to normalized (absolute) form,
    # and eliminates duplicates.
    # Note: Ruby's `Dir#[]` does not understand `~`
    def files_to_process

    # Dir[] does not understand ~, need to process it ourselves.
    # This does *not* handle the `~username` form.
    replace_tilde_if_needed = ->(filespec) do
    filespec.start_with?('~/') \
    ? File.join(ENV['HOME'], filespec[2, -1]) \
    : filespec
    end

    # When Dir[] gets a directory it returns no files. Need to add '/*' to it.
    add_star_to_dirspec_if_needed = ->(filespec) do
    File.directory?(filespec) \
    ? File.join(filespec, '*') \
    : filespec
    end

    ARGV[0] ||= '*' # default to all files in current directory but not its subdirectories

    all_filespecs = ARGV.each_with_object(Set.new) do |filemask, all_filespecs|
    filemask = replace_tilde_if_needed.(filemask)
    filemask = add_star_to_dirspec_if_needed.(filemask)

    Dir[filemask] \
    .map { |f| File.absolute_path(f) } \
    .select { |f| File.file?(f) } \
    .each do |filespec|
    all_filespecs << filespec
    end
    end
    all_filespecs.sort
    end


    def greeting
    puts <<~GREETING
    process-av-files
    Enables the vetting of audio and video files.
    For each file, plays it with mplayer, and prompts for what you would like to do
    with that file, moving the file to one of the following subdirectories:
    * deletes
    * saves
    * undecideds
    This software uses mplayer to play audio files. Use cursor keys to move forwards/backwards in time.
    Press 'q' or 'ESC' to abort playback and specify disposition of that file.
    Run `man mplayer` for more on mplayer.
    Assumes all files in the current directory are files playable by mplayer.
    Creates subdirectories in the current directory: deletes, saves, undecideds.
    Logs to hidden directory '.process-av-files.log'
    GREETING
    end


    def play_file(filespec)
    # If you have problems with mplayer, remove the redirection ("2> /dev/null")
    `mplayer #{filespec} 2> /dev/null`
    end


    def disposition_prompt(filespec)
    "#{filespec}: s = save, d = delete, u = undecided: "
    end


    def get_disposition_from_user
    loop do
    response = $stdin.gets.chomp.downcase
    if %w(s d u).include?(response)
    return {
    's' => 'saves',
    'd' => 'deletes',
    'u' => 'undecideds'
    }[response]
    else
    print "s = save, d = delete, u = undecided: "
    end
    end
    end


    def log(filespec, destination_subdir)
    File.open('.process-av-files.log', 'w+') do |file|
    file << "#{destination_subdir[0].upcase} #{Date.iso8601} #{filespec}\n"
    end
    end


    def main
    check_presence_of_mplayer
    create_dirs
    puts greeting
    files_to_process.each do |filespec|
    play_file(filespec)
    print disposition_prompt(filespec)
    destination_subdir = get_disposition_from_user
    `mv #{filespec} #{destination_subdir}`
    log(filespec, destination_subdir)
    end
    end


    main