class BookParserTest(unittest.TestCase): def setUp(self): self.book_parser = BookParser() def test_load_word_data(self): expected_dict = { 'hello': [ {'word': 'hello', 'sentence': 'he said hello', 'book': 'Treasure Island', 'chapter': '10 - Ahoy', 'filename': 'file1.html'} ] } self.book_parser.load_word_data( 'hello', 'file1.html', 'he said hello', 'Treasure Island', '10 - Ahoy') self.assertItemsEqual(expected_dict, self.book_parser.get_full_dict())
def create_database(): """ Uses BookParser to process the html book files into a data structure in memory then iterates through and stores in a sqlite file. """ conn = sqlite3.connect('test.db') cur = conn.cursor() create_table = """ CREATE TABLE IF NOT EXISTS words (word TEXT PRIMARY KEY, serialized_context_list TEXT) """ cur.execute(create_table) insert_row = """ INSERT OR REPLACE INTO words VALUES (?, ?) """ book_parser = BookParser() book_parser.initiate() for word, context_list in book_parser.get_full_dict().iteritems(): cur.execute(insert_row, (word.decode('utf-8'), json.dumps(context_list))) conn.commit() conn.close()
def setUp(self): self.book_parser = BookParser()
def __init__(self): self.bookParser = BookParser()
class BookService(object): def __init__(self): self.bookParser = BookParser() def list_user_books(self,uid,start,end): """list the books of a given user by uid""" sqls="SELECT a.bid,isbn10,isbn13,title,subtitle,author,translators,publisher,pubdate,price,pages,update_time,create_time,quantity,\ series,keywords,summary,b.status \ FROM "+TABLE_USERBOOK+" a RIGHT JOIN "+TABLE_BOOK+" b ON a.bid=b.bid WHERE a.uid=%d " % uid if end: sqls+= " LIMIT %d,%d" % (start,end) logger.debug(sqls) result= db.query(sqls) books=[] if result: for r in result: book = self.compose_book(r) books.append(book) return books def list_books(self,start,nfetch,query_in_title=None): """list the books """ sqlc="SELECT COUNT(*) as total FROM bm_book " sqls="SELECT bid,isbn10,isbn13,title,subtitle,author,translators,publisher,pubdate,price,pages,update_time,create_time,quantity,\ series,keywords,summary,status \ FROM "+TABLE_BOOK if query_in_title: sqlwhere=" WHERE title LIKE '%"+query_in_title+"%' OR author LIKE '%"+query_in_title+"%'" sqls+=sqlwhere sqlc +=sqlwhere sqls +=" ORDER BY title,author " sqls+=" LIMIT %d,%d" % (start,nfetch) logger.debug(sqls) total=0 result=db.query(sqlc) if result: for r in result: total = r['total'] result = db.query(sqls) books=[] if result: for r in result: book=self.compose_book(r) books.append(book) return books,total def compose_book(self,r): book = Book() book.id = r['bid'] book.isbn10 = r['isbn10'] book.isbn13 = r['isbn13'] book.title = r['title'] book.subtitle = r['subtitle'] book.author = r['author'] book.translators = r['translators'] book.publisher = r['publisher'] book.pubdate = r['pubdate'] book.price = r['price'] book.pages = r['pages'] book.update_time = r['update_time'] book.create_time = r['create_time'] book.quantity = r['quantity'] book.series = r['series'] book.keywords = r['keywords'] book.summary = r['summary'] book.status = r['status'] book.transtr = book._transtr() return book def get_book_byisbn_fromremote(self,isbn): return self.bookParser.parsebookbyisbn(isbn) def insert_book(self,isbn,uid=None): """get book by isbn from douban and insert it into local db""" try: if not uid: uid=1 book = self.get_book_byisbn(isbn) if book and book.id: #check if it's already in user book list? sqls="select 1 FROM %s WHERE `uid`=%d and `bid`=%d" %(TABLE_USERBOOK,uid,book.id) result=db.query(sqls) if result: logger.debug(("already exist:",isbn)) return else: self.add_userbook(uid,book.id) else: book = self.get_book_byisbn_fromremote(isbn) if book : t=db.transaction() bid = self.create_book(book) if bid: self.add_userbook(uid,bid) else: logger.warn(('failed to get bid:', bid)) t.commit() else: logger.warn(('book not returned:%s' % isbn)) except Exception,e: logger.error(e)