def sentiment_analyzer(): """Returns the change in value in interval of days.""" query = request.args.get('query', type=str) if query is None: return Response("{'Message': 'Send query in get query'}", status=404, mimetype='application/json') item = QueryModel.query.filter_by(query_=query).first() if item is not None: if valid_time(item.time): return json.dumps( {"predict": ast.literal_eval(item.list_predicted)}) else: db.session.delete(item) query = query.replace("%20", " ") list_predicted = {} try: news_score = predict1(query).final_pred except Exception as e: return Response("{'Message': '" + str(e) + "'}", status=404, mimetype='application/json') 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["1"] = inter1.get() list_predicted["7"] = inter7.get() list_predicted["15"] = inter15.get() list_predicted["30"] = inter30.get() if item is None: item = QueryModel(query_=query, list_predicted=str(list_predicted), news_score=news_score, time=datetime.datetime.now()) db.session.add(item) item.list_predicted = str(list_predicted) item.news_score = news_score # Try-catch to resolve integrity error try: db.session.commit() except: print("[ERROR]: Company query already exists!") return json.dumps({"predict": list_predicted})
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_predictor(self): self.assertRaises(InvalidQuery, lambda: predict1("iaenflaisuef"))
def test_empty_predictor(self): self.assertRaises(AssertionError, lambda: predict1(""))
def setUp(self): self.pred = predict1("Tesla")