Exemplo n.º 1
0
def make_prediction(id: int):
    """
    This is what make the predictions
    """
    db = SessionLocal()
    sms_table = db.query(FraudSMS).filter(FraudSMS.id == id).first()
    model_path = "./lib/xgb_model.pkl"
    xgb_model = joblib.load(model_path)

    try:
        raw = sms_table.sms_text
        processor = custom_processor.InputTransformer()
        data = processor.transform(raw)
        print(data)
    except ValueError:
        print("Request has no data or is not a valid json object")

    predictions = xgb_model.predict(data)
    probability = xgb_model.predict_proba(data)
    results = dict()

    sms_table.probability = list(probability)[0][1]
    # sms_table.prediction = list(predictions)[0]

    db.add(sms_table)
    db.commit()
Exemplo n.º 2
0
def fetch_stock_data(id: int):
    # Create a new db session in the background
    db = SessionLocal()
    # Query the db for the matching stock record using this id
    stock = db.query(Stock).filter(Stock.id == id).first()

    # Test that our function even works before fetching from YF
    # by manually updating 'forward_pe' field
    # stock.forward_pe = 10

    # Get data from YF and map to our table columns
    yahoo_data = yfinance.Ticker(stock.symbol)

    stock.ma200 = yahoo_data.info["twoHundredDayAverage"]
    stock.ma50 = yahoo_data.info["fiftyDayAverage"]
    stock.price = yahoo_data.info["previousClose"]
    stock.forward_pe = yahoo_data.info["forwardPE"]
    stock.forward_eps = yahoo_data.info["forwardEps"]
    # Some stocks don't give dividend_yield, so add conditional
    if yahoo_data.info["dividendYield"] is not None:
        stock.dividend_yield = yahoo_data.info["dividendYield"] * 100

    # Update the record
    db.add(stock)
    db.commit()
Exemplo n.º 3
0
def hw_add_aeroporto(request):
    session = SessionLocal()
    aeroporto = Aeroporto(nome=request["nome"], cidade=request["cidade"])
    # aeroporto_json = populate_aeroporto([aeroporto])
    session.add(aeroporto)
    session.commit()
    aeroporto_json = hw_get_aeroporto(aeroporto.id)
    session.close()
    return aeroporto_json
def fetch_the_data(id: int):
    db = SessionLocal()

    house = db.query(house_price).filter(house_price.id == id)

    
    house.MedInc = house['MedInc']
    db.add(house)
    db.commit()
Exemplo n.º 5
0
def create_favorite_item(item_id: None, owner: None):
    attributes = oac.search_by_imdb_id(item_id, sanitize=True)
    db_favorite_item = models.FavoriteMovie(id=str(uuid.uuid1()),
                                            imdb_id=item_id,
                                            attributes=attributes,
                                            owner_id=owner.id)
    db = SessionLocal()
    db.add(db_favorite_item)
    db.commit()
    db.refresh(db_favorite_item)
    db.close()
    return db_favorite_item
Exemplo n.º 6
0
def hw_update_aeroporto(request):
    session = SessionLocal()
    aeroporto_old = session.query(Aeroporto).filter_by(
        id=request["id"]).first()
    session.delete(aeroporto_old)
    aeroporto = Aeroporto(id=request["id"],
                          nome=request["nome"],
                          cidade=request["cidade"])
    session.add(aeroporto)
    session.commit()
    aeroporto_json = hw_get_aeroporto(aeroporto.id)
    session.close()
    return aeroporto_json
Exemplo n.º 7
0
def yahoo_data_fetch(id: int):
    db = SessionLocal()
    stock_data = db.query(StockData).filter(StockData.id == id).first()
    yahoo_stock_data = yf.Ticker(stock_data.stockname)
    stock_data.ma200 = yahoo_stock_data.info["twoHundredDayAverage"]
    stock_data.ma50 = yahoo_stock_data.info["fiftyDayAverage"]
    stock_data.price = yahoo_stock_data.info["previousClose"]
    stock_data.forward_pe = yahoo_stock_data.info["forwardPE"]
    stock_data.forward_eps = yahoo_stock_data.info["forwardEps"]
    if stock_data.dividend_yield is not None:
        stock_data.dividend_yield = yahoo_stock_data.info["dividendYield"] * 100
    db.add(stock_data)
    db.commit()
