Xu Xiaodong avatar Xu Xiaodong committed f1d082d

add epub_rename

Comments (0)

Files changed (2)

+#!/usr/bin/env perl
+#
+# name:      epub_rename, rename epub ebook
+# author:    Xu Xiaodong <xxdlhy@gmail.com>
+# license:   GPL
+# created:   2011 Nov 17
+# modified:  2011 Nov 17
+#
+
+use strict;
+use warnings;
+
+use Archive::Zip;
+use Archive::Zip::MemberRead;
+
+use feature qw(say);
+
+sub get_title {
+  my ($epub) = @_;
+
+  my $zip = Archive::Zip->new($epub);
+  my $opf = Archive::Zip::MemberRead->new( $zip, "OEBPS/content.opf" );
+
+  my $title;
+  while ( defined( my $line = $opf->getline() ) ) {
+    $title = $1 if $line =~ /<dc:title.*?>(.*)<\/dc:title>/;
+  }
+
+  $title =~ s/&amp;/and/g;
+
+  return $title;
+}
+
+sub rename_epub {
+  my ($epub) = @_;
+
+  my $title = get_title($epub);
+  my $retval = rename $epub => "$title" . ".epub";
+
+  say "$epub renamed to ${title}.epub" if $retval;
+}
+
+sub run {
+  die "$0 <epub>\n" unless @ARGV;
+
+  rename_epub($_) for @ARGV;
+}
+
+run() unless caller;
+
+# vim: ai:et:ts=2:sw=2:sts=2:tw=78:ft=perl
 # author:    Xu Xiaodong <xxdlhy@gmail.com>
 # license:   GPL
 # created:   2011 Apr  9
-# modified:  2011 Aug 15
+# modified:  2011 Nov 17
 #
 
 use strict;
 if ( $hour > 0 ) {
   say "${hour}h ${min}m ${sec}s";
 }
+elsif ($min > 0) {
+  say "${min}m ${sec}s";
+}
 else {
-  say "${min}m ${sec}s";
+  say "${sec}s";
 }
 
 # vim: ai:et:ts=2:sw=2:sts=2:tw=78:ft=perl
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.