Jump to content

mward

Members
  • Posts

    1
  • Joined

  • Last visited

Posts posted by mward

  1. Here's a perl script which sits in the background waiting for new mp3 files to appear in the various plugtemp places. When it finds one, it waits for it to stop growing and then copies it to $HOME/pandora with a name based on the date and time. Now all you need to do is work out the artist and title :D

    #!/usr/local/bin/perl
    
    # Look for mp3 files in $HOME/tmp/plugtmp*
    
    #
    
    
    
    use strict;
    
    use warnings;
    
    use File::Copy;
    
    
    
    $SIG{CHLD} = 'IGNORE'; # Automatically reap terminated child processes
    
    
    
    my $HOME = $ENV{'HOME'} || $ENV{'LOGDIR'} ||
    
                    (getpwuid($<))[7] || die "You're homeless!n";
    
    
    
    my $D = 0; # debug
    
    
    
    my $dest = "$HOME/pandora";
    
    my %seen = ();
    
    
    
    my @dirs = ("$HOME/tmp", "/tmp");
    
    my $i = 0;
    
    
    
    my $buf;
    
    
    
    while (1) {
    
    
    
      my $base = $dirs[$i];
    
      $i++;
    
      $i = 0 if ($i == @dirs);
    
      chdir($base);
    
      opendir(DIR, ".") or die;
    
      foreach my $dir (grep  { /^plugtmp/ } readdir(DIR)) {
    
        print "Scanning: $base/$dirn" if $D;
    
        my @files = ();
    
        if (-f $dir) {
    
          @files = ($dir);
    
        } else {
    
          opendir(SUB, $dir) or next;
    
          @files = map { "$dir/$_" } grep { !/^..?$/ } readdir(SUB);
    
          closedir(SUB);
    
        }
    
        foreach my $file (@files) {
    
          next if $seen{$file}++;
    
          PrivoxyWindowOpen(IN, $file) or next;
    
          read(IN, $buf, 2);
    
          close(IN);
    
          next unless $buf eq "xFFxFB";
    
          print "New file: $file n" if $D;
    
          unless (fork) {
    
        # Child waits for file to stop growing and then copies it
    
        my $old = 0;
    
        while (1) {
    
          my $size = -s $file;
    
          print "$file size = $sizen" if $D;
    
          if ($size > $old) {
    
            $old = $size;
    
            sleep(60);
    
          } else {
    
            if ($size > 1000000) {
    
              # Grab this file
    
              my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
    
                       = localtime(time);
    
              $year += 1900;
    
              $mon++;
    
              my $copy = sprintf("%04i%02i%02i.%02i.%02i.%02i.mp3",
    
                     $year, $mon, $mday, $hour, $min, $sec);
    
              print "copying $file to $dest/$copyn" if $D;
    
                  copy($file, "$dest/$copy");
    
            }
    
            exit(0);
    
          }
    
        }
    
          }
    
        }
    
      }
    
    
    
      sleep(10);
    
    
    
    }

    Cooper: Made it a code block for improved readability (indentation).

×
×
  • Create New...