Spaces:
Sleeping
Sleeping
| from flask import Flask, request, render_template, redirect, url_for, session, flash, jsonify | |
| from scraper import search_product # Imports combined scraper functions | |
| import sqlite3 | |
| app = Flask(__name__) | |
| app.secret_key = 'your_secret_key' | |
| def init_db(): | |
| conn = sqlite3.connect('database.db') | |
| cursor = conn.cursor() | |
| # Create users table | |
| cursor.execute('''CREATE TABLE IF NOT EXISTS users ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| username TEXT NOT NULL UNIQUE, | |
| password TEXT NOT NULL | |
| )''') | |
| cursor.execute('''CREATE TABLE IF NOT EXISTS saved_items ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| username TEXT NOT NULL, | |
| product_name TEXT NOT NULL, | |
| product_img TEXT, | |
| amazon_name TEXT, | |
| amazon_price TEXT, | |
| amazon_link TEXT, | |
| one_mg_name TEXT, | |
| one_mg_price TEXT, | |
| one_mg_link TEXT, | |
| netmeds_name TEXT, | |
| netmeds_price TEXT, | |
| netmeds_link TEXT, | |
| pharmeasy_name TEXT, | |
| pharmeasy_price TEXT, | |
| pharmeasy_link TEXT, | |
| FOREIGN KEY(username) REFERENCES users(username) | |
| )''') | |
| conn.commit() | |
| conn.close() | |
| init_db() | |
| def save_item(): | |
| if 'username' not in session: | |
| return jsonify({'status': 'error', 'message': 'User not logged in'}), 401 | |
| username = session['username'] | |
| data = request.json | |
| try: | |
| conn = sqlite3.connect('database.db') | |
| cursor = conn.cursor() | |
| # Check if the item is already saved | |
| cursor.execute(''' | |
| SELECT id FROM saved_items | |
| WHERE username = ? AND product_name = ? AND amazon_link = ? | |
| ''', (username, data['name'], data.get('amazon_link'))) | |
| existing_item = cursor.fetchone() | |
| if existing_item: | |
| return jsonify({'status': 'error', 'message': 'Item already saved'}) | |
| # Insert the item if not already saved | |
| cursor.execute(''' | |
| INSERT INTO saved_items (username, product_name, product_img, amazon_name, amazon_price, amazon_link, | |
| one_mg_name, one_mg_price, one_mg_link, | |
| netmeds_name, netmeds_price, netmeds_link, | |
| pharmeasy_name, pharmeasy_price, pharmeasy_link) | |
| VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) | |
| ''', (username, data['name'], data['img'], | |
| data.get('amazon_name'), data.get('amazon_price'), data.get('amazon_link'), | |
| data.get('one_mg_name'), data.get('one_mg_price'), data.get('one_mg_link'), | |
| data.get('netmeds_name'), data.get('netmeds_price'), data.get('netmeds_link'), | |
| data.get('pharmeasy_name'), data.get('pharmeasy_price'), data.get('pharmeasy_link'))) | |
| conn.commit() | |
| except Exception as e: | |
| return jsonify({'status': 'error', 'message': str(e)}), 500 | |
| finally: | |
| conn.close() | |
| return jsonify({'status': 'success', 'message': 'Item saved successfully'}) | |
| def saved_items(): | |
| if 'username' not in session: | |
| return redirect(url_for('login')) | |
| username = session['username'] | |
| conn = sqlite3.connect('database.db') | |
| cursor = conn.cursor() | |
| cursor.execute(''' | |
| SELECT product_name, product_img, amazon_name, amazon_price, amazon_link, | |
| one_mg_name, one_mg_price, one_mg_link, | |
| netmeds_name, netmeds_price, netmeds_link, | |
| pharmeasy_name, pharmeasy_price, pharmeasy_link | |
| FROM saved_items WHERE username = ? | |
| ''', (username,)) | |
| items = cursor.fetchall() | |
| conn.close() | |
| saved_items = [ | |
| {'name': row[0], 'img': row[1], | |
| 'amazon': {'name': row[2], 'price': row[3], 'link': row[4]}, | |
| 'one_mg': {'name': row[5], 'price': row[6], 'link': row[7]}, | |
| 'netmeds': {'name': row[8], 'price': row[9], 'link': row[10]}, | |
| 'pharmeasy': {'name': row[11], 'price': row[12], 'link': row[13]}} | |
| for row in items | |
| ] | |
| return render_template('saved_items.html', saved_items=saved_items, username=username) | |
| def get_saved_items(): | |
| if 'username' not in session: | |
| return jsonify({'status': 'error', 'message': 'User not logged in'}), 401 | |
| username = session['username'] | |
| try: | |
| conn = sqlite3.connect('database.db') | |
| cursor = conn.cursor() | |
| cursor.execute('SELECT product_name FROM saved_items WHERE username = ?', (username,)) | |
| saved_items = [row[0] for row in cursor.fetchall()] # List of saved product names | |
| except Exception as e: | |
| return jsonify({'status': 'error', 'message': str(e)}), 500 | |
| finally: | |
| conn.close() | |
| return jsonify({'status': 'success', 'saved_items': saved_items}) | |
| def home(): | |
| username = session.get('username', 'Login') | |
| return render_template('index.html', username=username) | |
| def login(): | |
| if request.method == 'POST': | |
| username = request.form['username'] | |
| password = request.form['password'] | |
| conn = sqlite3.connect('database.db') | |
| cursor = conn.cursor() | |
| cursor.execute('SELECT * FROM users WHERE username = ? AND password = ?', (username, password)) | |
| user = cursor.fetchone() | |
| conn.close() | |
| if user: | |
| session['username'] = username | |
| flash('Login successful!', 'success') | |
| return redirect(url_for('home')) | |
| else: | |
| flash('Invalid username or password', 'danger') | |
| return render_template('login.html') | |
| # Route for signup | |
| def signup(): | |
| if request.method == 'POST': | |
| username = request.form.get('username') | |
| password = request.form.get('password') | |
| confirm_password = request.form.get('password2') | |
| # Check if passwords match | |
| if password != confirm_password: | |
| flash('Confirm Password do not match!', 'error') | |
| return redirect(url_for('signup')) | |
| conn = sqlite3.connect('database.db') | |
| cursor = conn.cursor() | |
| try: | |
| cursor.execute('INSERT INTO users (username, password) VALUES (?, ?)', (username, password)) | |
| conn.commit() | |
| flash('Signup successful! Please log in.', 'success') | |
| return redirect(url_for('login')) | |
| except sqlite3.IntegrityError: | |
| flash('Username already exists. Please choose a different one.', 'danger') | |
| finally: | |
| conn.close() | |
| return render_template('signup.html') | |
| def result(): | |
| if 'username' not in session: | |
| return redirect(url_for('login')) | |
| username = session['username'] | |
| try: | |
| conn = sqlite3.connect('database.db') | |
| cursor = conn.cursor() | |
| # Fetch all saved product names for the current user | |
| cursor.execute('SELECT product_name FROM saved_items WHERE username = ?', (username,)) | |
| saved_items = {row[0] for row in cursor.fetchall()} # Use a set for quick lookups | |
| except Exception as e: | |
| flash(f"Error fetching saved items: {e}", 'danger') | |
| saved_items = set() | |
| finally: | |
| conn.close() | |
| # Replace `products` with your logic for fetching the product list | |
| products = [ | |
| # Example product structure | |
| {'name': 'Product 1', 'img': 'image_url', 'amazon': {'name': 'Amazon Prod', 'price': '100'}}, | |
| {'name': 'Product 2', 'img': 'image_url', 'amazon': {'name': 'Amazon Prod 2', 'price': '200'}}, | |
| ] | |
| return render_template('results.html', products=products, saved_items=saved_items) | |
| def logout(): | |
| session.pop('username', None) | |
| flash('You have been logged out.', 'info') | |
| return redirect(url_for('home')) | |
| def search(): | |
| query = request.form['query'] | |
| results = search_product(query) | |
| username = session.get('username', None) # Use None if not logged in | |
| # Fetch side effects using your JavaScript logic (moved here for backend processing) | |
| import requests | |
| api_url = f'https://api.fda.gov/drug/event.json?api_key=ah8n0nI6468qpS6cMfqvBM0sdGs4nO1sT3Ie7LjJ&search=patient.drug.medicinalproduct:"{query}"' | |
| try: | |
| response = requests.get(api_url) | |
| data = response.json() | |
| if data.get('results'): | |
| side_effects = [ | |
| reaction.get('reactionmeddrapt', 'N/A') | |
| for reaction in data['results'][0].get('patient', {}).get('reaction', []) | |
| ] | |
| else: | |
| side_effects = ["No side effects found."] | |
| except Exception as e: | |
| print(f"Error fetching data: {e}") | |
| side_effects = ["Error fetching data. Please try again."] | |
| return render_template('results.html', products=results, query=query, username=username,side_effects=side_effects) | |
| if __name__ == '__main__': | |
| app.run(host='0.0.0.0', port=7860, debug=True) | |