Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Organize you MP3 backup

by rlb3 (Deacon)
on Aug 02, 2003 at 18:58 UTC ( #280310=perlcraft: print w/ replies, xml ) Need Help??

   1: =head1 DESCRIPTION
   2: 
   3: Hello Monks,
   4:     I figure the only way I'm going to get better at perl is
   5:  by writing code and getting comments on it, so here is a
   6:  module to scratch a itch I had.
   7: 
   8:     I've got lots of Mp3s. More that one CDs worth anyway. 
   9: So I needed something to separate them into CD sized directories.
  10: So here is my latest offering.
  11: 
  12: 
  13: package Mp3::Backup;
  14: use strict;
  15:                                                                                                                               
  16: BEGIN {
  17:         use Exporter ();
  18:         use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  19:         $VERSION     = 0.01;
  20:         @ISA         = qw (Exporter);
  21:         #Give a hoot don't pollute, do not export more than needed by default
  22:         @EXPORT      = qw ();
  23:         @EXPORT_OK   = qw ();
  24:         %EXPORT_TAGS = ();
  25: }
  26:                                                                                                                               
  27: use File::Find;
  28: use File::Copy;
  29: use Data::Dumper;
  30: 
  31:                                                                                                                               
  32: sub new {
  33:         my $class = shift;
  34:                                                                                                                               
  35:         my $ref = {
  36:                 MP3DIR       => shift,                 # The directory to backup
  37:                 BACKUPDIR    => shift,                 # Where to backup the mp3s
  38:                 MAXSIZE      => (shift) * 1024 * 1024, # Size in kilobyes each disk should be
  39:                 DEBUG        => shift || 0,            # Set debug status;
  40:                 FILELIST     => {},                    # Hash of all mp3s
  41:         };
  42:                                                                                                                               
  43:         my $self = bless ($ref, ref ($class) || $class);
  44:                                                                                                                               
  45:         return ($self);
  46: }
  47: 
  48: # The sub builds the hash of mp3s to be backed up
  49: sub buildFileList {
  50:         my $class = shift;
  51:                                                                                                                               
  52:         find(
  53:                 sub {
  54:                                                                                                                               
  55:                         return unless ($File::Find::name =~ /\.mp3$/); # Only mp3s
  56:                         $class->{FILELIST}{$_}{PATH} = $File::Find::name;
  57:                         $class->{FILELIST}{$_}{SIZE} = -s $File::Find::name;
  58:                 },
  59:                 $class->{MP3DIR}
  60:                                                                                                                               
  61:         );
  62:                                                                                                                               
  63:         $class->_dump if ($class->{DEBUG} == 1); # Just to check what you are getting
  64:                                                                                                                               
  65: }
  66:                                                                                                                               
  67: # Private method
  68: sub _dump {
  69:         my $class = shift;
  70:         print Dumper $class->{FILELIST}; # Print to STDOUT the contents of the hash
  71: }
  72: 
  73: # Build backup
  74: sub fullBackup {
  75:         my $class = shift;
  76:         my $size  = 0;
  77:         my $name  = 'Disc';
  78:         my $count = 1;
  79:         my $flag  = 1;
  80:         my $dirname = '';
  81:                                                                                                                               
  82:         my $list = $class->{FILELIST};
  83:         my $maxsize = $class->{MAXSIZE};
  84:                                                                                                                               
  85:                                                                                                                               
  86:         die("This is no directory named $class->{BACKUPDIR}\n") unless -d $class->{BACKUPDIR};  # Die if no directory
  87:                                                                                                                               
  88:         foreach ( sort keys %$list) {
  89:                                                                                                                               
  90:                 if ($flag) { # There has got to be a better way to do this that seting a flag
  91:                         $dirname = "$class->{BACKUPDIR}/$name$count"; # Create new directory name
  92:                         mkdir $dirname; # Create the directory
  93:                         print 'Creating new dir: '.$dirname."\n" if $class->{DEBUG}; # Be more verbose
  94:                         $flag = 0;
  95:                         open LIST, ">$dirname/index.txt" || die ("Cannot open file for writing: $!"); # Create index of Mp3s
  96:                 }
  97:                                                                                                                               
  98:                 copy ($class->{FILELIST}{$_}{PATH},$dirname); # Copy file to new directoy
  99:                 $size += $class->{FILELIST}{$_}{SIZE}; # Add total kilobyes in directory
 100:                 print LIST $_."\n";
 101:                                                                                                                               
 102:                 if ($size >= $maxsize) { # Set up for new directory
 103:                         $flag = 1;
 104:                         $count++;
 105:                         $size = 0;
 106:                 close LIST;
 107:                 }
 108:                                                                                                                               
 109:         }
 110: }
 111:                                                                                                                               
 112: 1; #this line is important and will help the module return a true value
 113: __END__
 114: 

Comment on Organize you MP3 backup
Download Code
Re: Organize you MP3 backup
by Aristotle (Chancellor) on Aug 02, 2003 at 21:16 UTC
      Well don't I feel stupid for re-inventing the wheel... :)
      I've never even heard of that module. I guess you learn
      something everyday...

      Thanks,
      rlb3
        Don't feel stupid, happens to everyone---just think of it as an opportunity to do an 'Update:' using the suggested module!

        --hsm

        "Never try to teach a pig to sing...it wastes your time and it annoys the pig."
Re: Organize you MP3 backup
by hieronymus (Scribe) on Aug 15, 2003 at 14:25 UTC
    not to cop-out on this one, but it just so happens that over at the perl journal there's an article Luis E Munoz entitled "Managing Your MP3 Library in Perl"...it may be of some use to you

    -hieronymus

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlcraft [id://280310]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2014-07-26 12:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (176 votes), past polls