#!c:/python25/python.exe
from __future__ import with_statement
import os
import re
import sqlite3
import string

def as_code (title):
  title = re.sub ("|".join ("\\%s" % i for i in string.punctuation), "", title.lower ())
  return "-".join (title.split ())

db = sqlite3.connect ("books.db")
for title, image in db.execute ("SELECT title, image FROM whatsnew"):
  if image:
    filename = os.path.join ("thumbnails", as_code (title) + ".jpg")
    print filename
    with open (filename, "wb") as f:
      f.write (image)