Browse Source

initial commit - woohoo

lotus 3 months ago
commit
c2486a8e85
6 changed files with 315 additions and 0 deletions
  1. 5 0
      LICENSE.md
  2. 53 0
      README.md
  3. 22 0
      cbr-convert.1
  4. 133 0
      cbr-convert.sh
  5. 19 0
      makefile
  6. 83 0
      runner-cbr.sh

+ 5 - 0
LICENSE.md

@@ -0,0 +1,5 @@
+Copyright 2019 lotus
+
+Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

+ 53 - 0
README.md

@@ -0,0 +1,53 @@
+# cbr-convert
+
+[![License](https://is.gd/GbWFMI)](https://opensource.org/licenses/ISC)
+
+### Motivation
+The .cbr (comic book rar) file format is proprietary and doesn't play well with all comic book readers. This script will recurse a directory and convert each .cbr file found to .cbz. We divide up the work by directory and use gnu parallel to execute on each folder. After the work is complete we cleanup the old cbr files and move them to the trash folder. This tool is meant to be a one-shot to convert your whole collection. Spaces in directories or filenames are anticipated and OK 👍. 
+
+### Installation
+Simply run:
+```bash
+make install      #(as root)
+
+# to uninstall
+make uninstall    #(as root)
+```
+
+### Dependencies
+In order to use this tool, you will need these things installed:
++ [unar](https://theunarchiver.com/command-line)
++ [zip](https://www.digitalocean.com/community/questions/how-to-install-zip-in-ubuntu)
++ [trash-cli](https://github.com/andreafrancia/trash-cli)
++ [GNU Parallel](https://www.gnu.org/software/parallel)
++ [make](http://man7.org/linux/man-pages/man1/make.1.html)
+
+>There are no requirements to use a specific shell, but I have not tested on all of them, only ksh and bash. The above dependencies should be available in your OS's package manager (apt, pkg, portage, dnf, etc.)
+
+### Usage
+```bash 
+cbr-convert ./MyComicsDir    # convert all cbr files found in this directory
+cbr-convert -j 8 ./Deadpool  # convert using 8 threads
+cbr-convert -h               # show this help message
+cbr-convert -v               # show version
+
+!!! - After conversion .cbr files are moved to trash - !!!
+(Note that this will recurse down into each subdirectory)
+```
+
+### Benchmarks
+```bash
+Dataset = 22GB
+CPU = 4th Gen i7 Quad Core
+Storage = 250 Sata III SSD
+
+-j 1 (sequential)      -j 8 (multithreaded)
+real	18m34.91s     real	5m49.69s
+
+Conclusion:
+While this tool defaults to 2 jobs, if you have more cores available, specify a higher -j value.
+```
+
+### License / Disclaimer
+This project is licensed under the [ISC](https://opensource.org/licenses/ISC) license. (See LICENSE.md)
+

+ 22 - 0
cbr-convert.1

@@ -0,0 +1,22 @@
+.TH CBR-CONVERT 1
+.SH NAME
+cbr-convert \- recursively convert directories of .cbr files in parallel
+.SH SYNOPSIS
+.B cbr-convert
+[\fB-j\fR]
+[\fB-h\fR]
+[\fB-v\fR]
+.IR directory ...
+.SH DESCRIPTION
+.B cbr-convert
+converts .cbr (comic book rar) files to .cbz (comic book zip) files.
+.SH OPTIONS
+.TP
+.BR -j " " \fInum\fR
+The number of jobs (i.e. threads) to start and process files with. If you do not specify the -j flag, the default number of jobs is two.
+.TP
+.BR -h "      "
+Display help information.
+.TP
+.BR -v
+Display version information.

+ 133 - 0
cbr-convert.sh

@@ -0,0 +1,133 @@
+#!/bin/sh
+#
+# author: lotus
+# license: isc (SEE LICENSE.md)
+# website: git.zerohack.xyz/lotus/cbr-convert
+#
+# --- Convert comic book files from .cbr to .cbz ---
+# The .cbr (comic book rar) file format is proprietary and doesn't play well
+# with all comic book readers. This script will recurse a directory and convert
+# each .cbr file found to .cbz
+#
+# Dependencies: trash-cli, zip, unar, gnu parallel
+
+VERSION="0.1"
+JOBS=2
+LOGDIR=~/.cbr-convert
+LOGFILE=$LOGDIR/errors.log
+
+trap fun_cleanup INT
+
+fun_echo_dirs () {
+    SAVEIFS=$IFS
+    IFS='
+'
+    for DIR in $(find "$@") ; do
+	if [ -d "$DIR" ] ; then
+	    echo "$DIR"
+        fi
+    done
+    IFS=$SAVEIFS
+}
+
+# call this function to start the script
+fun_run () {
+    fun_echo_dirs "$@" | parallel -j $JOBS runner-cbr "secretargument" {}
+}
+
+
+# log that an error occurred
+fun_log_error () {
+    if ! [ -d $LOGDIR ] ; then
+	mkdir $LOGDIR
+    fi
+    echo "$(date): $1" | tee -a $LOGFILE
+    echo "(logged @ $LOGFILE)"
+}
+
+
+# pass the error message, we echo it, log it, and print the help info
+fun_error () {
+    fun_log_error "$1"
+    echo ""
+    fun_print_usage
+    exit 1
+}
+
+
+# run when ctrl-c is pressed
+fun_cleanup () {
+    echo "caught sigint, exiting.."
+    exit 0
+}
+
+
+fun_print_usage () {
+    echo "usage:" 
+    echo "       cbr-convert ./MyComicsDir    # convert all cbr files found in this directory"
+    echo "       cbr-convert -j 8 ./Deadpool  # convert using 8 threads"
+    echo "       cbr-convert -h               # show this help message"
+    echo "       cbr-convert -v               # show version"
+    echo ""
+    echo "!!! - After conversion .cbr files are moved to trash - !!!"
+    echo "(Note that this will recurse down into each subdirectory)"
+}
+
+
+fun_print_info () {
+    echo "cbr-convert - .cbr comic book files to .cbz"
+    echo "author: lotus"
+    echo "license: isc"
+    fun_print_ver
+    echo ""
+    fun_print_usage
+}
+
+
+fun_print_ver () {
+    echo "cbr-convert version: $VERSION"
+}
+
+
+# checks if we were passed a valid dir
+fun_check_dir () {
+    if [ "$#" -gt 0 ] && [ -d "$@" ] && [ "$(ls -A "$@")" ] ; then
+	echo "Starting up..."
+	fun_run "$@"
+    else
+	fun_error "Please pass in a valid (i.e. non-empty) directory"
+    fi
+}
+
+
+# set the number of jobs (threads) to spawn
+fun_set_numjobs () {
+    if [ "$1" = "-j" ] ; then
+	JOBS="$2"
+	shift; shift
+	fun_check_dir "$@"
+    else
+	fun_error "Bad State! - fun_set_numjobs"
+    fi
+}
+
+
+# parse what arguments were passed in
+# call a corresponding function
+fun_check_args () {
+    if [ "$#" -ge 1 ] ; then
+	case "$1" in
+	    -j)  fun_set_numjobs "$@" ;;
+            -h)	 fun_print_info "$@" ;;
+            -v)  fun_print_ver "$@" ;;
+            *)	 fun_check_dir "$@" ;;
+	esac
+    else
+	fun_error "Pass in a directory or flag!"
+    fi
+}
+
+# -- ENTRY POINT--
+fun_check_args "$@"
+exit 0
+

+ 19 - 0
makefile

@@ -0,0 +1,19 @@
+PREFIX ?= /usr/local
+BINDIR ?= $(PREFIX)/bin
+MANDIR ?= $(PREFIX)/man/man1
+
+.POSIX: install
+
+all: help
+
+help:
+	@echo "please run 'make install' as root"
+install:
+	cp ./cbr-convert.sh $(BINDIR)/cbr-convert
+	cp ./runner-cbr.sh $(BINDIR)/runner-cbr
+	cp ./cbr-convert.1 $(MANDIR)
+uninstall:
+	rm $(BINDIR)/cbr-convert
+	rm $(BINDIR)/runner-cbr
+	rm $(MANDIR)/cbr-convert.1
+

+ 83 - 0
runner-cbr.sh

@@ -0,0 +1,83 @@
+#!/bin/sh
+#
+# author: lotus
+# license: isc
+# site: git.zerohack.xyz/lotus/cbr-convert
+#
+# Helper file for cbr-convert.sh
+# don't call this on its own.
+
+LOGDIR=~/.cbr-convert
+LOGFILE=$LOGDIR/errors.log
+
+trap fun_catch_sigint INT
+
+# check if cbr file
+# cuts off extension and compares it
+fun_is_cbr () {
+    L_FILE="$1"
+    # ignore directories
+    if [ -f "$L_FILE" ] ; then
+	L_FILE_EXT=$(echo "${L_FILE##*.}")
+	if [ $L_FILE_EXT = "cbr" ] ; then
+	    unset L_FILE L_FILE_EXT
+	    return 0    
+	fi
+	unset L_FILE_EXT
+    fi
+    unset L_FILE 
+    return 1
+}
+
+# run when ctrl-c is pressed
+fun_catch_sigint () {
+    exit 0
+}
+
+fun_log_error () {
+    if ! [ -d $LOGDIR ] ; then
+	mkdir $LOGDIR
+    fi
+    echo "$(date): $1" | tee -a $LOGFILE
+    exit 1
+}
+
+# were we called by cbr-convert?
+if ! [ "$1" = "secretargument" ] ; then
+    fun_log_error "Do not call this script on its own, use cbr-convert!"
+elif [ "$#" -lt 2 ] ; then
+    fun_log_error "You must pass a directory"
+else
+    shift
+fi
+
+# does the dir we were given exist?
+if [ -d "$1" ] ; then
+    cd "$1"
+    echo "Working On: $1"
+else
+    fun_log_error "Passed a bad dir? : $1"
+    exit 1
+fi
+
+CBRS_FOUND="no"
+for FILE in * ; do
+    [ -e "$FILE" ] || continue
+    if fun_is_cbr "$FILE" ; then
+	CBRS_FOUND="yes"
+	echo "Converting: $FILE"
+	DIR="${FILE%.*}"
+	mkdir "$DIR"
+	unar -quiet ./"$FILE" -o "$DIR"
+	zip --quiet -r "$DIR".cbz "$DIR"
+	rm -r "$DIR"
+    fi
+done # conversion complete
+
+# put .cbr files into trash
+if [ "$CBRS_FOUND" = "yes" ] ; then
+    trash-put *.cbr
+fi
+
+exit 0
+