mysqlHelper.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # -*- coding: utf-8 -*-
  2. import time
  3. import pymysql.cursors
  4. class MysqlHelper(object):
  5. def __init__(self, host, user, password, db, source):
  6. self.__conn = pymysql.connect(host=host, user=user, password=password, db=db, charset='utf8mb4',
  7. cursorclass=pymysql.cursors.DictCursor)
  8. self.source = source
  9. def get_connection(self):
  10. return self.__conn
  11. def get_book_info_by_source(self, source_bid):
  12. sql = 'select id from books where source_bid=%s and source = %s'
  13. with self.__conn.cursor() as cursor:
  14. cursor.execute(sql, (source_bid, self.source))
  15. result = cursor.fetchone()
  16. self.__conn.commit()
  17. return result
  18. def get_need_update_book_list(self):
  19. sql = 'select id,source_bid from books where source=%s and `status` = 0'
  20. with self.__conn.cursor() as cursor:
  21. cursor.execute(sql, (self.source, ))
  22. result = cursor.fetchall()
  23. self.__conn.commit()
  24. return result
  25. def get_last_cid_by_bid(self, bid):
  26. sql = "select id,bid,`name`,sequence,source_chapter_id from chapters where bid = %s" \
  27. " order by sequence desc limit 1"
  28. with self.__conn.cursor() as cursor:
  29. cursor.execute(sql, (int(bid), ))
  30. result = cursor.fetchone()
  31. self.__conn.commit()
  32. return result
  33. def insert_book(self, item):
  34. sql = '''
  35. insert into books(source_bid, `name`,author, intro, cover ,keyword , category_id,status,
  36. chapter_count,first_cid,last_cid,`size`,last_chapter,category_name,source,updated_at,created_at)
  37. values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
  38. '''
  39. with self.__conn.cursor() as cursor:
  40. cursor.execute(sql, (item.get('source_bid'),
  41. item.get('name'),
  42. item.get('author'),
  43. item.get('intro'),
  44. item.get('cover'),
  45. item.get('keyword'),
  46. item.get('category_id'),
  47. item.get('status'),
  48. item.get('chapter_count'),
  49. item.get('first_cid'),
  50. item.get('last_cid'),
  51. item.get('size'),
  52. item.get('last_chapter'),
  53. item.get('category_name'),
  54. item.get('source'),
  55. item.get('updated_at'),
  56. item.get('created_at')
  57. ))
  58. bid = int(cursor.lastrowid)
  59. self.__conn.commit()
  60. return bid
  61. def insert_chapter(self, item):
  62. chapter_content_id = self.insert_content(item)
  63. sql = "INSERT INTO `chapters` (`bid`, `name`,`sequence`,`size`,`is_vip`,`prev_cid`,`next_cid`,`recent_update_at`,`created_at`,`updated_at`,`chapter_content_id`,source_chapter_id) " \
  64. "VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
  65. with self.__conn.cursor() as cursor:
  66. cursor.execute(sql, (
  67. item['bid'], item['name'], item['sequence'], item['size'], item['is_vip'], item['prev_cid'],
  68. item['next_cid'], item['recent_update_at'], item['created_at'], item['updated_at'], chapter_content_id,
  69. item['source_chapter_id']))
  70. cid = int(cursor.lastrowid)
  71. self.__conn.commit()
  72. return cid
  73. def insert_content(self, item):
  74. sql = "insert into chapter_contents (chapter_name,content,created_at,updated_at) values (%s,%s,%s,%s)"
  75. with self.__conn.cursor() as cursor:
  76. cursor.execute(sql, (
  77. item['name'], item['content'], item['created_at'], item['updated_at']))
  78. content_id = int(cursor.lastrowid)
  79. self.__conn.commit()
  80. return content_id
  81. def get_book_list(self):
  82. sql = "select id,source_bid from books where source=%s"
  83. with self.__conn.cursor() as cursor:
  84. cursor.execute(sql, (self.source,))
  85. result = cursor.fetchall()
  86. self.__conn.commit()
  87. return result
  88. def get_chapter_info_by_source_cid(self, bid, source_chapter_id):
  89. sql = 'select id from chapters where bid=%s and source_chapter_id = %s'
  90. with self.__conn.cursor() as cursor:
  91. cursor.execute(sql, (bid, source_chapter_id))
  92. result = cursor.fetchone()
  93. self.__conn.commit()
  94. return result
  95. def get_book_info_by_id(self, bid):
  96. sql = 'select source_bid from books where id = %s and source=%s '
  97. with self.__conn.cursor() as cursor:
  98. cursor.execute(sql, (int(bid), self.source))
  99. result = cursor.fetchone()
  100. self.__conn.commit()
  101. return result
  102. def get_cid_by_bid_sequence(self, bid, sequence):
  103. sql = "select id,chapter_content_id from chapters where bid = %s and sequence=%s"
  104. with self.__conn.cursor() as cursor:
  105. cursor.execute(sql, (int(bid), int(sequence)))
  106. result = cursor.fetchone()
  107. self.__conn.commit()
  108. return result
  109. def update_content(self, content_id, chapter_name, content):
  110. now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  111. sql = 'update chapter_contents set chapter_name=%s,content=%s,updated_at=%s where id=%s'
  112. with self.__conn.cursor() as cursor:
  113. cursor.execute(sql, (
  114. chapter_name, content, now, int(content_id)))
  115. self.__conn.commit()
  116. def update_chapter(self, item):
  117. now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  118. sql = 'update chapters set `name`=%s,`sequence`=%s,`size`=%s,`is_vip`=%s,' \
  119. 'updated_at=%s,`source_chapter_id`=%s where id = %s'
  120. with self.__conn.cursor() as cursor:
  121. cid = int(item['cid'])
  122. cursor.execute(sql, (
  123. item['name'], item['sequence'], item['size'], item['is_vip'], now,
  124. item['source_chapter_id'], cid))
  125. self.__conn.commit()