Advertisement
Guest User

Kobo shelves building

a guest
Jul 26th, 2012
824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.47 KB | None | 0 0
  1. import sqlite3
  2.  
  3. conn = sqlite3.connect('KoboReader.sqlite')
  4.  
  5. c = conn.cursor()
  6.  
  7. c.execute('''SELECT DISTINCT BookID FROM content''')
  8.  
  9. import re
  10. re_path = re.compile(r'^file:///mnt/(?P<dev>[^/]*)/(?P<path>.*)/(?P<filename>[^/]*)$')
  11.  
  12. def splitpath(p):
  13.     m = re_path.match(p)
  14.     return (m.group('dev'), m.group('path'), m.group('filename'))
  15.  
  16. database = {}
  17. for v in c.fetchall():
  18.     p = v[0]
  19.     if p and p.startswith('file:///mnt/'):
  20.         dev, dirpath, filename = splitpath(p)
  21.         if (dev,dirpath) not in database:
  22.             database[(dev,dirpath)] = []
  23.         database[(dev,dirpath)].append(filename)
  24.  
  25. for dev,dirpath in database:
  26.     shelf = dirpath.replace('/','_')
  27.     c.execute("SELECT * FROM Shelf WHERE InternalName='%s'" % shelf)
  28.     if c.fetchone() is None :
  29.         c.execute("INSERT INTO Shelf VALUES ('2012-07-25T19:59:29Z', NULL, '%s', '2012-07-25T19:59:29Z', '%s', NULL, 'false', 'true', 'false')" % (shelf,shelf))
  30.         c.execute("SELECT * FROM Shelf WHERE InternalName='%s'" % shelf)
  31.     for f in database[(dev,dirpath)]:
  32.         fullname = u'file:///mnt/%s/%s/%s' % (dev,dirpath,f)
  33.         fullname = fullname.replace("'","''")
  34.         c.execute("SELECT * FROM ShelfContent WHERE ShelfName='%s' AND ContentId='%s'" % (shelf, fullname))
  35.         r = c.fetchone()
  36.         if r is None:
  37.             c.execute("INSERT INTO ShelfContent VALUES ('%s','%s','2012-07-25T19:59:29Z','false','false')" % (shelf,fullname))
  38.  
  39. conn.commit()
  40. c.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement