Ok, after some annoying issues with mobi2mobi sometimes parsing the file as binary, and that using the search for the author field with search term 'Author' instead of 'item: 100' is stupid as the tag description or excerpt might have the word author in it, here's a much more stringent bash .sh file.
There's only 3 small changes, and it now works for all my files
Code:
#!/bin/bash
count1=0
asin1="B00"
FILES="*.mobi"
for g in $FILES
do
if [ "$g" ]
then
count1=$[$count1+1]
continue
fi
done
echo "There is $count1 files"
rand1=$[1000-$count1]
while [ $rand1 -lt 1000 ]
do
rand1=$[ (( $RANDOM % ( $[ 9999 - 1000 ] + 1 ) ) + 1000) - $count1 ]
done
rand2=$[ ( $RANDOM % ( $[ 999 - 100 ] + 1 ) ) + 100 ]
for f in $FILES
do
if [ "$f" ]
then
mobi2mobi --outfile "$f" --delexthtype 113 "$f"> /dev/null 2>&1
mobi2mobi --outfile "$f" --delexthtype 501 "$f"> /dev/null 2>&1
author=$(mobi2mobi "$f" | grep -a "item: 100" | cut -d- -f4)
title=$(mobi2mobi "$f" | grep -a LONGTITLE | cut -c11-100)
echo "Author is $author, and Title is $title"
python asin_search.py "$title" "$author">asin.txt
search1=`head -n 1 asin.txt`
if [ "$search1" ]
then
mobi2mobi --outfile "$f" --exthtype 113 --exthdata $search1 "$f"> /dev/null 2>&1
echo "Gave a real ASIN number to: $title. It's new number is $search1"
else
mobi2mobi --outfile "$f" --exthtype 113 --exthdata $asin1$rand2$rand1 "$f"> /dev/null 2>&1
echo "Gave a fake ASIN number to: $title. It's new number is $asin1$rand2$rand1"
rand1=$[$rand1+1]
fi
mobi2mobi --outfile "$f" --addexthtype 501 --exthdata "EBOK" "$f"> /dev/null 2>&1
rm asin.txt
continue
fi
done