Re: [CBLX] daisyreader.pl

[ Thread Index | Date Index | More lists.tuxfamily.org/carrefourblinux Archives ]


Hello Yannick, 

quel plaisir de te lire!

On Fri, Nov 15, 2013 at 05:49:57PM +0100, yan@xxxxxxxxxxxx wrote:
> Hello a tous,
> J'ai vu passer un message au sujet de mon vieux, tres vieux, daisyreader.pl.
> Je veux bien le remettre au gout du jour, le seul soucis etant que :
> * Je n'ai plus le scripte

Je l'avais mis en pj dans mon 1er mail mais vais le remettre attaché à ce 
présent courriel! Cf. pj:
la seule modife conf faite est qu'au lieu de pointer vers /mnt/cdrom j'ai
mis /media/cdrom0/ 
Mais le script a fonctionné entre mars dernier et jusqu'à ce que Dominique
Lambert signale que ça ne fonctionnait plus.

> * je n'ai plus de livre Daisy.

Je peux faire un iso ou tgz de la revue dont il s'agit et t'envoeyr l'url
tmp où je mettrai le tgz ou iso.

> * Et enfin, je n'ai plus le binaire servant a la lecture de MP3/fichier 
> audio avec possibilite de specifier un debut et une fin.

J'ignore ce que le script utilise, mais ça devrait être madplay si je ne me
trompes. 

> Si quelqu'un peut me renvoyer tout ca, ce sera avec plaisir :)
> Bonne fin de journee a tous,

OK, on fais ça: le script en pj et l'url pour le tgz d'une revue dès que
possible demain.

Merci en tout cas de le remettre sur pied; il est tout petit et tout simple
mais efficace, j'adore ça! :-)

Amitié,
Aldo. 
#!/usr/bin/perl -w
## daisyreader.pl for DaisyReader in /home/yan/Daisy/
##
## Made by Yannick PLASSIARD
## Login   <yan@xxxxxxxxxxxx>
##
## Started on  Wed Jun 30 10:08:47 2004 Yannick PLASSIARD
## Last update Sat Jul 24 00:52:38 2004 Yannick PLASSIARD

##
## send comments to me. ;-)
##

use	strict;

my      $debug = 1; # 1 if you want lots of output
my      $version = '0.3';
my	$mode; # refers to the current viewing mode
my	$choice = undef;
my	%cells;
my	%chapters;
my	%pages;
my      %config;
my	$view;
my	$continue_reading;
my      $configdir = "$ENV{'HOME'}/.daisyreader/";
my      $configfile = 'config';
my      $daisydir;
my	%book;

$| = 1;

##### SUBS #####

sub	getline
{
    my	$default = shift;
    my	$res = <>;

    $res = $default if ($res =~ /^$/ && defined($default));
    chomp($res);
    return $res;
}


## load_config : Loads program's configuration 

