From 4f789dfa7af88b74bbaad25e67634908ef167b15 Mon Sep 17 00:00:00 2001 From: finnius Date: Fri, 13 Jun 2025 01:50:07 +0000 Subject: [PATCH 1/2] Add 'kid' and 'pssh' search operators to /api/cache/search endpoint sqlite --- .../database/cache_to_db_sqlite.py | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/custom_functions/database/cache_to_db_sqlite.py b/custom_functions/database/cache_to_db_sqlite.py index 806e20b..1d327c0 100644 --- a/custom_functions/database/cache_to_db_sqlite.py +++ b/custom_functions/database/cache_to_db_sqlite.py @@ -43,21 +43,28 @@ def search_by_pssh_or_kid(search_filter): # Initialize a set to store unique matching records results = set() - # Search for records where PSSH contains the search_filter - cursor.execute(''' - SELECT * FROM licenses WHERE PSSH LIKE ? - ''', ('%' + search_filter + '%',)) - rows = cursor.fetchall() - for row in rows: - results.add((row[1], row[2], row[3])) # (PSSH, KID, Key) + if search_filter.lower().startswith("kid:"): + value = search_filter[4:] - # Search for records where KID contains the search_filter - cursor.execute(''' - SELECT * FROM licenses WHERE KID LIKE ? - ''', ('%' + search_filter + '%',)) - rows = cursor.fetchall() - for row in rows: - results.add((row[1], row[2], row[3])) # (PSSH, KID, Key) + # Search for records where KID matches the operator value + cursor.execute('SELECT PSSH, KID, `Key` FROM licenses WHERE KID = ?', (value,)) + results.update(cursor.fetchall()) + elif search_filter.lower().startswith("pssh:"): + value = search_filter[5:] + + # Search for records where PSSH contains the operator + cursor.execute('SELECT PSSH, KID, `Key` FROM licenses WHERE PSSH LIKE ?', (f"%{value}%",)) + results.update(cursor.fetchall()) + else: + like_filter = f"%{search_filter}%" + + # Search for records where PSSH contains the search_filter + cursor.execute('SELECT PSSH, KID, `Key` FROM licenses WHERE PSSH LIKE ?', (like_filter,)) + results.update(cursor.fetchall()) + + # Search for records where KID contains the search_filter + cursor.execute('SELECT PSSH, KID, `Key` FROM licenses WHERE KID LIKE ?', (like_filter,)) + results.update(cursor.fetchall()) # Convert the set of results to a list of dictionaries for output final_results = [{'PSSH': result[0], 'KID': result[1], 'Key': result[2]} for result in results] -- 2.43.0 From 2dbedd72065e929a5f0bcc944ea4840b9c735dfb Mon Sep 17 00:00:00 2001 From: finnius Date: Fri, 13 Jun 2025 01:52:24 +0000 Subject: [PATCH 2/2] Add 'kid' and 'pssh' search operators to /api/cache/search endpoint mariadb --- .../database/cache_to_db_mariadb.py | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/custom_functions/database/cache_to_db_mariadb.py b/custom_functions/database/cache_to_db_mariadb.py index de18fd3..41d359e 100644 --- a/custom_functions/database/cache_to_db_mariadb.py +++ b/custom_functions/database/cache_to_db_mariadb.py @@ -70,13 +70,24 @@ def search_by_pssh_or_kid(search_filter): try: with mysql.connector.connect(**get_db_config()) as conn: cursor = conn.cursor() - like_filter = f"%{search_filter}%" + if search_filter.lower().startswith("kid:"): + value = search_filter[4:] + + cursor.execute('SELECT PSSH, KID, `Key` FROM licenses WHERE KID = %s', (value,)) + results.update(cursor.fetchall()) + elif search_filter.lower().startswith("pssh:"): + value = search_filter[5:] - cursor.execute('SELECT PSSH, KID, `Key` FROM licenses WHERE PSSH LIKE %s', (like_filter,)) - results.update(cursor.fetchall()) + cursor.execute('SELECT PSSH, KID, `Key` FROM licenses WHERE PSSH LIKE %s', (f"%{value}%",)) + results.update(cursor.fetchall()) + else: + like_filter = f"%{search_filter}%" - cursor.execute('SELECT PSSH, KID, `Key` FROM licenses WHERE KID LIKE %s', (like_filter,)) - results.update(cursor.fetchall()) + cursor.execute('SELECT PSSH, KID, `Key` FROM licenses WHERE PSSH LIKE %s', (like_filter,)) + results.update(cursor.fetchall()) + + cursor.execute('SELECT PSSH, KID, `Key` FROM licenses WHERE KID LIKE %s', (like_filter,)) + results.update(cursor.fetchall()) final_results = [{'PSSH': row[0], 'KID': row[1], 'Key': row[2]} for row in results] return final_results[:20] -- 2.43.0