View Single Post
Old 01-27-2014, 09:59 AM   #8
lormac
Junior Member
lormac began at the beginning.
 
Posts: 1
Karma: 10
Join Date: Jan 2014
Device: kobo glo
wasn't working for kobo glo, I had to tweak it a little bit, here it is:

#!/usr/bin/python

import sqlite3

conn = sqlite3.connect('KoboReader.sqlite')

c = conn.cursor()

c.execute('''SELECT DISTINCT BookID FROM content''')

import re
re_path = re.compile(r'^file:///mnt/(?P<dev>[^/]*)/(?P<path>.*)/(?P<filename>[^/]*)$')
#(r'^file:///mnt/(?P<dev>)([^/]?P<path>)([^/]?P<filename>)$')
#(r'^file:///mnt/(?P<dev>[^/]*)/(?P<path>.*)/(?P<filename>[^/]*)$')

def splitpath(p):
m = re_path.match(p)
# print('qui',m)
return (m.group('dev'), m.group('path'), m.group('filename'))

database = {}
for v in c.fetchall():
p = v[0]
if p and p.startswith('file:///mnt/'):
dev, dirpath, filename = splitpath(p)
print(dev , dirpath , filename)
if (dev,dirpath) not in database:
database[(dev,dirpath)] = []
database[(dev,dirpath)].append(filename)

for dev,dirpath in database:
shelf = dirpath.replace('/','_')
print('shelf',shelf)
c.execute("SELECT * FROM Shelf WHERE InternalName='prova'")
print(c.fetchone())
c.execute("SELECT * FROM Shelf WHERE InternalName='%s'" % shelf)
temp = c.fetchone()
print(temp)
if temp is None:
c.execute("INSERT INTO Shelf VALUES ('2014-01-27T15:09:57Z', '%s', '%s', '2014-01-27T15:09:57Z', '%s', NULL, 'false', 'true', 'false')" % (shelf,shelf,shelf))
c.execute("SELECT * FROM Shelf WHERE InternalName='%s'" % shelf)
for f in database[(dev,dirpath)]:
fullname = u'file:///mnt/%s/%s/%s' % (dev,dirpath,f)
fullname = fullname.replace("'","''")
c.execute("SELECT * FROM ShelfContent WHERE ShelfName='%s' AND ContentId='%s'" % (shelf, fullname))
r = c.fetchone()
print(r)
if r is None:
c.execute("INSERT INTO ShelfContent VALUES ('%s','%s','2014-01-27T15:09:57Z','false','false')" % (shelf,fullname))

conn.commit()
c.close()
lormac is offline   Reply With Quote