|
|
from flask import Flask, render_template, request, redirect, url_for, session |
|
|
from flask_socketio import SocketIO, send, emit |
|
|
import os |
|
|
import re |
|
|
from nltk.tokenize import word_tokenize |
|
|
from nltk.corpus import stopwords |
|
|
import nltk |
|
|
|
|
|
|
|
|
nltk_data_dir = "/tmp/nltk_data" |
|
|
os.makedirs(nltk_data_dir, exist_ok=True) |
|
|
nltk.data.path.append(nltk_data_dir) |
|
|
|
|
|
nltk.download('punkt', download_dir=nltk_data_dir) |
|
|
nltk.download('stopwords', download_dir=nltk_data_dir) |
|
|
|
|
|
app = Flask(__name__) |
|
|
app.secret_key = 'your_secret_key_here' |
|
|
socketio = SocketIO(app,cors_allowed_origins="*") |
|
|
|
|
|
|
|
|
MESSAGE_FILE = 'chat_messages.txt' |
|
|
|
|
|
|
|
|
if not os.path.exists(MESSAGE_FILE): |
|
|
with open(MESSAGE_FILE, 'w') as f: |
|
|
f.write('') |
|
|
|
|
|
|
|
|
USERS = { |
|
|
'ali': '1234', |
|
|
'zain': '1234', |
|
|
'fahad': '1234' |
|
|
} |
|
|
|
|
|
@app.route('/', methods=['GET']) |
|
|
def home(): |
|
|
|
|
|
if 'username' not in session: |
|
|
return redirect(url_for('login')) |
|
|
return redirect(url_for('chat')) |
|
|
|
|
|
@app.route('/login', methods=['GET', 'POST']) |
|
|
def login(): |
|
|
if request.method == 'POST': |
|
|
username = request.form['username'] |
|
|
password = request.form['password'] |
|
|
|
|
|
|
|
|
if username in USERS and USERS[username] == password: |
|
|
session['username'] = username |
|
|
return redirect(url_for('chat')) |
|
|
else: |
|
|
return "Invalid username or password. <a href='/login'>Try again</a>" |
|
|
|
|
|
return render_template('login.html') |
|
|
|
|
|
@app.route('/chat', methods=['GET']) |
|
|
def chat(): |
|
|
|
|
|
if 'username' not in session: |
|
|
return redirect(url_for('login')) |
|
|
|
|
|
|
|
|
with open(MESSAGE_FILE, 'r') as f: |
|
|
messages = f.readlines() |
|
|
|
|
|
return render_template('chat.html', messages=messages) |
|
|
|
|
|
@app.route('/logout') |
|
|
def logout(): |
|
|
session.pop('username', None) |
|
|
return redirect(url_for('login')) |
|
|
|
|
|
@app.route('/matchmaking') |
|
|
def matchmaking(): |
|
|
|
|
|
if 'username' not in session: |
|
|
return redirect(url_for('login')) |
|
|
|
|
|
|
|
|
matches = check_for_matches() |
|
|
return render_template('matchmaking.html', matches=matches) |
|
|
|
|
|
|
|
|
@socketio.on('message') |
|
|
def handle_message(data): |
|
|
username = session.get('username') |
|
|
message = data['message'] |
|
|
|
|
|
|
|
|
with open(MESSAGE_FILE, 'a') as f: |
|
|
f.write(f'{username}: {message}\n') |
|
|
|
|
|
|
|
|
emit('message', {'username': username, 'message': message}, broadcast=True) |
|
|
|
|
|
|
|
|
check_for_matches() |
|
|
|
|
|
def check_for_matches(): |
|
|
|
|
|
with open(MESSAGE_FILE, 'r') as f: |
|
|
messages = f.readlines() |
|
|
|
|
|
|
|
|
buy_requests = [] |
|
|
sell_requests = [] |
|
|
|
|
|
|
|
|
for message in messages: |
|
|
username, text = message.split(':', 1) |
|
|
text = text.strip().lower() |
|
|
|
|
|
|
|
|
tokens = word_tokenize(text) |
|
|
tokens = [word for word in tokens if word.isalnum() and word not in stopwords.words('english')] |
|
|
|
|
|
|
|
|
if 'buy' in tokens: |
|
|
product = extract_product_name(tokens) |
|
|
if product: |
|
|
buy_requests.append({'username': username.strip(), 'product': product}) |
|
|
elif 'sell' in tokens: |
|
|
product = extract_product_name(tokens) |
|
|
if product: |
|
|
sell_requests.append({'username': username.strip(), 'product': product}) |
|
|
|
|
|
|
|
|
matches = [] |
|
|
for buy in buy_requests: |
|
|
for sell in sell_requests: |
|
|
if buy['product'] == sell['product']: |
|
|
matches.append({ |
|
|
'buyer': buy['username'], |
|
|
'seller': sell['username'], |
|
|
'product': buy['product'] |
|
|
}) |
|
|
|
|
|
remove_matched_messages(buy['username'], sell['username'], buy['product']) |
|
|
|
|
|
|
|
|
if matches: |
|
|
for match in matches: |
|
|
emit('match_found', { |
|
|
'buyer': match['buyer'], |
|
|
'seller': match['seller'], |
|
|
'product': match['product'] |
|
|
}, broadcast=True) |
|
|
|
|
|
def extract_product_name(tokens): |
|
|
|
|
|
for i, word in enumerate(tokens): |
|
|
if word in ['buy', 'sell'] and i + 1 < len(tokens): |
|
|
return tokens[i + 1] |
|
|
return None |
|
|
|
|
|
def remove_matched_messages(buyer, seller, product): |
|
|
|
|
|
with open(MESSAGE_FILE, 'r') as f: |
|
|
messages = f.readlines() |
|
|
|
|
|
|
|
|
new_messages = [] |
|
|
for message in messages: |
|
|
username, text = message.split(':', 1) |
|
|
text = text.strip().lower() |
|
|
if not (username.strip() == buyer and f'buy {product}' in text) and \ |
|
|
not (username.strip() == seller and f'sell {product}' in text): |
|
|
new_messages.append(message) |
|
|
|
|
|
|
|
|
with open(MESSAGE_FILE, 'w') as f: |
|
|
f.writelines(new_messages) |
|
|
|
|
|
if __name__ == '__main__': |
|
|
socketio.run(app, debug=True, host='0.0.0.0') |