You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 line
2.7 KiB

2 年之前
  1. import os
  2. import sqlite3 as sqlite
  3. import random
  4. import base64
  5. import simplejson as json
  6. class Book:
  7. id: str
  8. title: str
  9. author: str
  10. publisher: str
  11. original_title: str
  12. translator: str
  13. pub_year: str
  14. pages: int
  15. price: int
  16. binding: str
  17. isbn: str
  18. author_intro: str
  19. book_intro: str
  20. content: str
  21. tags: [str]
  22. pictures: [bytes]
  23. def __init__(self):
  24. self.tags = []
  25. self.pictures = []
  26. class BookDB:
  27. def __init__(self, large: bool = False):
  28. parent_path = os.path.dirname(os.path.dirname(__file__))
  29. self.db_s = os.path.join(parent_path, "data/book.db")
  30. self.db_l = os.path.join(parent_path, "data/book_lx.db")
  31. if large:
  32. self.book_db = self.db_l
  33. else:
  34. self.book_db = self.db_s
  35. def get_book_count(self):
  36. conn = sqlite.connect(self.book_db)
  37. cursor = conn.execute(
  38. "SELECT count(id) FROM book")
  39. row = cursor.fetchone()
  40. return row[0]
  41. def get_book_info(self, start, size) -> [Book]:
  42. books = []
  43. conn = sqlite.connect(self.book_db)
  44. cursor = conn.execute(
  45. "SELECT id, title, author, "
  46. "publisher, original_title, "
  47. "translator, pub_year, pages, "
  48. "price, currency_unit, binding, "
  49. "isbn, author_intro, book_intro, "
  50. "content, tags, picture FROM book ORDER BY id "
  51. "LIMIT ? OFFSET ?", (size, start))
  52. for row in cursor:
  53. book = Book()
  54. book.id = row[0]
  55. book.title = row[1]
  56. book.author = row[2]
  57. book.publisher = row[3]
  58. book.original_title = row[4]
  59. book.translator = row[5]
  60. book.pub_year = row[6]
  61. book.pages = row[7]
  62. book.price = row[8]
  63. book.currency_unit = row[9]
  64. book.binding = row[10]
  65. book.isbn = row[11]
  66. book.author_intro = row[12]
  67. book.book_intro = row[13]
  68. book.content = row[14]
  69. tags = row[15]
  70. picture = row[16]
  71. for tag in tags.split("\n"):
  72. if tag.strip() != "":
  73. book.tags.append(tag)
  74. for i in range(0, random.randint(0, 9)):
  75. if picture is not None:
  76. encode_str = base64.b64encode(picture).decode('utf-8')
  77. book.pictures.append(encode_str)
  78. books.append(book)
  79. # print(tags.decode('utf-8'))
  80. # print(book.tags, len(book.picture))
  81. # print(book)
  82. # print(tags)
  83. return books