mysqlHelper.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. # -*- coding: utf-8 -*-
  2. import time
  3. import pymysql.cursors
  4. class MysqlHelper(object):
  5. def __init__(self, host, user, password, db, source,source_id):
  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. self.source_id = source_id
  10. def get_book_info_by_source(self, source_bid):
  11. sql = 'select id from zy_books where cp_bid="%s" and cp_id = %s'
  12. with self.__conn.cursor() as cursor:
  13. cursor.execute(sql, (int(source_bid), self.source_id))
  14. result = cursor.fetchone()
  15. self.__conn.commit()
  16. return result
  17. def get_book_info_by_id(self,bid):
  18. sql = 'select id,cp_bid from zy_books where id= %s and cp_id = %s'
  19. with self.__conn.cursor() as cursor:
  20. cursor.execute(sql, (int(bid), self.source_id))
  21. result = cursor.fetchone()
  22. self.__conn.commit()
  23. return result
  24. def get_need_update_book_list(self):
  25. sql = 'select id,cp_bid from zy_books where cp_id=%s and `status` = 0'
  26. with self.__conn.cursor() as cursor:
  27. cursor.execute(sql, (self.source_id,))
  28. result = cursor.fetchall()
  29. self.__conn.commit()
  30. return result
  31. def get_last_cid_by_bid(self, bid):
  32. sql_format = "select id,bid,`name`,sequence,source_chapter_id from zy_book_chapters where bid = {} order by sequence desc limit 1"
  33. sql = sql_format.format(bid)
  34. with self.__conn.cursor() as cursor:
  35. cursor.execute(sql)
  36. result = cursor.fetchone()
  37. self.__conn.commit()
  38. return result
  39. def get_cid_by_bid_sequence(self, bid, sequence):
  40. sql = "select id,chapter_content_id from zy_book_chapters where bid = %s and sequence=%s"
  41. with self.__conn.cursor() as cursor:
  42. cursor.execute(sql, (int(bid), int(sequence)))
  43. result = cursor.fetchone()
  44. self.__conn.commit()
  45. return result
  46. def insert_book(self, item):
  47. now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  48. sql = '''
  49. insert into zy_books(cp_id,cp_name,cp_bid,`name`,author, intro, cover ,keyword , category_id,status
  50. ,`size`,category_name,channel,updated_at,created_at)
  51. values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
  52. '''
  53. with self.__conn.cursor() as cursor:
  54. cursor.execute(sql, (self.source_id,self.source,item.get('cp_bid'),
  55. item.get('name'),
  56. item.get('author'),
  57. item.get('intro'),
  58. item.get('cover'),
  59. item.get('keyword'),
  60. item.get('category_id'),
  61. item.get('status'),
  62. item.get('size'),
  63. item.get('category_name'),
  64. item.get('channel'),
  65. now,now
  66. ))
  67. bid = int(cursor.lastrowid)
  68. self.__conn.commit()
  69. return bid
  70. def insert_chapter(self, item):
  71. chapter_content_id = self.insert_content(item)
  72. sql = "INSERT INTO `zy_book_chapters` (`bid`, `name`,`sequence`,`size`,`is_vip`,`prev_cid`,`next_cid`," \
  73. "`recent_update_at`,`created_at`,`updated_at`,`chapter_content_id`,source_chapter_id) " \
  74. "VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
  75. with self.__conn.cursor() as cursor:
  76. cursor.execute(sql, (
  77. item['bid'], item['name'], item['sequence'], item['size'], item['is_vip'], item['prev_cid'],
  78. item['next_cid'], item['recent_update_at'], item['created_at'], item['updated_at'],
  79. chapter_content_id,
  80. item['source_chapter_id']))
  81. cid = int(cursor.lastrowid)
  82. self.__conn.commit()
  83. return cid
  84. def insert_content(self, item):
  85. sql = "insert into zy_book_chapter_contents (bid,chapter_name,content,created_at,updated_at) values (%s,%s,%s,%s,%s)"
  86. with self.__conn.cursor() as cursor:
  87. cursor.execute(sql, (
  88. item['bid'],item['name'], item['content'], item['created_at'], item['updated_at']))
  89. content_id = int(cursor.lastrowid)
  90. self.__conn.commit()
  91. return content_id
  92. def update_content(self, content_id, chapter_name, content):
  93. now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  94. sql = 'update zy_book_chapter_contents set chapter_name=%s,content=%s,updated_at=%s where id=%s'
  95. with self.__conn.cursor() as cursor:
  96. cursor.execute(sql, (
  97. chapter_name, content, now, int(content_id)))
  98. self.__conn.commit()
  99. def update_chapter(self, item):
  100. now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  101. sql = 'update zy_book_chapters set `name`=%s,`sequence`=%s,`size`=%s,`is_vip`=%s,' \
  102. 'updated_at=%s,`source_chapter_id`=%s where id = %s'
  103. with self.__conn.cursor() as cursor:
  104. cid = int(item['cid'])
  105. cursor.execute(sql, (
  106. item['name'], item['sequence'], item['size'], item['is_vip'], now,
  107. item['source_chapter_id'], cid))
  108. self.__conn.commit()
  109. def update_book_info(self, book_info):
  110. sql = 'update zy_books set `name`=%s,author=%s,intro=%s,cover=%s,channel=%s,category_name=%s,category_id=%s where id =%s'
  111. with self.__conn.cursor() as cursor:
  112. cursor.execute(sql, (
  113. book_info['name'], book_info['author'], book_info['intro'], book_info['cover'],
  114. book_info['channel'], book_info['category'],book_info['category_id'], int(book_info['bid'])))
  115. self.__conn.commit()
  116. def update_book_status(self, bid,status):
  117. sql = 'update zy_books set status={} where id ={}'.format(status,bid)
  118. with self.__conn.cursor() as cursor:
  119. cursor.execute(sql)
  120. self.__conn.commit()
  121. def re_sequence(self, bid):
  122. sql = '''
  123. update zy_book_chapters a join (
  124. SELECT id,(@a:=@a+1) as sequence FROM zy_book_chapters,(SELECT @a:=0) as a WHERE bid = {} ORDER BY sequence
  125. ) b on a.id = b.id set a.sequence = b.sequence where a.bid = {}
  126. '''.format(bid, bid)
  127. with self.__conn.cursor() as cursor:
  128. cursor.execute(sql)
  129. self.__conn.commit()
  130. def after_spider(self,bid,start=1):
  131. chapter_list = self.get_simple_chapter_list(bid,start)
  132. if chapter_list is None:
  133. return None
  134. point = 0
  135. for chapter_item in chapter_list:
  136. if point == 0:
  137. point = chapter_item['id']
  138. continue
  139. sql1 = 'update zy_book_chapters set next_cid={} where id={}'.format(chapter_item['id'],point)
  140. sql2 = 'update zy_book_chapters set prev_cid={},next_cid=0 where id={}'.format(point,chapter_item['id'])
  141. self.simple_update(sql1)
  142. self.simple_update(sql2)
  143. point = chapter_item['id']
  144. book_info = self.get_base_info(bid)
  145. book_sql = '''
  146. update zy_books set size={},chapter_count={},first_cid={},last_cid={},last_chapter='{}' where id = {}
  147. '''.format(book_info['size'],book_info['chapter_count'],book_info['first_cid'],book_info['last_cid'],book_info['last_chapter'],bid)
  148. self.simple_update(book_sql)
  149. def after_fix_delete_unnecessary(self,bid,start):
  150. sql = 'update zy_book_chapters set bid=-bid where bid={} and sequence > {}'.format(bid,start)
  151. self.simple_update(sql)
  152. def get_base_info(self,bid):
  153. sql = '''
  154. SELECT id as last_cid,`name` as last_chapter,( SELECT id FROM zy_book_chapters WHERE bid = {} ORDER BY sequence limit 1 ) as first_cid ,
  155. ( SELECT count(*) FROM zy_book_chapters WHERE bid = {} ) as chapter_count,
  156. ( SELECT sum(size) FROM zy_book_chapters WHERE bid = {} ) as size
  157. FROM zy_book_chapters where bid = {} ORDER BY sequence desc LIMIT 1
  158. '''.format(bid,bid,bid,bid)
  159. with self.__conn.cursor() as cursor:
  160. cursor.execute(sql)
  161. result = cursor.fetchone()
  162. self.__conn.commit()
  163. return result
  164. def get_simple_chapter_list(self,bid,start):
  165. sql = 'select id from zy_book_chapters where bid = {} and sequence >={} order by sequence'.format(bid,start)
  166. with self.__conn.cursor() as cursor:
  167. cursor.execute(sql)
  168. result = cursor.fetchall()
  169. self.__conn.commit()
  170. return result
  171. def simple_update(self,sql):
  172. with self.__conn.cursor() as cursor:
  173. cursor.execute(sql)
  174. self.__conn.commit()