Update search.py
Searchcord update: Added advanced search with from:, contains:, and has:image_true tags for precise filtering. Messages now show time sent, message ID, and are deduplicated across datasets. UI improved with fade-in animations, blue/pink theme, and image display support.
This commit is contained in:
parent
2e34e9128a
commit
ac9e02010b
1 changed files with 47 additions and 6 deletions
53
search.py
53
search.py
|
|
@ -1,5 +1,6 @@
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
|
import re
|
||||||
from flask import Flask, render_template_string, request, jsonify
|
from flask import Flask, render_template_string, request, jsonify
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
@ -186,14 +187,54 @@ def index():
|
||||||
|
|
||||||
@app.route('/search')
|
@app.route('/search')
|
||||||
def search():
|
def search():
|
||||||
query = request.args.get('query', '').lower()
|
query = request.args.get('query', '').lower().strip()
|
||||||
if not query:
|
if not query:
|
||||||
return jsonify(DATA[:200]) # default show some data
|
return jsonify(DATA[:200]) # default show some data
|
||||||
results = [
|
|
||||||
msg for msg in DATA
|
results = DATA
|
||||||
if query in msg.get('username', '').lower() or query in msg.get('message', '').lower()
|
from_user = None
|
||||||
]
|
contains_text = None
|
||||||
return jsonify(results)
|
has_image = None
|
||||||
|
|
||||||
|
# Parse from:(user)
|
||||||
|
match_from = re.search(r'from:\((.*?)\)', query)
|
||||||
|
if match_from:
|
||||||
|
from_user = match_from.group(1).lower()
|
||||||
|
query = re.sub(r'from:\(.*?\)', '', query).strip()
|
||||||
|
|
||||||
|
# Parse contains:(text)
|
||||||
|
match_contains = re.search(r'contains:\((.*?)\)', query)
|
||||||
|
if match_contains:
|
||||||
|
contains_text = match_contains.group(1).lower()
|
||||||
|
query = re.sub(r'contains:\(.*?\)', '', query).strip()
|
||||||
|
|
||||||
|
# Parse has:image_true
|
||||||
|
if 'has:image_true' in query:
|
||||||
|
has_image = True
|
||||||
|
query = query.replace('has:image_true', '').strip()
|
||||||
|
|
||||||
|
# Filter messages and remove duplicates
|
||||||
|
filtered = []
|
||||||
|
seen_ids_local = set()
|
||||||
|
for msg in results:
|
||||||
|
msg_id = msg.get('id')
|
||||||
|
if msg_id in seen_ids_local:
|
||||||
|
continue
|
||||||
|
seen_ids_local.add(msg_id)
|
||||||
|
|
||||||
|
if from_user and from_user not in msg.get('username', '').lower():
|
||||||
|
continue
|
||||||
|
if contains_text and contains_text not in msg.get('message', '').lower():
|
||||||
|
continue
|
||||||
|
if has_image and not msg.get('img'):
|
||||||
|
continue
|
||||||
|
if query and query not in msg.get('message', '').lower() and query not in msg.get('username','').lower():
|
||||||
|
continue
|
||||||
|
|
||||||
|
filtered.append(msg)
|
||||||
|
|
||||||
|
return jsonify(filtered[:200])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(debug=True)
|
app.run(debug=True)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue