from flask import Flask,jsonify
from textblob import TextBlob
import pandas as pd
import mysql.connector
from flask_cors import CORS


app = Flask(__name__)
CORS(app)

@app.route('/sentimental', methods=['GET'])
def get():
    mydb = mysql.connector.connect(
    host="localhost",
    user="root",
    password="root",
    database="inteljr"
    )

    mycursor = mydb.cursor()

    mycursor.execute("SELECT * FROM conversations")
    myresult = mycursor.fetchall()
    df=pd.DataFrame(myresult)
    for index, row in df.iterrows():
        edu=TextBlob(row[4])
        x=edu.sentiment.polarity
        if x<0:
            sql = '''UPDATE conversations set sentimental = "%s" WHERE id="%s" '''
            val = ("NEGATIVE",row[0])
            mycursor.execute(sql, val)
        elif x==0:
            sql = '''UPDATE conversations set sentimental = "%s" WHERE id="%s" '''
            val = ("NEUTRAL",row[0])
            mycursor.execute(sql, val)
        elif x>0 and x<=1:
            sql = '''UPDATE conversations set sentimental = "%s" WHERE id="%s" '''
            val = ("POSITIVE",row[0])
            mycursor.execute(sql, val)
        
    mydb.commit()
    return "Task is done"

if __name__  == "__main__":
    app.run(debug=True)