Exemplo n.º 8
0
def fetch_stock_data(id: int):
    db = SessionLocal()
    stock = db.query(Stock).filter(Stock.id == id).first()
    yahoo_data = yf.Ticker(stock.symbol)

    stock.ma200 = yahoo_data.info['twoHundredDayAverage']
    stock.ma50 = yahoo_data.info['fiftyDayAverage']
    stock.price = yahoo_data.info['previousClose']
    stock.forward_pe = yahoo_data.info['forwardPE']
    stock.forward_eps = yahoo_data.info['forwardEps']
    stock.dividend_yield = (yahoo_data.info['dividendYield'] or 1) * 100
    db.add(stock)
    db.commit()
Exemplo n.º 9
0
def hw_update_reserva(request):
    session = SessionLocal()
    reserva_old = session.query(Reserva).filter_by(id=request["id"]).first()
    session.delete(reserva_old)
    reserva = Reserva(
        id_voo=request["id_voo"],
        id_cadastro=request["id_cadastro"],
        e_ticket=request["e_ticket"],
    )
    session.add(reserva)
    session.commit()
    reserva_json = hw_get_reserva(reserva.id)
    session.close()
    return reserva_json
Exemplo n.º 10
0
def hw_add_reserva(request):
    session = SessionLocal()
    reserva = Reserva(id_voo=request["id_voo"],
                      id_cadastro=request["id_cadastro"])
    session.add(reserva)
    session.commit()
    hash_obj = hashlib.md5(f"{reserva.id}".encode())
    md5_value = hash_obj.hexdigest()
    reserva.e_ticket = md5_value
    session.add(reserva)
    session.commit()
    reserva_json = hw_get_reserva(reserva.id)
    session.close()
    return reserva_json
Exemplo n.º 11
0
def populate_data():
    db = SessionLocal()
    countries = covid.getLocations(rank_by='deaths')
    for country in countries:
        db_record = models.State(id=country['id'],
                                 country=country['country'],
                                 country_code=country['country_code'],
                                 population=country['country_population'],
                                 last_updated=country['last_updated'],
                                 confirmed=((country['latest'])['confirmed']),
                                 deaths=((country['latest'])['deaths']),
                                 recovered=((country['latest'])['recovered']))
        db.add(db_record)
    db.commit()
Exemplo n.º 12
0
def fetch_stock_data(id: int):
    db = SessionLocal()
    stock = db.query(Stock).filter(Stock.id == id).first()
    yahoo_data = yf.Ticker(stock.symbol)
    stock.ma200 = yahoo_data.info["twoHundredDayAverage"]
    stock.ma50 = yahoo_data.info["fiftyDayAverage"]
    stock.price = yahoo_data.info["previousClose"]
    stock.forward_pe = yahoo_data.info["forwardPE"]
    stock.forward_eps = yahoo_data.info["forwardEps"]
    if yahoo_data.info["dividendYield"]:
        stock.dividend_yield = yahoo_data.info["dividendYield"] * 100

    db.add(stock)
    db.commit()
Exemplo n.º 13
0
def fetch_stock_data(_id: int):
    db = SessionLocal()
    stock: Stock = db.query(Stock).filter(Stock.id == _id).first()

    yfinance_data = yf.Ticker(stock.symbol)
    if yfinance_data.info['dividendYield'] is not None:
        stock.dividend_yield = yfinance_data.info['dividendYield'] * 100

    stock.forward_eps = yfinance_data.info['forwardEps']
    stock.forward_pe = yfinance_data.info['forwardPE']
    stock.price = yfinance_data.info['previousClose']
    stock.ma50 = yfinance_data.info['fiftyDayAverage']
    stock.ma200 = yfinance_data.info['twoHundredDayAverage']

    db.add(stock)
    db.commit()
Exemplo n.º 14
0
def hw_add_voo(request):
    session = SessionLocal()
    voo = Voo(
        data=datetime.strptime(request["data"], "%d/%m/%Y %H:%M:%S"),
        destino=request["destino"],
        companhia=request["companhia"],
        capacidade=request["capacidade"],
        ocupacao=request["ocupacao"],
        preco=request["preco"],
        id_aeroporto=request["id_aeroporto"],
    )
    session.add(voo)
    session.commit()
    voo_json = hw_get_voo(voo.id)
    session.close()
    return voo_json
Exemplo n.º 15
0
def fetch_stock_data(id: int):
    db = SessionLocal()
    stock = db.query(Stock).filter(Stock.id == id).first()

    yahoo_data = yfinance.Ticker(stock.symbol)
    stock.ma200 = yahoo_data.info['twoHundredDayAverage']
    stock.ma50 = yahoo_data.info['fiftyDayAverage']
    stock.price = yahoo_data.info['previousClose']
    stock.forward_pe = yahoo_data.info['forwardPE']
    stock.forward_eps = yahoo_data.info['forwardEps']
    dividend = yahoo_data.info['dividendYield']
    stock.dividend_yield = 0 if dividend == None else dividend * 100

    db.add(stock)
    db.commit()
    print('    Data fetched from Yahoo!Finance and saved for', stock.symbol)
Exemplo n.º 16
0
def fetch_stock_data(id: int):
    """
        hold the id and after getting data insert it to database
        async in background task
    """
    db = SessionLocal()
    stock = db.query(Stock).filter(Stock.id == id).first()

    yahoo_data = yf.Ticker(stock.symbol)

    stock.ma200 = yahoo_data.info['twoHundredDayAverage']
    stock.ma50 = yahoo_data.info['fiftyDayAverage']
    stock.price = yahoo_data.info['previousClose']
    stock.forward_pe = yahoo_data.info['forwardPE']
    stock.forward_EPS = yahoo_data.info['forwardEps']

    db.add(stock)
    db.commit()
Exemplo n.º 17
0
def hw_update_voo(request):
    session = SessionLocal()
    voo_old = session.query(Voo).filter_by(id=request["id"]).first()
    session.delete(voo_old)
    voo = Voo(
        id=request["id"],
        data=datetime.strptime(request["data"], "%d/%m/%Y %H:%M:%S"),
        destino=request["destino"],
        companhia=request["companhia"],
        capacidade=request["capacidade"],
        ocupacao=request["ocupacao"],
        preco=request["preco"],
    )
    session.add(voo)
    session.commit()
    voo_json = hw_get_voo(voo.id)
    session.close()
    return voo_json
Exemplo n.º 18
0
def write_video_comments_sql(isVideoExists, video_info, comments_json_array):
    """
    将评论数据写入数据库
    """
    if isVideoExists:
        result = delete_video_by_oid(video_info['oid'])
        if not result:
            return False

    try:
        session = SessionLocal()

        # 写入bilibiliVideos
        video_record = BilibiliVideo(oid=video_info['oid'],
                                     url=video_info['url'],
                                     type=video_info['type'],
                                     title=video_info['title'])

        session.add(video_record)
        session.flush()

        # 写入bilibiliVideoComments
        vid = video_record.id
        commentRecords = []

        for page_json in comments_json_array:
            page_comment = json.loads(page_json)
            top_level_replies = page_comment['data']['replies']
            for reply in top_level_replies:
                # 去除表情符号
                text = re.sub(r'\[\S+\]', '', reply['content']['message'])
                comment = BilibiliVideoComment(vid=vid, text=text)
                commentRecords.append(comment)

        session.add_all(commentRecords)

    except Exception as e:
        print(e)
        session.close()
        return False
    else:
        session.commit()
        return True
