''' Description: Author: Fishermanykx Date: 2021-01-13 09:20:57 LastEditors: Fishermanykx LastEditTime: 2021-01-14 08:26:20 ''' import pymysql from pprint import pprint MYSQL_HOSTS = "192.168.136.129" MYSQL_USER = "root" MYSQL_PASSWORD = "1213800" MYSQL_PORT = 3306 MYSQL_DB = "computer_accessories" class UpdateTrendings: def __init__(self, resDict): self.resDict = resDict if not resDict['flag']: self.config_list = resDict['result'] else: print("No legal configurations, so no updates") exit(0) def update(self): cfgs = self.config_list index = 0 for cfg in cfgs: all_link = "" # 构建返回值 dict res = {} for key in cfg: if (type(cfg[key]) is int): continue all_link += cfg[key][1] root_str = str(key) res[root_str] = cfg[root_str][0] res[root_str+'_price'] = cfg[root_str][2] res[root_str+'_link'] = cfg[root_str][1] # print(cfg[key][1]) hash_val = self.hashStr(all_link) # print(hash_val) cfg_id = str(hash_val) res['id'] = cfg_id res['totalPrice'] = cfg['totalPrice'] connection = pymysql.connect( host=MYSQL_HOSTS, port=MYSQL_PORT, user=MYSQL_USER, passwd=MYSQL_PASSWORD, db=MYSQL_DB, charset="utf8") # 获取游标 cursor = connection.cursor(cursor=pymysql.cursors.DictCursor) # frequency query = "select * from trendings where id="+cfg_id row = cursor.execute(query) if not row: res['frequency'] = 1 # pprint(res) else: data = cursor.fetchone() res['frequency'] = data['frequency'] + 1 # 插入数据库 sql_insert = "INSERT INTO trendings VALUES "\ "(%(id)s, %(frequency)s, %(CPU)s, %(CPU_price)s, %(CPU_link)s, %(motherboard)s, %(motherboard_price)s, %(motherboard_link)s, %(GPU)s, %(GPU_price)s, %(GPU_link)s, "\ "%(memory)s, %(memory_price)s, %(memory_link)s, %(CPURadiator)s, %(CPURadiator_price)s, %(CPURadiator_link)s, "\ "%(SSD)s, %(SSD_price)s, %(SSD_link)s, %(HDD)s, %(HDD_price)s, %(HDD_link)s, %(powerSupply)s, %(powerSupply_price)s, "\ "%(powerSupply_link)s, %(case)s, %(case_price)s, %(case_link)s, %(totalPrice)s)"\ "ON DUPLICATE KEY UPDATE frequency=VALUES(frequency), totalPrice=VALUES(totalPrice)" cursor.execute(sql_insert, res) connection.commit() cursor.close() connection.close() # 修改 resDict,加入 Hash值 self.resDict['result'][index]['hash'] = cfg_id index += 1 return self.resDict def hashStr(self, string): seed = 3 hash_val = 0 for s in string: hash_val = hash_val * seed + ord(s) return hash_val & 0x7FFFFFFFF if __name__ == "__main__": resDict = {'type': 0, 'flag': 0, 'result': [{'CPU': ['AMD锐龙7 5800X 处理器', 'https://item.jd.com/100009085723.html', 3199], 'motherboard': ['华硕TUF GAMING X570-PLUS (WI-FI)', 'https://item.jd.com/100003815417.html', 1649], 'memory': ['G.SKILLF4-3200C14D-16GTZR', 'https://item.jd.com/4353372.html', 1149], 'SSD': ['西部数据WDS100T3XHC-00SJG0', 'https://item.jd.com/100004703834.html', 1499], 'GPU': ['七彩虹iGame GeForce RTX 3070 Ultra OC 8G', 'https://item.jd.com/100016138906.html', 4399], 'case': ['乔思伯U5', 'https://item.jd.com/8885801.html', 469], 'powerSupply': ['酷冷至尊MPY-6501-AFAAGV', 'https://item.jd.com/100002748170.html', 749], 'CPURadiator': ['华硕ROG STRIX LC 240 RGB WE', 'https://item.jd.com/100010744694.html', 1399], 'HDD': ['西部数据WD10EZEX', 'https://item.jd.com/675971.html', 278], 'totalPrice': 14790}, {'CPU': ['AMDAMD 锐龙9 3900XT 高性能版处理器', 'https://item.jd.com/100007715111.html', 3899], 'motherboard': ['华硕TUF GAMING X570-PLUS (WI-FI)', 'https://item.jd.com/100003815417.html', 1649], 'memory': ['G.SKILLF4-3200C14D-16GTZR', 'https://item.jd.com/4353372.html', 1149], 'SSD': ['西部数据WDS100T3X0C-00SJG0', 'https://item.jd.com/100002206075.html', 1259], 'GPU': ['七彩虹iGame GeForce RTX 3070 Advanced OC 8G', 'https://item.jd.com/100015756562.html', 4699], 'case': ['乔思伯U5', 'https://item.jd.com/8885801.html', 469], 'powerSupply': ['美商海盗船RMX650 2018款', 'https://item.jd.com/6544246.html', 799], 'CPURadiator': ['华硕ROG STRIX LC 240 RGB WE', 'https://item.jd.com/100010744694.html', 1399], 'HDD': ['西部数据WD1003FZEX', 'https://item.jd.com/1018295.html', 529], 'totalPrice': 15851}, {'CPU': ['AMDAMD 锐龙9 3900XT 高性能版处理器', 'https://item.jd.com/100007715111.html', 3899], 'motherboard': ['华硕PRIME X570-PRO', 'https://item.jd.com/100003815427.html', 1759], 'memory': ['G.SKILLF4-3200C14D-16GTZR', 'https://item.jd.com/4353372.html', 1149], 'SSD': ['西部数据WDS100T1R0A', 'https://item.jd.com/100010512790.html', 1599], 'GPU': ['七彩虹iGame GeForce RTX 3070 Vulcan OC 8G', 'https://item.jd.com/100008952783.html', 5199], 'case': ['乔思伯U5', 'https://item.jd.com/8885801.html', 469], 'powerSupply': ['美商海盗船RMX650 2018款', 'https://item.jd.com/6544246.html', 799], 'CPURadiator': ['华硕ROG STRIX LC 240 RGB WE', 'https://item.jd.com/100010744694.html', 1399], 'HDD': ['西部数据WD10EZEX', 'https://item.jd.com/675971.html', 278], 'totalPrice': 16550} ] } updater = UpdateTrendings(resDict) resDict = updater.update() pprint(resDict)