-
-
Save jflefebvre/8268761 to your computer and use it in GitHub Desktop.
Allows - to build an sqlite database storing name/path and creation date of your ebooks (-b parameter)
- easily find ebook (-f searchstring parameter)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import sqlite3 as lite | |
import string | |
import fnmatch | |
import os | |
import sys | |
import time | |
import argparse | |
parser = argparse.ArgumentParser(description="Allow to build or find ebooks") | |
parser.add_argument("-f", "--find", help="find ebook") | |
parser.add_argument("-b", "--build", help="build database",action="store_true") | |
args = parser.parse_args() | |
if args.build: | |
#print args.build | |
extensions = ['.pdf', '.epub', '.chm'] | |
def listFiles(dir): | |
rootdir = dir | |
for root, subFolders, files in os.walk(rootdir,topdown=False): | |
for file in files: | |
yield os.path.join(root,file) | |
return | |
def addEbook(cur, dir): | |
c = 1 | |
for f in listFiles(dir): | |
try: | |
base = os.path.basename(f) | |
base_dir = os.path.dirname(f) | |
filename_split = os.path.splitext(base) | |
ext = filename_split[1] | |
filesize = os.path.getsize(f) | |
#print base | |
if ext in extensions: | |
#print "Insert " + f | |
c+=1 | |
cur.execute("INSERT INTO ebooks(Name, Path, Ext, Filesize, CreationDate) VALUES(?,?,?,?,?)", (base, base_dir, ext, str(filesize),time.ctime(os.path.getctime(f)))) | |
except OSError as e: | |
print "I/O error({0}): {1}".format(e.errno, e.strerror) | |
print str(c) + " ebooks inserted" | |
return | |
con = lite.connect('/opt/local/bin/ebooks.db') | |
con.text_factory = str | |
with con: | |
cur = con.cursor() | |
cur.execute("DROP TABLE IF EXISTS ebooks") | |
cur.execute("CREATE TABLE ebooks(Id INTEGER PRIMARY KEY, Name TEXT, Path TEXT, Ext Text, Filesize INT, CreationDate DateTime Text)") | |
addEbook(cur, r"/Users/jeff/Downloads/ebooks") | |
addEbook(cur, r"/Volumes/STUDIO/ebooks") | |
addEbook(cur, r"/Volumes/Shawinigan/ebooks") | |
else: | |
if args.find: | |
con = lite.connect('/opt/local/bin/ebooks.db') | |
with con: | |
cur = con.cursor() | |
cur.execute("PRAGMA case_sensistive_like=ON;"); | |
cur.execute("SELECT * FROM ebooks WHERE Name LIKE ? ORDER BY Name ASC", ['%'+args.find+'%']) | |
rows = cur.fetchall() | |
print string.ljust("FILENAME", 80) + ' ' + string.ljust("CREATION DATE", 25) + ' ' + string.ljust("PATH", 100) | |
print ''.ljust(80, "-") + ' ' + ''.ljust(25, "-") + ' ' + ''.ljust(100, "-") | |
c = 0 | |
for row in rows: | |
c += 1 | |
print string.ljust(row[1].encode('utf-8')[0:75], 80) + ' ' + string.ljust(row[5].encode('utf-8'), 25) + ' ' + row[2].encode('utf-8') | |
print "Found " + str(c) + " ebooks" | |
else: | |
print parser.print_help() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment