import random import pymysql from pymysql.cursors import DictCursor import json from blue_forecast import dict_sort, dict_rate from lottery_random import new_num db = { 'host': 'home.rogersun.online', 'user': 'root', 'password': 'Sxzgx1209', 'database': 'lottery' } def get_all_data(): conn = pymysql.connect(**db) curser = conn.cursor(DictCursor) curser.execute("SELECT * FROM history") _all_data = curser.fetchall() # print(all_data) return _all_data def get_all_red(): _all_data = get_all_data() _red_data = [] for data in _all_data: _red_data.append(json.loads(data['red'])) return _red_data def get_all_red_with_id(): _all_data = get_all_data() _red_data_dict = {} for data in _all_data: _red_data_dict[data['id']] = json.loads(data['red']) return _red_data_dict def get_last_red(_red_data): return _red_data[-1] def get_last_id(_all_data): return _all_data[-1]['id'] def get_all_data_rate(_all_red_data): _red = {} for _red_list in _all_red_data: for r in _red_list: if r in _red.keys(): _red[r] += 1 else: _red[r] = 1 print("历史红球的出现概率:") for k, v in dict_rate(dict_sort(_red, 'val', True)).items(): print(f"{k}: {v}") def get_red_forecast(_all_data, _red_data_dict, _red_data, range_num=1): print("红球预期数据:") _last_red = get_last_red(_red_data) _last_id = get_last_id(_all_data) _id_dict = {} for _red in _last_red: for _id, _red_list in _red_data_dict.items(): if _id == _last_id: break if _red in _red_list: if _red in _id_dict.keys(): _id_dict[_red].append(_id + 1) else: _id_dict[_red] = [_id + 1] # for k, v in _id_dict.items(): # print(f"{k}-{v}") _red_dict = {} for _red, _id_list in _id_dict.items(): # print(f"{_red}-{_id_list}") _red_temp_dict = {} for _id in _id_list: for _r in _red_data_dict[_id]: if _r in _red_temp_dict.keys(): _red_temp_dict[_r] += 1 else: _red_temp_dict[_r] = 1 _red_dict[_red] = _red_temp_dict # for k, v in _red_dict.items(): # print(f"{k}-{dict_rate(dict_sort(v,'val', True))}") _red_forecast_dict = {} for _red, _red_list in _red_dict.items(): for _r, _sum in _red_list.items(): if _r in _red_forecast_dict.keys(): _red_forecast_dict[_r] += _sum else: _red_forecast_dict[_r] = _sum _red_forecast_dict = dict_rate(dict_sort(_red_forecast_dict, 'val', True)) for k, v in _red_forecast_dict.items(): print(f"{k}-{v}") _red_forecast_list = [] for _red, __ in _red_forecast_dict.items(): _red_forecast_list.append(_red) print('高概率红球推荐:') print(', '.join(sorted(_red_forecast_list[0:6]))) # 产生随机数 red_index_list = get_random_index() _random_red = [] for index in red_index_list: _random_red.append(_red_forecast_list[index]) _random_red = sorted(_random_red) print('随机红球推荐:') print(', '.join(_random_red)) return _red_forecast_dict, _red_forecast_list, _random_red def get_last_date_id(): conn = pymysql.connect(**db) curser = conn.cursor(DictCursor) curser.execute("SELECT * FROM history ORDER BY id DESC LIMIT 1") _data = curser.fetchone() # print(all_data) return _data['dateId'] def update_red_forecast_db(): all_data = get_all_data() red_data = get_all_red() red_data_dict = get_all_red_with_id() red_forecast_dict, red_forecast_list, random_red = get_red_forecast(all_data, red_data_dict, red_data) date_id = all_data[-1]['dateId'] params = 1 red_rate = json.dumps(red_forecast_dict) conn = pymysql.connect(**db) curser = conn.cursor(DictCursor) last_date_id = get_last_date_id() curser.execute("SELECT * FROM lottery.red_forecast WHERE dateId = %s", (last_date_id,)) have_duplicate = False if len(curser.fetchall()) == 0 else True if not have_duplicate: curser.execute( "INSERT INTO lottery.red_forecast (`dateId`, `params`, `red_rate`, `red_suggest`) VALUES (%s, %s, %s, %s)", (date_id, params, red_rate, json.dumps(random_red))) conn.commit() curser.close() else: print('数据库中有重复记录,无需重复插入') def get_random_index(): # 产生随机数 random_small, random_middle, random_big = 0, 0, 0 while random_middle <= 0: random_big = random.randint(1, 3) random_small = random.randint(1, 2) random_middle = 6 - random_small - random_big # print(random_small, random_middle, random_big) small_red_index_list = [n[0] - 1 for n in new_num(11, random_small)[0:random_small]] middle_red_index_list = [n[0] - 1 + 10 for n in new_num(11, random_middle)[0:random_middle]] big_red_index_list = [n[0] - 1 + 20 for n in new_num(11, random_big)[0:random_big]] red_index_list = sorted(small_red_index_list + middle_red_index_list + big_red_index_list) # print(red_index_list) return red_index_list if __name__ == "__main__": # 实现获取含有这个数的下一期或几期中数字的概率 # 实现获取含有所以数据的总出现概率 update_red_forecast_db()