Saturday September 20 , 2014
Text Size
   
Welcome, Guest
Username: Password: Remember me
Python Scripts for ComicRack
  • Page:
  • 1
  • 2

TOPIC: cbr2cbz RAR to ZIP conversion for linux

cbr2cbz RAR to ZIP conversion for linux 1 year 7 months ago #30013

  • Karvajalka
  • Karvajalka's Avatar
  • OFFLINE
  • Senior Boarder
  • Posts: 67
  • Thank you received: 4
  • Karma: 2
Hello,


This is not really ComicRack script, but if someone has comics in linux based NAS, this might be useful script. The script goes through directory structure and converts all cbrs to cbzs. It's just a part of my normal process to do the conversion anyway. So, I thought I can let the NAS machine do the job for me.

Just go to directory you want to convert and run the script. Or give the directory to process as parameter. For example following command would go through whole /comics directory and all subdirectories:

cbr2cbz /comics

You should always use this script BEFORE you add files to your library. In the process file name and size changes. So, it will look like new comic book for ComicRack. That means you will loose data which is not stored into the archive itself. With RARs that would be pretty much everything.


Here is the script
#!/bin/bash
echo "Converting CBRs to CBZs"

# Set the "field separator" to something other than spaces/newlines" so that spaces
# in the file names don't mess things up. I'm using the pipe symbol ("|") as it is very
# unlikely to appear in a file name.
IFS="|"

# Set working directory where to create the temp dir. The user you are using must have permission
# to write into this directory. 
# For performance reasons I'm using ram disk (/dev/shm/) in Ubuntu server.
WORKDIR = "/dev/shm/"

# Set name for the temp dir. This directory will be created under WORDDIR 
TEMPDIR="cbr2cbz"

# The script should be invoked as "cbr2cbz {directory}", where "{directory}" is the
# top-level directory to be searched. Just to be paranoid, if no directory is specified,
# then default to the current working directory ("."). Let's put the name of the
# directory into a shell variable called SOURCEDIR.
# Note: "$1" = "The first command line argument"
if test -z $1; then
	SOURCEDIR=`pwd`
else
	SOURCEDIR="$1"
fi

echo "Working from directory $SOURCEDIR"

# We need an empty directory to work in, so we'll create a temp directory here
cd $WORKDIR
mkdir $TEMPDIR
# and step into it
cd $TEMPDIR