sub    load_config
{
    my $line;

    unless (-d $configdir)
    {
	print "No directory .daisyreader in your home, creating one...\n";
	if (mkdir($configdir) == 0)
	{
	    print "\aError creating directory: $!\n";
	    return 0;
	}
	print "Creating config file ...\n";
	open(FD, ">$configdir/$configfile") 
	  or die("Cannot create config file: $!\n");
	print FD <<EOF
# DaisyReader Configuration file
VIEW=chapter
#LAST_PATH=/mnt/cdrom
LAST_PATH=/media/cdrom0
CONTINUE_PLAYING=no
EOF
	  ;
	close(FD);
    }
    $line = 'z';
    open(FD, "$configdir/$configfile") or die("Cannot open $configdir/$configfile for reading: $!\n");
    while (eof(FD) == 0)
    {
	$line = <FD>;
	next if ($line =~ /^\#.*$/);
	if ($line =~ /LAST\_PATH=(.*)/i)
	{
	    $config{'path'} = $1;
	}
	if ($line =~ /VIEW=(chapter|page|cell)/i)
	{
	    $config{'view'} = $1;
	}
	if ($line =~ /^CONTINUE_PLAYING=(yes|no)/i)
	{
	    $config{'contplay'} = $1;
	}
    }
    if (defined($debug))
    {
	print "Configuration summary:\n";
	print "$_=$config{$_}\n" foreach (sort keys %config);
    }
}


## parse_toc': Parses HTML and XML files to build cells' index.

sub	parse_toc
{
    my	$line = 'Line';;

    my	$id = 1;
    my	$id_page = 1;
    my	$id_chapter = 1;

    unless (open(NCC, "$daisydir/ncc.html")
	or open(NCC, "$daisydir/NCC.html"))
    {
	print "Error opening the NCC file: $!\n";
	print "Please ensure that this is a Daisy-book format.\n";
	exit(1);
    }
    while (defined($line))
    {
	$line = <NCC>;
	next if (!defined($line));
	if ($line =~ /\<title\>(.+)\<\/title\>/i)
	{
	    $book{'title'} = $1;
	    $book{'title'} = "Unspecified" if ($1 =~ /^\s*$/);
	}
	if ($line =~ /\<meta\s+name\=\"NCC:Format\"\s+content\=\"(.*)\".*\>/i)
	{
	    $book{'format'} = $1;
	    $book{'format'} = "Unspecified" if ($1 =~ /^\s*$/);
	}
	if ($line =~ /\<meta\s+name\=\"NCC:Publisher\"\s+content\=\"(.*)\".*\>/i)
	{
	    $book{'publisher'} = $1;
	    $book{'publisher'} = "Unspecified" if ($book{'publisher'} =~ /^\s*$/);
	}
	if ($line =~ /\<meta\s+name\=\"NCC:Identifier\"\s+content\=\"(.*)\".*\>/i)
	{
	    $book{'identifier'} = $1;
	    $book{'identifier'} = "Unspecified" if ($book{'identifier'} =~ /^\s*$/);
	}
	if ($line =~ /\<meta\s+name\=\"ncc:totaltime\"\s+content\=\"(.*)\".*\>/i)
	{
	    $book{'time'} = $1;
	    $book{'time'} = "Not Available" if ($1 =~ /^\s*$/);
	}
	if ($line =~ /\<meta\s+name=\"dc:date\"\s+content=\"([^\"]*).*\>/i)
	  {
	      $book{'date'} = $1;
	      $book{'date'} = "Not available" if ($1 =~ /^\s*$/);
	  }
	if ($line =~ /\<meta\s+name=\"dc:creator\"\s+content=\"(.*)\".*\>/i)
	  {
	      $book{'author'} = $1;
	      $book{'author'} = "Unspecified" if ($1 =~ /^\s*$/);
	  }
	if ($line =~ /\<[aA]\s[hH][rR][eE][fF]\=\"(.*)\.smil#(.*)\"\>(.*)\<\/[aA]\>/i)
	{
	    $cells{$id}{'title'} = $3;
	    $cells{$id}{'smil'} = "$1.smil";
	    $cells{$id}{'part'} = $2;

	    # We also put it in the page / chapter index depending on the title

	    if ($cells{$id}{'title'} =~ /^\d+$/)
	    { # this is a page
		$pages{$id_page}{'title'} = $cells{$id}{'title'};
		$pages{$id_page}{'smil'} = $cells{$id}{'smil'};
		$pages{$id_page}{'part'} = $cells{$id}{'part'};
		$id_page++;
	    }
	    else
	    { # this is a chapter
		$chapters{$id_chapter}{'title'} = $cells{$id}{'title'};
		$chapters{$id_chapter}{'smil'} = $cells{$id}{'smil'};
		$chapters{$id_chapter}{'part'} = $cells{$id}{'part'};
		$id_chapter++;
	    }
	    $id++;
	}
    }
    close(NCC);
}

## select_chapter: presents a choice of cells and asks the user for one.

sub	select_chapter()
{
    my $end = 0;
    my $start = 0;

    $mode = \%pages if ($config{'view'} =~  /page/i);
    $mode = \%chapters if ($config{'view'} =~ /chapter/i);
    $mode = \%cells if ($config{'view'} =~ /page/i);

    $continue_reading = 0;
    $choice = undef;
    while (!defined($choice) || $choice !~ /^[cChHpPqQ]$|^\d+$|^$/)
      {
	  for (my $id = 1; defined(${$mode}{$id}) && $choice =~ /^$/; )
	  {
	      my	$i;
	      
	      system("clear");
	      print "Page list:\n" if($view =~ /pages/i);
	      print "Chapter list:\n" if ($view =~ /chapter/i);
	      print "Cell list:\n" if ($view =~ /cell/i);
	      for ($i = 0; $i < 23 && defined(${$mode}{$id}); $i++)
	      {
		  print "$id: ${$mode}{$id++}{'title'}\n";
	      }
	      print "Your choice (Enter=NextPage, C=Cells, H=cHapters, P=Pages, Q=Quit) ? ";
	      $choice = getline();
	  }
      }
    $view = "page" if ($choice =~ /^p$/i);
    $view = "chapter" if ($choice =~ /^h$/i);
    $view = "cell" if ($choice =~ /^c$/i);
    if ($choice =~ /^[qQ]$/)
    {
	print "Really quit DaisyReader [yN]? ";
	$choice = getline('n');
	if ($choice =~ /^y/i)
	  {
	      print "Thank you for using DaisyReader!\n";
	      exit 0;
	  }
	return ;
    }
    if ($choice =~ /^\d+$/)
    {
	my	$quit = 0;

	for (my $id = 1; $quit != 1; $id++)
	{
	    $quit = 1 if (!defined($cells{$id}));
	    print "${$mode}{$choice}{'title'} -> $cells{$id}{'title'}\n" if (defined($debug));
	    if ($cells{$id}{'title'} eq ${$mode}{$choice}{'title'})
            {
		$quit = 1;
		$start = $id;
	    }
	}
	for (my $id = $start + 1, $quit = 0; $quit != 1; $id++)
	{
	    $quit = 1 if (!defined($cells{$id}));
	    if ($cells{$id}{'title'} eq ${$mode}{$choice + 1}{'title'})
	    {
		print "Setting the end to $cells{$id}{'title'} -> ${$mode}{$choice + 1}{'title'}\n" if (defined($debug));
		$end = $id;
		$quit = 1;
	    }
	}
	return if (&read_chapter($start, $end));
	if ($continue_reading == 1)
	{
	    $start++;
	    while (defined($cells{$start}))
	    {
		return if (&read_chapter($start) == 1);
		$start++;
	    }
	}
	print "Press enter to go back to the list...";
	<>;
    }
}


## read_chapter: attempts to read a specified chapter.

sub	read_chapter($$)
{
    my	$id = shift;
    my  $end = shift;
    my	$line = 'Line';
    my	$choice = undef;

    if (!defined($cells{$id}))
    {
	print "\aERROR:  invalid chapter ID.\n";
	print "Press enter to continue...";
	<>;
	return;
    }
    while (!defined($choice) && $continue_reading == 0)
    {
	print "Only read this cell/page/chapter [default yes] ? ";
	$choice = getline('yes');
	$continue_reading = 0 if ($choice =~ /^[Yy]/);
	$continue_reading = 1 if ($choice =~ /^[Nn]/);
	$choice = undef if ($choice !~ /^[YnyN]/);
    }
    print "Reading chapter $cells{$id}{'title'} ...\n";
    $end = $id + 1 if (!defined($end) || $end == 0);
    print "Playing from $id to $end.\n";
    for (my $i = $id; $i < $end; $i++)
    {
	# print "$i\n";
	my       $next_cell = 0;
	my	$smilfile = $daisydir . '/' . $cells{$i}{'smil'};
	unless(open(SMIL, $smilfile))
	{
	    print "\aCannot open chapter-specific file $daisydir/$cells{$i}{'smil'}...\n";
	    print "Press enter to continue...";
	    <>;
	    return ;
	}
	$line = 'Line';
	while (defined($line))
	{
	    $line = <SMIL>;
	    next if (!defined($line));
	    if ($line =~ /.*id=\"(.*)\"/)
	    {
		# print "Found ID $1 in $smilfile\n";
		if ($1 eq $cells{$i}{'part'})
		{
		    # print "Found ID $1\n";
		    $line = <SMIL> while ($line !~ /.*\<audio.*/);
		    if (!defined($line) && $continue_reading)
		    {
			print "\aERROR: Audio tag not found matching ID $cells{$i}{'part'}\n";
			print "Press enter to continue...";
			close(SMIL);
			<>;
			return;
		    }
		    while ($line =~ /\<audio\s+src=\"(.*)\"\s+clip-begin=\"npt=(.*)\"\s+clip-end=\"npt=([^\"]*)\".*\/\>/)
		    {
			my	$start = $2;
			my	$file = $1;
			my	$end = $3;

			$start =~ s/s$//g;
			$end =~ s/s$//g;
			my	$duration = sprintf "%.3f", $end - $start;
			print "Playing $file from $start to $end ($duration) ...\n";
			$next_cell = 1;
			system("madplay -v --start=$start -t $duration $daisydir/$file") == 0 or return 1;
			$line = <SMIL>;
		    }
		}
	    }
	}
	close(SMIL);
	if ($next_cell == 0)
	{
	    print "Chapter's ID $cells{$id}{'title'} was not found in any SMIL file.\n";
	    print "Press enter to continue...";
	    <>;
	}
	$next_cell = 0;
    }
}



## MAIN PART

print "DaisyReader.pl version $version starting.\n";
print "Copyright (C) 2004 Yannick PLASSIARD <yan\@mistigri.org>.\n";
print "This program is FREE SOFTWARE. Please see the COPYING file included with this\n";
print "program to know what FREE SOFTWARE means.\n";

print "Loading configuration file ...\n";
&load_config(); 
$view = $config{'view'};
$continue_reading = $config{'contplay'};
print "\nPlease enter the path to your book [$config{'path'}] ? ";
$choice = &getline($config{'path'});

# Just check if the directory is valid.
opendir(DIR, $choice) || die"Cannot open Daisy Directory $choice: $!\nExiting.\n";
closedir(DIR);

# Scanning the table of contents opening the ncc.html file.

print "Scanning table of contents...\n";
$daisydir = $choice;

&parse_toc();
print "Book's informations\n----------------------\n";
foreach my $k (keys(%book))
{
    print "$k : $book{$k}\n";
}
print "Pess enter to continue ...";
<>;

while (1)
{
    &select_chapter();
}


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/