Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Readers > Sony Reader > Sony Reader Dev Corner

Notices

Reply
 
Thread Tools Search this Thread
Old 11-04-2007, 05:15 PM   #1
chas
Junior Member
chas began at the beginning.
 
Posts: 3
Karma: 10
Join Date: Nov 2007
Device: Sony PRS-505
Perl Script to convert to .lrf for Sony PRS

Here is a small perl script I wrote that goes recursively through sub-directories looking for .lit, .pdf, .txt, .html. .htm, .txt, .rtf files and then converts them to the same name into the specified directory.

It actually simply calls "any2lrf" to do the actual work so you will need to install libprs500 and clit18 as well.

Hope this helps some of you out.

Usage is:

convlrf.pl [-r] <sourcedir> <destdir>

eg. To convert the files in the current directory and put them into
/data/eReader simply: convlrf.pl -r . /data/eReader

It prefers .lit files rather than any other format so if you have multiple formats of the same book the .lit will be the one you get.

Enjoy.

#### Cut below this line and save to convlrf.pl ######

Code:
#!/usr/bin/perl
#
# Recursively go through directory structure from current directory looking
# for books and then convert them to .lrf and place in
# a single directory (won't replace filenames with ones that
# already exist) so you can upload them to your Sony eReader.
#
# Basically it just calls lit2lrf
# Don't panic if it takes a while converting in the BBeB stage takes TIME!!!
#

use File::Basename;

sub
usage
{
	print "convlrf.pl [-r] <litdir> <lrfdir>\n";
	print "\t[-r]: recurse through subdirs for .lit files.\n";
	print "\tlitdir: directory to start from.\n";
	print "\tlrfdir: destination directory.\n";
	exit 1;
}

#
# Return yes if first arg matches one of the rest in the list
#
sub
suffmatch
{
	my ($s, $suff) = @_;

	my $ret = 0;
	$suff =~ tr/a-z/A-Z/;
	$s =~ tr/a-z/A-Z/;
	
	my (@suffix) = split /:/, $s;
	foreach my $sfx (@suffix)
	{
		next if ("$suff" eq "");
		if ("$sfx" eq "$suff")
		{
#			print "Match:$suff=$sfx\n";
			$ret = 1;
			break;
		}
	}
#	print "No Match:$suff|$s|\n" if (! $ret);
	$ret;
}

#
# MAIN
#