Exemplo n.º 19
0
async def create_stock(stock_request: StockRequest,
                       background_tasks: BackgroundTasks,
                       db: SessionLocal = Depends(get_db)):
    """
    Create new stock entry and store it in database.
    Run background task in queue to update db entry
    with fetched stock details
    :return:
    """
    # Instantiate model with input data
    stock = Stock()
    stock.symbol = stock_request.symbol
    # Save to db
    db.add(stock)
    db.commit()

    background_tasks.add_task(fetch_stock_data, stock.id)

    return {"code": "success", "message": "Stock was added to database"}
def fetch_stock_data(stock_id: int):
    db = SessionLocal()
    stock = db.query(Stock).filter(Stock.id == stock_id).first()

    yahoo_data = yf.Ticker(stock.symbol)
    print(yahoo_data.info)
    stock.name = yahoo_data.info['longName']
    stock.ma200 = yahoo_data.info['twoHundredDayAverage']
    stock.ma50 = yahoo_data.info['fiftyDayAverage']
    stock.price = yahoo_data.info['previousClose']
    stock.forward_pe = yahoo_data.info['forwardPE']
    stock.forward_eps = yahoo_data.info['forwardEps']
    if yahoo_data.info['dividendYield'] is not None:
        stock.dividend = yahoo_data.info['dividendYield'] * 100
    else:
        stock.dividend = 0

    db.add(stock)
    db.commit()
Exemplo n.º 21
0
def fetch_stock_data(id: int):
    """
    Import stocks data from yfinance API
    """
    db = SessionLocal()
    stock = db.query(StockItem).filter(StockItem.id == id).first()

    yahoo_data = yf.Ticker(stock.ticker)

    stock.ma200 = yahoo_data.info['twoHundredDayAverage']
    stock.ma50 = yahoo_data.info['fiftyDayAverage']
    stock.price = yahoo_data.info['previousClose']
    stock.shortname = yahoo_data.info['shortName']
    try:
        stock.forwardpe = yahoo_data.info['forwardPE']
    except:
        pass

    db.add(stock)
    db.commit()
Exemplo n.º 22
0
def fetch_stock_data(id: int):
    """
    Fetch the stock information from symbol given.
    """
    db = SessionLocal()

    stock = db.query(Stock).filter(Stock.id == id).first()

    yahoo_data = yfinance.Ticker(stock.symbol)

    stock.ma200 = yahoo_data.info['twoHundredDayAverage']
    stock.ma50 = yahoo_data.info['fiftyDayAverage']
    stock.price = yahoo_data.info['previousClose']
    stock.forward_pe = yahoo_data.info['forwardPE']
    stock.forward_eps = yahoo_data.info['forwardEps']

    if yahoo_data.info['dividendYield'] is not None:
        stock.dividend_yield = yahoo_data.info['dividendYield'] * 100

    db.add(stock)
    db.commit()
Exemplo n.º 23
0
def fetch_data(id: str):
    db = SessionLocal()

    country = db.query(State).filter(State.id == id).first()

    state_data = covid.get_status_by_country_name(country.state)

    country.id = state_data['id']

    country.state = state_data['country'].capitalize()

    country.confirmed = state_data['confirmed']

    country.active = state_data['active']

    country.deaths = state_data['deaths']

    country.recovered = state_data['recovered']

    db.add(country)
    db.commit()
Exemplo n.º 24
0
def fetch_real_time():

    db = SessionLocal()

    global collect
    while (collect):

        all_pairs_scraper = CurPairScraper(
            "https://finance.yahoo.com/currencies")

        for pair_info in all_pairs_scraper.obj_genrator():
            print("{} of {} : {}".format(pair_info["row_num"],
                                         pair_info["total_rows"],
                                         pair_info["name"]))
            curPair = models.CurPairs()
            curPair.cur_pair = pair_info["name"]
            curPair.price = pair_info["price"]
            curPair.change = pair_info["change"]
            curPair.per_change = pair_info["per_change"]
            db.add(curPair)
        db.commit()