# Now, execute a loop, based on a "find" command in the specified directory. The
# "-printf "$p|" will cause the file names to be separated by the pipe symbol, rather than
# the default newline. Note the backtics ("`") (the key above the tab key on US
# keyboards).
for CBRFILE in `find $SOURCEDIR -name "*.cbr" -printf "%p|"`; do

	# Now for the actual work. First, extract the base file name (without the extension)
	# using the "basename" command. Warning: more backtics.
	BASENAME=`basename $CBRFILE ".cbr"`

	# And the directory path for that file, so we know where to put the finished ".cbz"
	# file.
	DIRNAME=`dirname $CBRFILE`

	# Now, build the "new" file name,
	NEWNAME="$BASENAME.cbz"

	# We use RAR file's name to create folder for unpacked files
	echo "Processing $CBRFILE"
	mkdir "$BASENAME"
	# and unpack the rar file into it
	unrar e "$CBRFILE" "$BASENAME"
	cd "$BASENAME"

	# Lets ensure the permissions allow us to pack everything
	sudo chmod 777 -R ./*

	# Put all the extracted files into new ".cbz" file
	zip "$NEWNAME" *

	# And move it to the directory where we found the original ".cbr" file
	mv "$NEWNAME" $DIRNAME/"$NEWNAME"

	# Finally, "cd" back to the original working directory, and delete the temp directory
	# created earlier.
	cd ..
	rm -r "$BASENAME"

	# Delete the RAR file also
	rm "$CBRFILE"

done

# At the end we cleanup by removing the temp folder from ram disk
cd ..
echo "Conversion Done"
rm -r $TEMPDIR


I got the original version from here. And then modified it little bit to utilize ram disk etc...

I have Ubuntu server running and I haven't tested it in other distros. Also, there is no checkups for corrupted zips. So, test it well before running for all your comics...
The administrator has disabled public write access.
The following user(s) said Thank You: danielpublic

Re: cbr2cbz RAR to ZIP conversion for linux 1 year 7 months ago #30018

  • ComicTagger
  • ComicTagger's Avatar
  • OFFLINE
  • Senior Boarder
  • Posts: 43
  • Thank you received: 13
  • Karma: 6
I saw this original a few weeks ago. Nice addition of using the ramdisk!

If your NAS has python (2.6 or 2.7) on it, you might want to try using ComicTagger on it.

The script here will convert to zip, tag with ComicRack and ComicBookLover tags, and rename:
comictagger.forumotion.com/t47-post-download-script#200

You can install ComicTagger and its requirements with:
pip install beautifulsoup4 pil comictagger

(Make note at the end where pip installs "comictagger.py". That's the executable. )

You probably need to edit the settings file before you start. Run comictagger.py once with a --help option, so that it will create a settings file ($HOME/.ComicTagger/settings) . Edit the setting file and make sure that "unrar_exe_path" is set. Near the end of the file you might want to change the "rename_template"

I have run this on a Raspberry Pi, and it does work, albeit a bit slow. I assume as long as you have python on your NAS, you're good. I am curious!

Regarding the conversion script:
  • You will need to change $COMICTAGGER to point to the comictagger.py executable on your machine
  • Right now, the script operates on a flat folder of comics, and then moves them to another folder. Depending on your needs, you might want to change that. ComicTagger now supports a recursive option (-R) that could be used to operate more like your original script.
  • The rar to zip process will abort if there are failures to read or write, so it should be safe, but if you're the nervous sort, you might want to remove the "--delete-rar" option. (That said, I've converted thousands of comics with no issues). If you don't delete, though, you should move the original out of the way.

(Of course, you can do this on any linux or mac too!)
The administrator has disabled public write access.

Re: cbr2cbz RAR to ZIP conversion for linux 1 year 6 months ago #30372

Just wanted to say Thanks as I stumbled over this thread looking for something like this and Nice touch with the ramdisk!
I modified it slightly to use p7zip instead of the usual 'zip' as it's what I use otherwise.
#!/bin/bash
#Source: http://comicrack.cyolito.com/forum/13-scripts/30013-cbr3cbz-rar-to-zip-conversion-for-linux
echo "Converting CBRs to CBZs"

# Set the "field separator" to something other than spaces/newlines" so that spaces
# in the file names don't mess things up. I'm using the pipe symbol ("|") as it is very
# unlikely to appear in a file name.
IFS="|"

# Set working directory where to create the temp dir. The user you are using must have permission
# to write into this directory.
# For performance reasons I'm using ram disk (/dev/shm/) in Ubuntu server.
WORKDIR = "/dev/shm/"

# Set name for the temp dir. This directory will be created under WORDDIR
TEMPDIR="cbr2cbz"

# The script should be invoked as "cbr2cbz {directory}", where "{directory}" is the
# top-level directory to be searched. Just to be paranoid, if no directory is specified,
# then default to the current working directory ("."). Let's put the name of the
# directory into a shell variable called SOURCEDIR.
# Note: "$1" = "The first command line argument"
if test -z $1; then
        SOURCEDIR=`pwd`
else
        SOURCEDIR="$1"
fi

echo "Working from directory $SOURCEDIR"

# We need an empty directory to work in, so we'll create a temp directory here
cd $WORKDIR
mkdir $TEMPDIR
# and step into it
cd $TEMPDIR

# Now, execute a loop, based on a "find" command in the specified directory. The
# "-printf "$p|" will cause the file names to be separated by the pipe symbol, rather than
# the default newline. Note the backtics ("`") (the key above the tab key on US
# keyboards).
for CBRFILE in `find $SOURCEDIR -name "*.cbr" -printf "%p|"`; do

        # Now for the actual work. First, extract the base file name (without the extension)
        # using the "basename" command. Warning: more backtics.
        BASENAME=`basename $CBRFILE ".cbr"`

        # And the directory path for that file, so we know where to put the finished ".cbz"
        # file.
        DIRNAME=`dirname $CBRFILE`

        # Now, build the "new" file name,
        NEWNAME="$BASENAME.cbz"

        # We use RAR file's name to create folder for unpacked files
        echo "Processing $CBRFILE"
        mkdir "$BASENAME"
        # and unpack the rar file into it
        unrar e "$CBRFILE" "$BASENAME"
        cd "$BASENAME"

        # Lets ensure the permissions allow us to pack everything
        sudo chmod 777 -R ./*
        
	# Put all the extracted files into new ".cbz" file
        7z a -tzip -mx=9 "$NEWNAME" *

        # And move it to the directory where we found the original ".cbr" file
        mv "$NEWNAME" $DIRNAME/"$NEWNAME"

        # Finally, "cd" back to the original working directory, and delete the temp directory
        # created earlier.
        cd ..
        rm -r "$BASENAME"

        # Delete the RAR file also
        rm "$CBRFILE"

done

# At the end we cleanup by removing the temp folder from ram disk
cd ..
echo "Conversion Done"
rm -r $TEMPDIR
The administrator has disabled public write access.

Re: cbr2cbz RAR to ZIP conversion for linux 1 year 6 months ago #30386

  • startreksteve
  • startreksteve's Avatar
  • OFFLINE
  • Platinum Boarder
  • Posts: 379
  • Thank you received: 27
  • Karma: 6
Sounds really nice, but....
Why?
I read on ComicRack Android and it displays all formats apart from Solid archives.
The administrator has disabled public write access.

Re: cbr2cbz RAR to ZIP conversion for linux 1 year 6 months ago #30387

  • Karvajalka
  • Karvajalka's Avatar
  • OFFLINE
  • Senior Boarder
  • Posts: 67
  • Thank you received: 4
  • Karma: 2
startreksteve wrote:
Sounds really nice, but....
Why?

Because when you are scraping info for the comics from Comic Vine or want to store other data, you can't do it with CBRs in ComicRack Windows. So, I want to convert the RARs to ZIPs and also store the metadata into the comic book files itself. Then if something nasty happens CR's database file. I don't need to rescrape everything.
The administrator has disabled public write access.

Re: cbr2cbz RAR to ZIP conversion for linux 1 year 6 months ago #30388

  • startreksteve
  • startreksteve's Avatar
  • OFFLINE
  • Platinum Boarder
  • Posts: 379
  • Thank you received: 27
  • Karma: 6
Ah, got it!
I only use CRA so that why I wasnt aware of that,
Cheers
Steve
The administrator has disabled public write access.

Re: cbr2cbz RAR to ZIP conversion for linux 1 year 6 months ago #30389

startreksteve wrote:
Sounds really nice, but....
Why?
I read on ComicRack Android and it displays all formats apart from Solid archives.

As a Libre software zealot, I don't like proprietary software(rar) and use it as little as possible. In other words, I have not used comicrack as it's dito and not available yet on linux platforms. I basically stumbled upon this thread/script.
Last Edit: 1 year 6 months ago by danielpublic.
The administrator has disabled public write access.

Re: cbr2cbz RAR to ZIP conversion for linux 1 year 6 months ago #30406

  • ComicTagger
  • ComicTagger's Avatar
  • OFFLINE
  • Senior Boarder
  • Posts: 43
  • Thank you received: 13
  • Karma: 6
In addition, in many reader apps, reading zips can be faster than reading rars.
The administrator has disabled public write access.

Re: cbr2cbz RAR to ZIP conversion for linux 1 year 6 months ago #30414

  • startreksteve
  • startreksteve's Avatar
  • OFFLINE
  • Platinum Boarder
  • Posts: 379
  • Thank you received: 27
  • Karma: 6
danielpublic wrote:
startreksteve wrote:
Sounds really nice, but....
Why?
I read on ComicRack Android and it displays all formats apart from Solid archives.

As a Libre software zealot, I don't like proprietary software(rar) and use it as little as possible. In other words, I have not used comicrack as it's dito and not available yet on linux platforms. I basically stumbled upon this thread/script.

I've been using Linux ONLY for 7 years (I'm the chairman of our local LUG), but if a program is the best there is, I have no problem paying for it. I use Linux because it does it BETTER than Windows and its free.

Steve
The administrator has disabled public write access.

Re: cbr2cbz RAR to ZIP conversion for linux 1 year 6 months ago #30415

startreksteve wrote:
danielpublic wrote:
startreksteve wrote:
Sounds really nice, but....
Why?
I read on ComicRack Android and it displays all formats apart from Solid archives.

As a Libre software zealot, I don't like proprietary software(rar) and use it as little as possible. In other words, I have not used comicrack as it's dito and not available yet on linux platforms. I basically stumbled upon this thread/script.

I've been using Linux ONLY for 7 years (I'm the chairman of our local LUG), but if a program is the best there is, I have no problem paying for it. I use Linux because it does it BETTER than Windows and its free.

Steve
Thread starting to go OT, but.. can one use comicrack on linux? Are you using it through wine?

On a more on topic note, I'm looking at bash (somewhat for the first time, definitely for the first time on 'if') and trying to figure out howto use/add the testing -t if 'Everything is Ok' then next or otherwise redo the job, to the script.
Last Edit: 1 year 6 months ago by danielpublic. Reason: testing feature
The administrator has disabled public write access.
  • Page:
  • 1
  • 2
Time to create page: 0.221 seconds

Who's Online

We have 228 guests and 1 member online
  • oraclexview

PIM

You are not logged in.