if (
	($#ARGV != 1 && $#ARGV != 2)
	||
	($#ARGV == 2 && $ARGV[0] ne "-r")
	||
	($#ARGV == 1 && $ARGV[0] eq "-r")
)
{
	usage();
}

if ($ARGV[0] eq "-r")
{
	$litdir = $ARGV[1];
	$lrfdir = $ARGV[2];
	if (! -d $litdir || ! -d $lrfdir)
	{
		print "No litdir directory: $litdir\n" if (! -d $litdir);
		print "No lrfdir directory: $lrfdir\n" if (! -d $lrfdir);
		usage();
	}
	$FILES = "/usr/bin/find $litdir -type f -print";
}
else
{
	$litdir = $ARGV[0];
	$lrfdir = $ARGV[1];
	if (! -d $litdir || ! -d $lrfdir)
	{
		print "No litdir directory: $litdir\n" if (! -d $litdir);
		print "No lrfdir directory: $lrfdir\n" if (! -d $lrfdir);
		usage();
	}
	$FILES = "/usr/bin/ls -1 $litdir";
}

open F, "$FILES |" || die "Cant open $FILES\n";

my @suffix = ('.lit', '.txt', '.htm', '.html', '.rtf', '.pdf', '.rar', '.zip');
my $sufflist = "";
foreach my $i (@suffix)
{
	$sufflist .= ":$i";
}
while (<F>)
{
	chop;
	$infile = $_;
	# Valid suffix's we can convert to .lrf's
	my ($name, $path, $suff) = File::Basename::fileparse($infile, @suffix);
#	print "Name=$name\nPath=$path\nSuffix=$suff\n";

	# Get rid of things in file name unneccessary so we dont get dupes
	# of the same book
	$name =~ s/\s*\(lit\)//;
	$name =~ s/\s*\(txt\)//;
	$name =~ s/\s*\(htm\)//;
	$name =~ s/\s*\(html\)//;
	$name =~ s/\s*\(rtf\)//;
	$name =~ s/\s*\(pdf\)//;
	$name =~ s/\s*\(rar\)//;
	$name =~ s/\s*\(LIT\)//;
	$name =~ s/\s*\(TXT\)//;
	$name =~ s/\s*\(HTM\)//;
	$name =~ s/\s*\(HTML\)//;
	$name =~ s/\s*\(RTF\)//;
	$name =~ s/\s*\(PDF\)//;
	$name =~ s/\s*\(RAR\)//;

	#
	# If we already have a lrf and we now have a .lit then do it in pref
	# don't bother otherwise lets jsut keep what we have already done
	#

	next if (-e "$lrfdir/$name.lrf" && ! suffmatch(".lit", "$suff"));
	if (suffmatch("$sufflist", "$suff"))
	{
		print "Doing:$name\n";
		system 'any2lrf', '--ignore-tables', '-p', 'prs500', '-o', "$lrfdir/$name.lrf", "$infile";
	}
}
close(F);
exit 0;
chas is offline   Reply With Quote
Old 11-04-2007, 10:29 PM   #2
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,843
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
Is it platform independent?
kovidgoyal is offline   Reply With Quote
Advert
Old 11-06-2007, 04:24 PM   #3
chas
Junior Member
chas began at the beginning.
 
Posts: 3
Karma: 10
Join Date: Nov 2007
Device: Sony PRS-505
Quote:
Originally Posted by kovidgoyal View Post
Is it platform independent?
As is. No, it is OK for linux, bsd & Mac OSX. With a bit of fiddling the "find" needs to be replaced by native perl calls to recurse through the directory. Not much effort, I'll play around a bit ad get it "Widowz ready"
chas is offline   Reply With Quote
Old 11-06-2007, 04:28 PM   #4
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,843
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
Cool
kovidgoyal is offline   Reply With Quote
Old 11-14-2007, 02:21 PM   #5
Miwa
Junior Member
Miwa began at the beginning.
 
Posts: 1
Karma: 10
Join Date: Nov 2007
Device: Sony PRS-505
Can you just replace find with dir /s /b on Windows?
Miwa is offline   Reply With Quote
Advert
Old 11-16-2007, 07:35 AM   #6
chas
Junior Member
chas began at the beginning.
 
Posts: 3
Karma: 10
Join Date: Nov 2007
Device: Sony PRS-505
Quote:
Originally Posted by Miwa View Post
Can you just replace find with dir /s /b on Windows?
You could, however hang on a few days as I will be releasing a new version. I have noticed that any2lrf hangs on some input files so I am putting some intelligence in the perl script to detect that and skip to the next file. With that I will also use the native perl library call for recursing through directories that makes the app universal.
chas is offline   Reply With Quote
Old 11-16-2007, 12:06 PM   #7
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,843
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
Send me a file that any2lrf hangs on.
kovidgoyal is offline   Reply With Quote
Old 11-22-2007, 05:22 PM   #8
stustaff
Wizard
stustaff knows what time it isstustaff knows what time it isstustaff knows what time it isstustaff knows what time it isstustaff knows what time it isstustaff knows what time it isstustaff knows what time it isstustaff knows what time it isstustaff knows what time it isstustaff knows what time it isstustaff knows what time it is
 
Posts: 1,055
Karma: 2110
Join Date: Nov 2007
Location: Derbyshire UK
Device: sony reader PRS505 and 600
This sounds interesting, some step by step instructions for someone who doesnt know his perl from his pearly whites would be great
stustaff is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Original Lit2sb perl script RESURRECTED! nrapallo IMP 6 07-09-2010 10:17 AM
How to convert .cbr to .epub/.lrf for PRS-600 with best rerult DaddyDough Calibre 0 12-18-2009 11:22 AM
Convert Sony BeBB (LRF) to Palm ebookfab Sony Reader Dev Corner 5 08-21-2009 03:33 PM
Convert from Sony format to LRF FligMupple Sony Reader 3 10-06-2008 03:20 PM
Handy Perl Script to convert HTML0 files to smartquotes maggotb0y Sony Reader 0 04-12-2007 11:49 AM


All times are GMT -4. The time now is 04:47 PM.


MobileRead.com is a privately owned, operated and funded community.