All pastes #909364 Raw Edit

move_n_sort

public text v1 · immutable
·published 2008-02-19 02:11 UTC
rendered paste body
################################################################################
#                                                                              #
# ############################################################################ #
# # Title       : move_n_sort                                                # #
# # Description : Copies, parses, sorts.                                     # #
# # Coded by    : Justin Lee                                                 # #
# # Version     : 0.0.1-RC2                                                  # #
# # Date        : Monday, February 17, 2008                                  # #
# # License     : GNU General Public License v.2                             # #
# #               Copyright (C) 2008  Justin Lee  <kool.name at gmail.com>   # #
# ############################################################################ #
#                                                                              #
# ############################################################################ #
# # This program is free software; you can redistribute it and/or            # #
# # modify it under the terms of the GNU General Public License version 2    # #
# # as published by the Free Software Foundation.                            # #
# #                                                                          # #
# # This program is distributed in the hope that it will be useful,          # #
# # but WITHOUT ANY WARRANTY; without even the implied warranty of           # #
# # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            # #
# # GNU General Public License for more details.                             # #
# #                                                                          # #
# # You should have received a copy of the GNU General Public License        # #
# # along with this program; if not, write to the Free Software              # #
# # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA            # #
# # 02110-1301, USA.  Or, see: <http://www.gnu.org/licenses/>                # #
# ############################################################################ #
#                                                                              #
################################################################################

use strict;
use warnings;

use File::Basename;
use IO::Handle;
use FileHandle;
use File::Copy qw(cp);
use File::Find;
use Image::MetaData::JPEG;
use Getopt::Long qw(:config bundling);
use Tie::Simple;
use constant {
  NAME => basename($0),
  HOME => @{[ fileparse($0) ]}[1]
};
#use lib HOME . 'lib';

my (
  $use_cli_progress_bar,    # Use a crazy text-only progress bar. (Cross platform.)
  $use_gfx_progress_bar,    # Use Win32::GUI (for now) to create a graphical progress bar.
  $use_hidden_root_window,  # Use Win32::GUI (for always) to hide the CMD window.
  $use_verbose,             # Display the log file in real time, as it's created.
  $no_logging,              # Disable saving of the log file... Not recomended.
  $raw_dir_orig,            # Specify where the camera stores it's pictures.
  $raw_dir_new,             # Specify where raw pictures are copied to for compressing.
) = (
  '','','',
  '','',

  join('/',split(/\\|\//,$ENV{USERPROFILE},'Desktop/pics')),
  join('/',split(/\\|\//,$ENV{USERPROFILE},'Desktop/new_pics'))
);

GetOptions(
  'progress-bar|P'      => \$use_cli_progress_bar,
  'gfx-progress-bar'    => \$use_gfx_progress_bar,
  'hide-window|w'       => \$use_hidden_root_window,
  'verbose|v!'          => \$use_verbose,

  'raw-directory|R=s'   => \$raw_dir_orig,
  'local-directory|L=s' => \$raw_dir_new,

  'no-logging'          => \$no_logging,

#  'help|h|H'            => \&print_help
);

if ($use_hidden_root_window) {
  require Win32::GUI;
  my $root = Win32::GUI::GetPerlWindow();
  Win32::GUI::Hide($root);
}

 # fill our pipe
my  ($inbox, $outbox);
pipe $inbox, $outbox or die;
             $outbox->autoflush(1);

 # and make sure we got a lighter
tie my $processing, 'Tie::Simple', $inbox,
  FETCH => sub {
    my $self = shift;
    die unless ref $self;
    chomp(my $in = <$self>);

    return   0 unless $in;
    return   0 if $in =~ /^(?:0|n(?:o(?:pe)?)?|done)$/io;
    return   1 if $in =~ /^(?:1|y(?:e(?:a?h|s)|up)?|copy(?:ing)?|start(?:up)?)$/io;
    return $in;
  };

 # enter new raw directory
chdir($raw_dir_new) or die $!;
unlink(<*>)         or warn $!;

if (my $pid = fork) {
  sleep 1;

  if ($processing) {
    while ($processing) {
      my $path  = $processing;

       # get date
      my  $image =  new Image::MetaData::JPEG ($path);
      my  $exif  =  $image->get_Exif_data('IMAGE_DATA', 'TEXTUAL');
      my ($date) =  split(/\s+/,$exif->{DateTimeOriginal}->[0]);
          $date  =~ s/:/-/go;

       # make if check fails
      unless (-d $date) {
        mkdir($date,0777) or die;
      }

       # move the pic
      rename($path,join('/',$date,$path));
    }
  }
} elsif (defined $pid) {

   # start smoking
  print $outbox "start\n";

  find(
    {
      no_chdir => 1,
      wanted   => sub {
        next unless $_ =~ /jpe?g$/oi;
        print $outbox "copying\n";
        print $outbox move($_), "\n";
      }
    },
    $raw_dir_orig
  );

  print $outbox "done\n";
} else {
  exit 1;
}

 # pretty *drools*
sub move {
  my $o_pth  =  shift;
  my $n_pth  =  $raw_dir_new . '/' . basename($o_path);
  my $o_fh   =  new FileHandle $o_path, "<";
  my $n_fh   =  new FileHandle $n_path, ">";

  return unless defined $o_fh;
  return unless defined $n_fh;
  return unless cp($o_fh,$n_fh);

                undef $o_fh;
                undef $n_fh;

  return unless unlink($o_path);
  return        basename($n_path);
}

exit 0;