Exemplo n.º 25
0
def fetch_stock_data(pk: int):
    db = SessionLocal()  #new session create
    stock = db.query(Stock).filter(Stock.id == pk).first(
    )  # find stock using pk id. Note `Stock` used not `stock`

    # yf.Ticker returns dict of lots of key-vakue pairs. we take only necessary.
    # check pypi docs for yf

    yahoo_data = yf.Ticker(stock.symbol)

    stock.ma200 = yahoo_data.info['twoHundredDayAverage']
    stock.ma50 = yahoo_data.info['fiftyDayAverage']
    stock.price = yahoo_data.info['previousClose']
    stock.forward_pe = yahoo_data.info['forwardPE']
    stock.forward_eps = yahoo_data.info['forwardEps']
    if yahoo_data.info['dividendYield'] is not None:
        stock.dividend_yield = yahoo_data.info['dividendYield'] * 100

    #save
    db.add(stock)
    db.commit()
Exemplo n.º 26
0
def fetch_stock_data(id: int):
    # New DB session
    db = SessionLocal()

    # Get first queried instance
    stock = db.query(Stock).filter(Stock.id == id).first()

    # Set attribute manually for testing if background task is running or not
    # stock.forward_pe = 10

    yahoo_data = yfinance.Ticker(stock.symbol)

    stock.ma200 = yahoo_data.info["twoHundredDayAverage"]
    stock.ma50 = yahoo_data.info["fiftyDayAverage"]
    stock.price = yahoo_data.info["previousClose"]
    stock.forward_pe = yahoo_data.info["forwardPE"]
    stock.forward_eps = yahoo_data.info["forwardEps"]

    if yahoo_data.info["dividendYield"] is not None:
        stock.dividend_yield = yahoo_data.info["dividendYield"] * 100

    db.add(stock)
    db.commit()
Exemplo n.º 27
0
def webhook():

    db_user = SessionLocal()

    data = request.get_json(silent=True)
    query = data['queryResult']['queryText']
    global user_utter

    # Set global variable that wizard listens to for query
    user_utter = query

    # Add new query to db
    new_user_utr = models.UserQuery(user_request=query)
    db_user.add(new_user_utr)
    db_user.commit()

    time.sleep(3.4)

    wiz_db_resp = db_user.query(models.UserQuery).order_by(models.UserQuery.id.desc()).first().wizard_response
    db_user.close()
    if(wiz_db_resp== "No Response"):
        return event('attempt-1')
    else:
        return ask(str(wiz_db_resp))
Exemplo n.º 28
0
 
logging.basicConfig(level=logging.INFO)
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)

models.Base.metadata.create_all(bind=engine)

app = FastAPI()

def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()


@app.get('/one', response_model=schemas.User)
def read_root(db: Session = Depends(get_db)):
    user = crud.get_user(db)
    return user

if __name__ == '__main__':
    db = SessionLocal()
    if db.query(models.User).count() == 0:
        user = models.User(name='test_user')
        item = models.Item(title='test_item')
        user.items = [item]
        db.add(user)
        db.commit()
    db.close()
    uvicorn.run(app, host="0.0.0.0", port=8000)
Exemplo n.º 29
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jan  9 13:59:28 2021

@author: joachim
"""


import glob
import models
from database import SessionLocal, engine

models.Base.metadata.create_all(bind=engine)

db = SessionLocal() #this might be stupid. Should i load i each time like in fastapi? Idk. databases are hard to get right

for file in glob.glob('./static/*.jpg'):
    picture = models.Picture(elo = 800, image_path = file, )
    db.add(picture)

db.commit()
Exemplo n.º 30
0
import csv
import datetime
import models.item_model
from database import SessionLocal, engine

db = SessionLocal()

models.item_model.Base.metadata.create_all(bind=engine)

with open("Item.csv", "r") as f:
    csv_reader = csv.DictReader(f)

    for row in csv_reader:
        db_record = models.item_model.Item(
            id=row["id"],
            name=row["name"],
            location=row["location"],
            description=row["description"],
            date=datetime.datetime.strptime(row["date"], "%Y-%m-%d"),
            #pic=row["pic"],
        )
        db.add(db_record)

    db.commit()

db.close()