Want more features on Pastebin? Sign Up, it's FREE!
Guest

Kobo shelves building

By: a guest on Jul 26th, 2012  |  syntax: Python  |  size: 1.47 KB  |  views: 431  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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()
clone this paste RAW Paste Data