#!/usr/bin/env python
import subprocess, os, popen2, fcntl, select, fnmatch, string, sys
#argu = sys.argv[1:]
argu = sys.argv[0:] # use this instead of above to include command as an argument
dest = "/mnt/installs/ebooks/all/iomari/"
lfolder = "/home/iomari/calibre_library/iomari/"
database = "metadata.db"
who = subprocess.Popen("whoami", shell=True, stdout=subprocess.PIPE)
who = who.stdout.read()
who = who[:-1]
idx = 0
choose = []
stime = subprocess.Popen('date +"%H:%M:%S"', shell=True, stdout=subprocess.PIPE)
stime = stime.stdout.read()
stime = stime[:-1]
if not who == "iomari":
print "\nYOU ARE CURRENTLY LOGGED IN AS \""+who+"\". YOU MUST LOG IN AS \"iomari\" for this to work.\n" ;
sys.exit() ;
format = subprocess.Popen("sqlite3 "+ lfolder+database+ " 'select * from data;' | cut -d '|' -f 3 | sort | uniq", shell=True, stdout=subprocess.PIPE)
out = format.stdout.read()
out2 = out.split("\n")
del out2[-1] # remove last empty item
def export():
print "\ndeleting "+buff+" files"
op = subprocess.call("rm -rf "+dest+buff, shell=True)
print "exporting " + buff + " files.............."
print "calibredb export --formats="+buff+" --template='"+buff+"/{#supercat}/{tags}/{authors}/{title} - {authors}' --to-dir="+dest+" --to-lowercase --all\n"
xrun = subprocess.Popen("calibredb export --formats="+buff+" --template='"+buff+"/{#supercat}/{tags}/{authors}/{title} - {authors}' --to-dir="+dest+" --to-lowercase --all", shell=True, stdout=subprocess.PIPE)
go = xrun.stdout.read()
print go
print "exporting all books.............\n"
if len(argu) == 2 and argu[1] == "all": # export all books
for buff in out2:
buff = buff.lower()
export()
else: # Add code here to choose which formats to export
for buff in out2:
buff = buff.lower()
ans = raw_input( "Do you want to export "+ buff+" books [y/n]? " )
ans = ans.lower()
choose.append(ans)
for buff in out2:
buff = buff.lower()
if choose[idx] == "y":
export()
idx = idx + 1
print"\nfinished\n"
sys.exit()