async def addSelfStockWithID(self, stock_id: str) -> str: dbo = DataBaseOperator() obj = dbo.searchOne(StockInformation, StockInformation.stock_id, stock_id) if obj: return "自选股已经被添加过\n" + obj.stock_name else: try: df = ts.get_realtime_quotes(stock_id) stock_name = df['name'][0].replace(' ', '') now_price = float(df['price']) flush_time = datetime.strptime( df['date'][0] + " " + df['time'][0], '%Y-%m-%d %H:%M:%S') except: return "API缺少该股信息" try: up_down_rate = 100 * (float(df['price'][0]) - float( df['pre_close'][0])) / float(df['pre_close'][0]) except: up_down_rate = 0 new_obj = StockInformation(stock_id=stock_id, stock_name=stock_name, now_price=now_price, flush_time=flush_time, up_down_rate=up_down_rate) try: dbo.add(new_obj) except: pass # 下面,添加到all_stock中 new_stock = AllStock(stock_id=stock_id, stock_name=stock_name) dbo.add(new_stock) flushOneNow(stock_id) return "自选股添加成功\n" + str(stock_name)
async def searchOneStock(self, stock_name): dbo = DataBaseOperator() stock_id = dbo.searchOne(AllStock,AllStock.stock_name,stock_name).stock_id flushOneNow(stock_id) obj = dbo.searchOne(StockInformation, StockInformation.stock_name, stock_name) if obj: return str(obj) else: return "未找到股票"
async def addSelfStock(self, stock_name: str) -> str: dbo = DataBaseOperator() obj = dbo.searchOne(StockInformation, StockInformation.stock_name, stock_name) if obj: return "自选股已经被添加过\n" + obj.stock_name else: stock = dbo.searchOne(AllStock, AllStock.stock_name, stock_name) if stock: new_obj = StockInformation(stock_id=stock.stock_id, stock_name=stock_name, now_price=-1, flush_time=datetime.now(), up_down_rate=0) dbo.add(new_obj) flushOneNow(stock.stock_id) return "自选股添加成功\n" + str(stock_name) else: return "自选股添加失败,API缺少该股信息"
async def addSelfStockWithID(self, stock_id: str) -> str: dbo = DataBaseOperator() obj = dbo.searchOne(StockInformation, StockInformation.stock_id, stock_id) if obj: return "自选股已经被添加过\n" + + obj.stock_name else: new_obj = StockInformation(stock_id=stock_id, stock_name="暂未查询", now_price=-1, flush_time=datetime.now(), up_down_rate=0) dbo.add(new_obj) stock_name = flushOneNow(stock_id) return "自选股添加成功\n" + str(stock_name)
async def searchOneStockWithID(self, stock_id): dbo = DataBaseOperator() obj = dbo.searchOne(StockInformation, StockInformation.stock_id, stock_id) if obj: if flushOneNow(stock_id): obj = dbo.searchOne(StockInformation, StockInformation.stock_id, stock_id) return str(obj) else: return "API查询失败,请稍后再次尝试" else: return "查询失败,请先添加自选股"
async def searchOneStock(self, stock_name): dbo = DataBaseOperator() try: stock_id = dbo.searchOne(AllStock, AllStock.stock_name, stock_name).stock_id except: return "查询失败,API缺少该股信息" obj = dbo.searchOne(StockInformation, StockInformation.stock_name, stock_name) if obj: if flushOneNow(stock_id): return str(obj) else: return "API查询失败,请稍后再次尝试" else: return "查询失败,请先添加自选股"