Podcast Synch Batch File: Difference between revisions
From Mike Beane's Blog
No edit summary |
(→Perl Script: Updated script - now deletes playlist) |
||
Line 36: | Line 36: | ||
use Term::ANSIColor; | use Term::ANSIColor; | ||
use MP3::Info; | use MP3::Info; | ||
use MP3::Tag; | |||
use warnings; | |||
use strict; | |||
use Cwd; | use Cwd; | ||
Line 45: | Line 49: | ||
my $podcastsource="C:/Documents and Settings/Mike/My Documents/My Music/Podcasts"; | my $podcastsource="C:/Documents and Settings/Mike/My Documents/My Music/Podcasts"; | ||
#my $podcastsource="d:/music/Ipod/Podcasts"; | |||
my $LOGFILE_IN="$dir/PLAYLISTS/On The Go.plp"; | |||
my $podcastdest="$dir/MUSIC/Podcasts"; | my $podcastdest="$dir/MUSIC/Podcasts"; | ||
my $dircontents=(); | my $dircontents=(); | ||
Line 50: | Line 57: | ||
my $checkmp3tag=(); | my $checkmp3tag=(); | ||
my $checkmp3name=(); | my $checkmp3name=(); | ||
my @newfile=(); | |||
my $newfile=(); | |||
my @newfiles=(); | |||
my @files=(); | |||
my $file=(); | |||
my $directorytmp=(); | |||
my $directory1tmp=(); | |||
my $directory2make=(); | |||
my @directory2make=(); | |||
my $tag=(); | |||
print color 'bold blue on_white'; | print color 'bold blue on_white'; | ||
Line 55: | Line 73: | ||
print color 'reset'; | print color 'reset'; | ||
& | &movefiles($podcastsource); #Check what is in the source and not in the dest - copy to dest | ||
print color 'bold blue on_white'; | print color 'bold blue on_white'; | ||
Line 64: | Line 82: | ||
print color 'bold blue on_white'; | print color 'bold blue on_white'; | ||
print "Checking for files | print "Checking for files removed from source:\n"; | ||
print color 'reset'; | print color 'reset'; | ||
Line 88: | Line 106: | ||
} | } | ||
$mp3 = new MP3::Info $file; | |||
#printf "\n\nChecked ID3v1 tags for $file:\nTitle: %s \nArtist: %s \nGenre: %s \nAlbum: %s \n\n", | |||
$ | #$mp3->title, $mp3->artist, $mp3->genre, $mp3->album; | ||
my $title=$mp3->title; | |||
if ($mp3-> | my $artist=$mp3->artist; | ||
my $album=$mp3->album; | |||
if (exists $mp3->{ID3v2}) {$mp3->{ID3v2}->remove_tag;} | |||
if (exists $mp3->{ID3v1}) {$mp3->{ID3v1}->remove_tag;} | |||
if ($mp3-> | |||
$tag->{TITLE} = $title; | |||
$tag->{ARTIST} = $newtag; | |||
$tag->{ALBUM} = $newtag; | |||
$tag->{GENRE} = 'Podcast'; | |||
set_mp3tag($file, $tag) or warn "$!"; | |||
$mp3 = new MP3::Info $file; | |||
#printf "\n\nChecked ID3v1 tags for $file:\nTitle: %s \nArtist: %s \nGenre: %s \nAlbum: %s \n\n", | |||
#$mp3->title, $mp3->artist, $mp3->genre, $mp3->album; | |||
printf " | |||
$mp3->title, $mp3->artist, $mp3->genre; | |||
#ref: FILE, TITLE, ARTIST, ALBUM, YEAR, COMMENT, GENRE | #ref: FILE, TITLE, ARTIST, ALBUM, YEAR, COMMENT, GENRE | ||
} | } | ||
Line 120: | Line 134: | ||
sub OnTheGo | sub OnTheGo | ||
{ | { | ||
$LOGFILE_IN | if(-e "$LOGFILE_IN") | ||
{ | |||
open LOGFILE_IN, $LOGFILE_IN or warn "Cannot open $LOGFILE_IN for read :$!"; | |||
while (<LOGFILE_IN>) | |||
{ | { | ||
my $tmp=$_; | |||
$tmp =~ s/\0//g; | |||
$tmp =~ s/\015//g; | |||
$tmp =~ s/\012//g; | |||
$tmp =~ s/HARP, /$dir\//; | |||
$tmp =~ s/\\/\//g; | |||
#print "$tmp\n"; | |||
if ($tmp =~ /$dir/) | |||
{ | { | ||
if ($tmp =~ m/($podcastdest)\/(.+)/) | |||
{ | { | ||
$file="$podcastsource/$file"; | my $file=$2; | ||
if(-e "$podcastsource/$file") | |||
{ | |||
$file="$podcastsource/$file"; | |||
unlink ($file) or warn "Cannot del $file: $!"; | |||
print color 'bold yellow on_magenta'; | |||
print "$2\n"; | |||
print color 'reset'; | |||
} | |||
} | } | ||
} | } | ||
} | } | ||
&deleteplaylist; | |||
} | } | ||
} | } | ||
Line 176: | Line 194: | ||
} | } | ||
sub | sub movefiles | ||
{ | { | ||
my $dir = shift; | my $dir = shift; | ||
opendir DIR, $dir or return; | opendir DIR, $dir or return; | ||
my @contents = | my @contents = | ||
map "$dir/$_", | map "$dir/$_", | ||
#sort grep !/\.mp3$/ || /\.jpg$/, | |||
sort grep !/^\.\.?$/, | sort grep !/^\.\.?$/, | ||
readdir DIR; | readdir DIR; | ||
Line 189: | Line 209: | ||
{ | { | ||
next unless !-l && -d; | next unless !-l && -d; | ||
& | &movefiles($_); | ||
$dircontents=$_; | $dircontents=$_; | ||
&createcontents($dircontents); | &createcontents($dircontents); | ||
Line 199: | Line 219: | ||
{ | { | ||
opendir(DIR, "$dircontents") or die "Cannot open $dircontents to read: $!"; | opendir(DIR, "$dircontents") or die "Cannot open $dircontents to read: $!"; | ||
@newfiles = grep(/\.mp3$/ || /\.jpg$/ ,readdir(DIR)); | #@newfiles = grep(/\.mp3$/ || /\.jpg$/ ,readdir(DIR)); | ||
@newfiles = grep(/\.mp3$/ ,readdir(DIR)); | |||
closedir(DIR); | closedir(DIR); | ||
foreach $newfile (@newfiles) | foreach $newfile (@newfiles) | ||
Line 274: | Line 295: | ||
} | } | ||
} | } | ||
sub deleteplaylist | |||
{ | |||
if(-e "$LOGFILE_IN") | |||
{ | |||
if(-w $LOGFILE_IN) | |||
{ | |||
print "!!!!!!!!!!!!!\n"; | |||
close LOGFILE_IN or warn "Cannot CLOSE $LOGFILE_IN: $!";; | |||
unlink $LOGFILE_IN or warn "Cannot del $LOGFILE_IN: $!"; #remove the list | |||
} | |||
} | |||
} | |||
[[Category:Perl]] | [[Category:Perl]] | ||
[[Category:Podcasts]] | [[Category:Podcasts]] |
Revision as of 13:02, 22 January 2008
Not finding a good way to manage my podcasts on my Sansa e250 and I'm already manually dumping files from iTunes anyway (my choice).
For the meantime, here is synch.bat to del all files in the Podcasts directory on the Sansa and then xcopy everything in the iTunes Podcasts directory. It's not quick, but it works.
echo off cls del "g:\music\podcasts\*" /S /Q xcopy "D:\music\Ipod\Podcasts\*.mp3" /S "g:\music\Podcasts\"
Next step I plan to write something that checks:
- Cleanup the code
- Make a playlist (by directory or by group?)
Does file exist in both places - don't copyDoes file exist in dest and not in source - del destDoes file exist in source and not in dest - copy sourceImplement create directory if not exist routineRead the Sansa On The Go Playlist and delete files that are foundMake dest drive setting valueCheck ID3 tag and set if blank- see &johncdvorak
Autoplay Handler
Added an AutoPlay handler (directions here). Now when connected, synchronization is possible from the popup window.
Script Test
Perl Script
use File::Copy; use Win32::Console::ANSI; use Term::ANSIColor; use MP3::Info; use MP3::Tag; use warnings; use strict; use Cwd; my $dir = cwd; $dir =~ s/\///g; print color 'bold blue on_white'; print "Portable Player Drive is $dir\n"; print color 'reset'; my $podcastsource="C:/Documents and Settings/Mike/My Documents/My Music/Podcasts"; #my $podcastsource="d:/music/Ipod/Podcasts"; my $LOGFILE_IN="$dir/PLAYLISTS/On The Go.plp"; my $podcastdest="$dir/MUSIC/Podcasts"; my $dircontents=(); my $destdir=(); my $checkmp3tag=(); my $checkmp3name=(); my @newfile=(); my $newfile=(); my @newfiles=(); my @files=(); my $file=(); my $directorytmp=(); my $directory1tmp=(); my $directory2make=(); my @directory2make=(); my $tag=(); print color 'bold blue on_white'; print "Checking source files to move to destination:\n"; print color 'reset'; &movefiles($podcastsource); #Check what is in the source and not in the dest - copy to dest print color 'bold blue on_white'; print "Reading On The Go.plp for files to remove from portable:\n"; print color 'reset'; &OnTheGo; print color 'bold blue on_white'; print "Checking for files removed from source:\n"; print color 'reset'; &smash($podcastdest); #Check what is in the dest and not in the source - del from dest sub johncdvorak { #This routine is dedicated to John C Dvorak by way of CrankyGeeks my $file=$checkmp3tag; my $mp3 = new MP3::Info $file; my $newtag=(); if ($checkmp3name =~ m/(.*).mp3/) { $newtag=$1; #Strip out numbers. If we're lucky #we'll have a nice name for the tag w/o #show/ep numbers $newtag =~ s/[0-9]//g; } $mp3 = new MP3::Info $file; #printf "\n\nChecked ID3v1 tags for $file:\nTitle: %s \nArtist: %s \nGenre: %s \nAlbum: %s \n\n", #$mp3->title, $mp3->artist, $mp3->genre, $mp3->album; my $title=$mp3->title; my $artist=$mp3->artist; my $album=$mp3->album; if (exists $mp3->{ID3v2}) {$mp3->{ID3v2}->remove_tag;} if (exists $mp3->{ID3v1}) {$mp3->{ID3v1}->remove_tag;} $tag->{TITLE} = $title; $tag->{ARTIST} = $newtag; $tag->{ALBUM} = $newtag; $tag->{GENRE} = 'Podcast'; set_mp3tag($file, $tag) or warn "$!"; $mp3 = new MP3::Info $file; #printf "\n\nChecked ID3v1 tags for $file:\nTitle: %s \nArtist: %s \nGenre: %s \nAlbum: %s \n\n", #$mp3->title, $mp3->artist, $mp3->genre, $mp3->album; #ref: FILE, TITLE, ARTIST, ALBUM, YEAR, COMMENT, GENRE } sub OnTheGo { if(-e "$LOGFILE_IN") { open LOGFILE_IN, $LOGFILE_IN or warn "Cannot open $LOGFILE_IN for read :$!"; while (<LOGFILE_IN>) { my $tmp=$_; $tmp =~ s/\0//g; $tmp =~ s/\015//g; $tmp =~ s/\012//g; $tmp =~ s/HARP, /$dir\//; $tmp =~ s/\\/\//g; #print "$tmp\n"; if ($tmp =~ /$dir/) { if ($tmp =~ m/($podcastdest)\/(.+)/) { my $file=$2; if(-e "$podcastsource/$file") { $file="$podcastsource/$file"; unlink ($file) or warn "Cannot del $file: $!"; print color 'bold yellow on_magenta'; print "$2\n"; print color 'reset'; } } } } &deleteplaylist; } } sub makedest { my $destdirectory=$destdir; @directory2make = split(/\//, $destdirectory); shift (@directory2make); #drop the first $directorytmp=""; $directory1tmp=""; foreach $directory2make (@directory2make) { $directory1tmp="$directory1tmp\/$directory2make"; #print "$directory1tmp\n"; $directorytmp="$dir$directory1tmp"; #print "$directory1tmp\n"; #print "$directorytmp\n"; if(!-e $directorytmp) { print color 'bold yellow on_magenta'; print "Did not find $directorytmp\n"; mkdir ("$directorytmp") or die "Cannot make $directorytmp :$!"; #incoming logs go here print "Created $directorytmp\n"; print color 'reset'; } } # end for each } sub movefiles { my $dir = shift; opendir DIR, $dir or return; my @contents = map "$dir/$_", #sort grep !/\.mp3$/ || /\.jpg$/, sort grep !/^\.\.?$/, readdir DIR; closedir DIR; foreach (@contents) { next unless !-l && -d; &movefiles($_); $dircontents=$_; &createcontents($dircontents); rmdir $_; } } sub createcontents { opendir(DIR, "$dircontents") or die "Cannot open $dircontents to read: $!"; #@newfiles = grep(/\.mp3$/ || /\.jpg$/ ,readdir(DIR)); @newfiles = grep(/\.mp3$/ ,readdir(DIR)); closedir(DIR); foreach $newfile (@newfiles) { if ($dircontents =~ m/($podcastsource)\/(.+)/) { $destdir="$podcastdest/$2"; my $testdir="$destdir/$newfile"; if(!-e $testdir) { if(!-e $destdir) #check to see if dest exists, if not, create the dir { &makedest($destdir); } my $filetobecopied = "$podcastsource/$2/$newfile"; my $oldfile = "$podcastdest/$2/$newfile"; print color 'bold yellow on_magenta'; print "Copying $newfile\n"; print color 'reset'; copy($filetobecopied, $oldfile) or die "File cannot be copied."; if ($newfile =~ "mp3") { $checkmp3tag=$oldfile; $checkmp3name=$newfile; &johncdvorak($checkmp3tag,$checkmp3name); } } } } } sub smash { my $dir = shift; opendir DIR, $dir or return; my @contents = map "$dir/$_", sort grep !/^\.\.?$/, readdir DIR; closedir DIR; foreach (@contents) { next unless !-l && -d; &smash($_); $dircontents=$_; &destroycontents($dircontents); rmdir $_; } } sub destroycontents { opendir(DIR, "$dircontents") or die "Cannot open $dircontents to read: $!"; @files = grep(/\.mp3$/,readdir(DIR)); closedir(DIR); foreach $file (@files) { #print "$file\n"; if ($dircontents =~ m/($podcastdest)\/(.+)/) { if(!-e "$podcastsource/$2/$file") { my $del="$podcastdest/$2/$file"; unlink ($del); print color 'bold yellow on_magenta'; print "$del\n"; print color 'reset'; } } } } sub deleteplaylist { if(-e "$LOGFILE_IN") { if(-w $LOGFILE_IN) { print "!!!!!!!!!!!!!\n"; close LOGFILE_IN or warn "Cannot CLOSE $LOGFILE_IN: $!";; unlink $LOGFILE_IN or warn "Cannot del $LOGFILE_IN: $!"; #remove the list } } }