Dotfiles, utilities, and other apparatus.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
2.4 KiB

13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
12 years ago
12 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
  1. #!/usr/bin/env perl
  2. =pod
  3. =head1 NAME
  4. photocp - copy files from digital camera media to my home dir
  5. =head1 SYNOPSIS
  6. # try a couple of defaults
  7. photocp
  8. # look in source_directory
  9. photocp [source_directory]
  10. =head1 DESCRIPTION
  11. This operates on a handful of assumptions, some of which are becoming obsolete
  12. as "cameras" are mostly phones these days:
  13. =over
  14. =item A given source will have a bunch of unique filenames.
  15. =item You're only likely to be copying from one source on a given day.
  16. =item You want to copy all of the files on the source (this is the big one).
  17. =back
  18. =head1 AUTHOR
  19. Brennen Bearnes
  20. =cut
  21. use strict;
  22. use warnings;
  23. use 5.10.0;
  24. use POSIX qw(strftime);
  25. use File::Basename;
  26. use File::Copy;
  27. use File::Find;
  28. use File::HomeDir;
  29. my $home = File::HomeDir->my_home;
  30. my $archive_dir = $home . '/workspace/photos';
  31. unless (-d $archive_dir) {
  32. die "$archive_dir is not a directory";
  33. }
  34. my @sources = (
  35. '/media/' . $ENV{'USER'} . '/EOS_DIGITAL',
  36. '/media/' . $ENV{'USER'} . '/CANON_DC',
  37. '/media/' . $ENV{'USER'} . '/NIKON D600',
  38. '/media/' . $ENV{'USER'} . '/NO NAME',
  39. '/media/' . $ENV{'USER'} . '/9016-4EF8',
  40. '/media/CANON_DC',
  41. '/media/EOS_DIGITAL',
  42. '/media/disk/DCIM',
  43. '/media/NIKON D600',
  44. '/run/user/1000/gvfs/' # XXX: this is fairly stupid.
  45. );
  46. my @t = localtime;
  47. my $daydir = strftime("%Y-%m-%d", @t);
  48. my $src;
  49. if ((defined $ARGV[0]) && (-d $ARGV[0])) {
  50. $src = $ARGV[0];
  51. } else {
  52. foreach my $source (@sources) {
  53. if (-d $source) {
  54. $src = $source;
  55. last;
  56. }
  57. }
  58. unless ($src) {
  59. die 'source not mounted? tried ' . join(' ', @sources);
  60. }
  61. }
  62. my $dest = $archive_dir . '/' . $daydir;
  63. if ((-e $dest) && (! -d $dest)) {
  64. die 'target exists but is not a directory';
  65. }
  66. if (! -d $dest) {
  67. say "creating $dest";
  68. mkdir($dest);
  69. } else {
  70. say "$dest exists";
  71. }
  72. my $count = 0;
  73. # XXX: What this doesn't *really* handle is the case where you sync
  74. # from multiple cameras and they have overlapping filenames for
  75. # the same sync date.
  76. find(
  77. sub {
  78. if ( /[.] (jpe?g | mpe?g | mp4 | avi | raw | cr2 | tiff? | bmp | png | gif | mov) $/ix ) {
  79. $count++;
  80. my $copy_to = "$dest/$_";
  81. if (-e $copy_to) {
  82. say "$copy_to already exists - skipping copy";
  83. } else {
  84. say "copying $File::Find::name to $copy_to";
  85. copy($File::Find::name, $copy_to)
  86. or die "copy failed: $!";
  87. }
  88. }
  89. },
  90. $src
  91. );
  92. say "$count files copied";