Rockbox.org home
release
dev builds
extras
themes manual
wiki
device status forums
mailing lists
IRC bugs
patches
dev guide
translations



Rockbox mail archive

Subject: Updated sync_podcasts script

Updated sync_podcasts script

From: Matthew Caron <matt_at_mattcaron.net>
Date: Fri, 18 Aug 2006 22:13:20 -0400

Now autogenerates playlists called "00_playlist.m3u" in each podcast
directory based on what is currently in that directory.
-- 
--------------------------------------------------------------------
PGP Key: http://www.mattcaron.net/pgp_key.txt
 ~~ Matt Caron ~~


#!/usr/bin/perl -w

####################################################
#
# sync_podcast - synchronize podcasts to USB Mass Storage Device type
# players
# Copyright (C) 2006 Matthew Caron <matt_at_mattcaron.net>
#
# 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.
#
# Full license at: http://www.fsf.org/licensing/licenses/info/GPLv2.html
#
####################################################

use strict;
use File::Find;
use File::Path;
use File::Copy;

## IMPORTANT - if your user name just happens to be "matt", make sure
## to comment out the section NOT FOR NORMAL PEOPLE, below

## Constants
# Directory from which podcasts are to be copied
my $source_dir = "/pub/matt/podcasts";

# Log file containing list of copied podcasts
my $podcast_log_file = "sync_podcasts.log";

# target directory to which podcasts are to be copied
my $target_dir = "/media/ipod/podcasts";

# portion of above representing the device's location on your machine.
# this is removed from playlists so that the relative path is correct
my $playlist_base_remove = "/media/ipod";

# name of the automagically generated playlist file
my $auto_playlist_file = "00_playlist.m3u";

## End Constants

## Derived constants
my $podcast_log = $source_dir . "/" . $podcast_log_file;

## Plain old variables
my $initialize = 0;
my %uploaded_files;

# Read in logfile and store file path and name in hash.
if(-e $podcast_log)
{
    open(LOGFILE, $podcast_log);

    while(<LOGFILE>)
    {
        chomp($_);
        $uploaded_files{$_} = 1;
    }

    close(LOGFILE);
}
else
{
    # logfile doesn't exist - assume initialization mode
    $initialize = 1;

    print "Initializing logile...\n";
}

# START NOT FOR NORMAL PEOPLE
# Call sub script to fix stupid LugRadio podcast nomenclature, because
# I always forget
if(getlogin() eq "matt")
{
    print "Fixing LugRadio names...\n";
    system("\"/pub/matt/podcasts/fix_lugradio_names\"");
}
# END NOT FOR NORMAL PEOPLE

# Recurse through directory and find each file there
find(\&copy_stuff, ($source_dir));

# Recurse through and build playlists for each directory
find(\&build_playlists, ($target_dir));

sub copy_stuff
{
    # file name
    my $filename = $_;

    # source directory
    my $dirname = $File::Find::dir;
    # source dir + file name
    my $fullpath = $File::Find::name;

    # If what we got isn't a directory, do something
    # And it's not the log file
    if(!-d $fullpath && $filename ne $podcast_log_file)
    {
        # get name of dir in isolation
        my $source_dirname = $dirname;
        $source_dirname =~ s/$source_dir//;
        
        # target dir
        my $destination_path = $target_dir . "/" . $source_dirname;
        # target dir + file name
        my $destination_path_file = $destination_path . "/" . $filename;
        
        # make sure this hasn't been uploaded...
        if(!defined $uploaded_files{$fullpath})
        {
            if(!$initialize)
            {

                # Put file on the music device, maintaining directory
                # structure..
                # make directory if it doesn't exist
                if(!-d $destination_path)
                {
                    print "\n\nMaking directory $destination_path...\n\n";
                    
                    mkpath($destination_path) ||
                        die "Cannot mkpath $destination_path: $!";
                }
                
                print "$fullpath => $destination_path_file\n";
                
                # copy file to that directory
                copy($fullpath, $destination_path_file) ||
                    die "Cannot copy $fullpath to $destination_path_file: $!";
                
            }
            
            # Log that you did it
            open(LOGFILE, ">>$podcast_log");

            print LOGFILE $fullpath . "\n";
            
            close(LOGFILE);

        }
    } # else, ignore directory
}

sub build_playlists
{
    my $fullpath = $File::Find::name;

    # only operate on directories and they must be below the main one
    if(-d $fullpath && $fullpath ne $target_dir)
    {
        print "Building playlist for $fullpath\n";

        my $target_playlist_file = $fullpath . "/" . $auto_playlist_file;

        # shell out call to create playlist
        # cd (dir) - \ls "`pwd`"/* > 00_playlist.m3u
        open (LS, "/bin/ls \"$fullpath\"/*|") ||
            die "Can't get directory listing for $fullpath: $!\n";
        open (PLAYLIST, ">$fullpath/$auto_playlist_file") ||
            die "Can't open $fullpath/$auto_playlist_file: $!\n";

        while(<LS>)
        {
            my $path = $_;

            # make sure it's not us
            if($path !~ /$auto_playlist_file/)
            {
                chomp($path);
                $path =~ s/^$playlist_base_remove//;
                print PLAYLIST $path . "\n";
            }
        }
        close LS;
        close PLAYLIST;
    }
}
Received on 2006-08-19

Page template was last modified "Tue Sep 7 00:00:02 2021" The Rockbox Crew -- Privacy Policy