CoolFace
Apppublic

abhicodes/storycircleapi

sourceHugging Faceapache-2.0updated 3y agoView on Hugging Face
0likes
main.py238 linesDownload Raw Back to root
1from email.message import EmailMessage2from flask import Flask, request, jsonify, render_template3import random4import string5from flask_cors import CORS6import smtplib7import bcrypt8import mysql.connector9 10app = Flask(__name__)11 12CORS(app)13 14mysql = mysql.connector.connect(15    host='sql12.freesqldatabase.com',16    user='sql12653124',17    password='kqM3CPBsqP',18    database='sql12653124',19    port=330620)21 22    23@app.route('/feedback', methods=['POST', 'GET'])24def feedback():25    if request.method == 'POST':26        data = request.get_json()27 28        cursor = mysql.cursor(dictionary=True)29        cursor.execute("INSERT INTO `feedbacks`(`email`, `feedback`) VALUES (%s, %s)",30                       (data.get('email'), data.get('feedback'),))31        mysql.commit()32        cursor.close()33 34        return jsonify({'success': True})35 36    if request.method == 'GET':37        cursor = mysql.cursor(dictionary=True)38        cursor.execute("SELECT * FROM `feedbacks`")39        feedback_data = cursor.fetchall()40        mysql.commit()41        cursor.close()42 43        return jsonify({'success': True, 'data': feedback_data})44 45 46@app.route('/register', methods=['POST'])47def register():48    if request.method == 'POST':49        data = request.get_json()50        cursor = mysql.cursor(dictionary=True)51        cursor.execute("SELECT * FROM users WHERE email = %s", (data.get('email'),))52        user = cursor.fetchone()53        mysql.commit()54        cursor.close()55 56        if user:57            error = 'Email address already in use. Please use a different email address.'58            return jsonify({'success': False, 'message': error})59        else:60            msg = EmailMessage()61 62            # alphabet = string.ascii_letters + string.digits63            otp = random.randint(100000, 999999)64            print(otp)65 66            cursor = mysql.cursor(dictionary=True)67            cursor.execute("INSERT INTO `otp`(`mail`, `otp`) VALUES (%s, %s)", (data.get('email'), otp,))68            mysql.commit()69            cursor.close()70 71            msg["Subject"] = "StoryCircle Verification"72            msg["From"] = "storycircle123@gmail.com"73            msg["To"] = data.get('email')74 75            html_content = render_template('email.html', name=data.get('name'), otp=otp)76            msg.set_content(html_content, subtype='html')77 78            with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:79                smtp.login('storycircle123@gmail.com', 'njoexkbwuscrwdhf')80                smtp.send_message(msg)81 82            return jsonify({'success': True})83 84 85@app.route('/verify', methods=['POST', 'GET'])86def verify():87    if request.method == 'POST':88        data = request.get_json()89        cursor = mysql.cursor(dictionary=True)90        cursor.execute("SELECT `otp` FROM `otp` WHERE `mail`=%s ORDER BY `id` DESC LIMIT 1", (data.get('email'),))91        system_otp = cursor.fetchone()92        mysql.commit()93        cursor.close()94 95        if system_otp['otp'] == data.get('otp'):96            cursor = mysql.cursor(dictionary=True)97            password = data.get('password').encode('utf-8')98            hash_password = bcrypt.hashpw(password, bcrypt.gensalt())99            cursor.execute("INSERT INTO `users` (`name`, `email`, `password`) VALUES (%s, %s, %s)",(data.get('name'), data.get('email'), hash_password,))100            mysql.commit()101            cursor.close()102            return jsonify({'success': True})103        else:104            return jsonify({'success': False})105 106 107@app.route('/login', methods=['POST', 'GET'])108def login():109    if request.method == 'POST':110        data = request.get_json()111        email = data.get('username')112        password = data.get('password').encode('utf-8')113 114        cursor = mysql.cursor(dictionary=True)115        cursor.execute("SELECT * FROM `users` WHERE email=%s", (email,))116        user = cursor.fetchone()117        mysql.commit()118        cursor.close()119 120 121        if user:122            if bcrypt.hashpw(password, user['password'].encode('utf-8')) == user['password'].encode('utf-8'):123                cursor = mysql.cursor(dictionary=True)124                cursor.execute("INSERT INTO `session`(`id`, `name`, `email`) VALUES (%s, %s, %s)", (user['id'], user['name'], user['email'],))125                mysql.commit()126                cursor.close()127 128 129                return jsonify({'login': True, 'message': 'Valid User Login', 'id': user['id'],130                        'name': user['name'], 'email': user['email']})131 132            else:133                return jsonify({'login': False, 'message': 'Invalid Password'})134        else:135            return jsonify({'login': False})136 137 138@app.route('/logincheck', methods=['POST'])139def checklogin():140    # print(session)141    data = request.get_json()142    print(data)143 144    if data.get('email') == 'Meow':145        return jsonify({'login': False})146 147    cursor = mysql.cursor(dictionary=True)148    cursor.execute("SELECT `id`, `name`, `email` FROM `session` WHERE `email`= %s ORDER BY `index` DESC LIMIT 1;", (data.get('email'),))149    user = cursor.fetchone()150    cursor.close()151 152    if user:153        return jsonify({'login': True, 'message': 'Valid User Login', 'id': user['id'],154                        'name': user['name'], 'email': user['email']})155 156    else:157        return jsonify({'login': False})158 159 160@app.route('/forgot', methods=['POST'])161def forgot():162    if request.method == 'POST':163        data = request.get_json()164        print(data)165        cursor = mysql.cursor(dictionary=True)166        cursor.execute("SELECT * FROM users WHERE email = %s", (data.get('username'),))167        user = cursor.fetchone()168        mysql.commit()169 170        if user:171            msg = EmailMessage()172 173            otp = random.randint(100000, 999999)174 175            cursor = mysql.cursor(dictionary=True)176            cursor.execute("INSERT INTO `otp`(`mail`, `otp`) VALUES (%s, %s)", (data.get('username'), otp,))177            mysql.commit()178            cursor.close()179 180            msg["Subject"] = "StoryCircle Verification"181            msg["From"] = "storycircle123@gmail.com"182            msg["To"] = data.get('username')183 184            html_content = render_template('pass.html', name=user['name'], otp=otp)185            msg.set_content(html_content, subtype='html')186 187            with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:188                smtp.login('storycircle123@gmail.com', 'njoexkbwuscrwdhf')189                smtp.send_message(msg)190 191            return jsonify({'success': True})192        else:193            error = 'No such User found. Please Register first.'194            return jsonify(error)195 196 197@app.route('/verifyforgot', methods=['POST'])198def verifyforgot():199    if request.method == 'POST':200        data = request.get_json()201        print(data)202        cursor = mysql.cursor(dictionary=True)203        cursor.execute("SELECT `otp` FROM `otp` WHERE `mail`=%s ORDER BY `id` DESC LIMIT 1;", (data.get('username'),))204        system_otp = cursor.fetchone()205        print(system_otp['otp'])206        mysql.commit()207        cursor.close()208 209        if str(system_otp['otp']) == data.get('otp'):210            return jsonify({'success': True})211        else:212            return jsonify({'success': False})213 214 215@app.route('/reset', methods=['POST'])216def reset():217    if request.method == 'POST':218        data = request.get_json()219        password = data.get('password').encode('utf-8')220        hash_password = bcrypt.hashpw(password, bcrypt.gensalt())221        cursor = mysql.cursor(dictionary=True)222        cursor.execute("UPDATE `users` SET `password`= %s WHERE `email`= %s", (hash_password, data.get('username'),))223        mysql.commit()224        cursor.close()225        return jsonify({'success': True})226 227 228@app.route('/logout', methods=['POST'])229def logout():230    data = request.get_json()231    cursor = mysql.cursor(dictionary=True)232    cursor.execute("DELETE FROM `session` WHERE `email` = %s ;", (data.get('email'),))233    mysql.commit()234    cursor.close()235    return jsonify({'logout': True})236 237if __name__ == '__main__':238    app.run()