You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
3.4 KiB
Python
107 lines
3.4 KiB
Python
from flask import Flask, request, jsonify
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
from sqlalchemy.orm import backref
|
|
from flask_cors import CORS # Import CORS from flask_cors module
|
|
from sqlalchemy import text
|
|
|
|
app = Flask(__name__)
|
|
CORS(app) # Enable CORS for your Flask app
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://IPSearch:R&>r"UhWArXy398@192.168.50.166/IPSearch'
|
|
db = SQLAlchemy(app)
|
|
|
|
class Website(db.Model):
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
url = db.Column(db.String(2048), nullable=False)
|
|
keywords = db.relationship('Keyword', backref='website', lazy=True)
|
|
|
|
class Keyword(db.Model):
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
word = db.Column(db.String(255), nullable=False)
|
|
website_id = db.Column(db.Integer, db.ForeignKey('website.id'), nullable=False)
|
|
|
|
|
|
def __repr__(self):
|
|
return '<Keyword %r>' % self.word
|
|
with app.app_context():
|
|
db.create_all()
|
|
|
|
@app.route('/add_website', methods=['POST'])
|
|
def add_website():
|
|
data = request.get_json()
|
|
url = str(data.get('url')).lower().replace(" ", "")
|
|
keywords = data.get('keywords', [])
|
|
|
|
print("Received keywords:", keywords) # Debugging output
|
|
|
|
if keywords is not None:
|
|
website = Website(url=url)
|
|
db.session.add(website)
|
|
db.session.commit()
|
|
|
|
for word in keywords:
|
|
# Convert each keyword to lowercase
|
|
keyword = Keyword(word=word.lower(), website_id=website.id)
|
|
db.session.add(keyword)
|
|
db.session.commit()
|
|
else:
|
|
return jsonify({'message': 'No keywords'}), 418
|
|
|
|
return jsonify({'message': 'Website added successfully'}), 201
|
|
|
|
|
|
@app.route('/search', methods=['POST'])
|
|
def search():
|
|
data = request.get_json()
|
|
keywords = data.get('keywords', [])
|
|
#Make the query lowercase
|
|
keywords = [x.lower() for x in keywords]
|
|
print(keywords)
|
|
|
|
# Construct the initial query with a limit of 50 results
|
|
query = "SELECT * FROM Keyword WHERE " + " OR ".join([f"word LIKE '%{kw}%'" for kw in keywords]) + " LIMIT 50"
|
|
|
|
with db.engine.connect() as connection:
|
|
# Execute the initial query
|
|
result = connection.execute(text(query))
|
|
results = result.mappings().all()
|
|
|
|
# Iterate over the results and execute the second query for each website_id
|
|
output = []
|
|
for row in results:
|
|
website_id = row['website_id']
|
|
keyword = row['word']
|
|
second_query = text('''
|
|
SELECT url
|
|
FROM Website
|
|
WHERE id = :website_id;
|
|
''')
|
|
second_result = connection.execute(second_query, {'website_id': website_id})
|
|
urls = [r['url'] for r in second_result.mappings().all()]
|
|
|
|
# Append URLs and keywords to the output
|
|
for url in urls:
|
|
output.append({'url': url, 'keywords': keyword})
|
|
|
|
# Close the connection
|
|
connection.close()
|
|
|
|
return jsonify(output)
|
|
|
|
@app.route('/random', methods=['GET'])
|
|
def random_websites():
|
|
with db.engine.connect() as connection:
|
|
# Query to select 50 random websites
|
|
result = connection.execute(text('''
|
|
SELECT url FROM Website ORDER BY RANDOM() LIMIT 50;
|
|
'''))
|
|
websites = [row[0] for row in result]
|
|
connection.close()
|
|
|
|
return jsonify(websites)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run(debug=False, host="0.0.0.0")
|