def graph(): query = request.args.get('query', type=str) if query is None: return Response("{'Message': 'Send query in get query'}", status=404, mimetype='application/json') query = query.replace("%20", " ") try: list_predicted = ast.literal_eval( QueryModel.query.filter_by(query_=query).first().list_predicted) except Exception as e: return Response("{'Message': 'Invalid query: " + str(e) + "'}", status=404, mimetype='application/json') graph_pre = [ list_predicted["1"], list_predicted["7"], list_predicted["15"], list_predicted["30"] ] try: return stock_graph(query, graph_pre).graph() except Exception as e: print(e) return Response("{'message': '" + str(e) + "'}", status=404)
async def websocket_handler(ws: WebSocket): await ws.accept() while True: query = await ws.receive_text() if query is None or query == "": await ws.send_json({"message": "Invalid query"}) await ws.close(code=1003) break try: news = scraper(query) news_score = predict1(query, news.results).final_pred except InvalidQuery as e: await ws.send_json({"message": str(e)}) await ws.close(code=1003) break pool = ThreadPool(processes=4) inter1 = pool.apply_async(pre, (1, news_score)) inter7 = pool.apply_async(pre, (7, news_score)) inter15 = pool.apply_async(pre, (15, news_score)) inter30 = pool.apply_async(pre, (30, news_score)) list_predicted = {} list_predicted[1] = inter1.get() list_predicted[7] = inter7.get() list_predicted[15] = inter15.get() list_predicted[30] = inter30.get() try: stg = stock_graph(query, [list_predicted[i] for i in [1, 7, 15, 30]]) except InvalidTicker as e: await ws.send_json({"message": str(e)}) await ws.close(code=1003) break responseObj = { "predictedPrice": { 1: stg.predict_price_value.iloc[1]["price"], 7: stg.predict_price_value.iloc[2]["price"], 15: stg.predict_price_value.iloc[3]["price"], 30: stg.predict_price_value.iloc[4]["price"], }, "graph": stg.graphSocket(), "summary": Summarize(news.get_title(), 3).generate_summary() } await ws.send_json(responseObj) await ws.close(code=1000) break
def test_invalid_graph(self): self.assertRaises(InvalidTicker, lambda: stock_graph("guocoland", [0.08733508525853388, 0.41467300402115265, 0.8421720201029591, 2.1635118971243883]))
def test_empty_graph(self): self.assertRaises(AssertionError, lambda: stock_graph("", []))
def setUp(self): self.sg = stock_graph("Tesla", [0.08733508525853388, 0.41467300402115265, 0.8421720201029591, 2.1635118971243883])