aboutsummaryrefslogtreecommitdiffstats
path: root/cogs/utils
diff options
context:
space:
mode:
Diffstat (limited to 'cogs/utils')
-rw-r--r--cogs/utils/file_search.py35
1 files changed, 32 insertions, 3 deletions
diff --git a/cogs/utils/file_search.py b/cogs/utils/file_search.py
index 8d3e5d6..7100f83 100644
--- a/cogs/utils/file_search.py
+++ b/cogs/utils/file_search.py
@@ -7,6 +7,7 @@ import json
7import logging 7import logging
8import aiohttp 8import aiohttp
9import os 9import os
10from typing import List
10from dotenv import load_dotenv 11from dotenv import load_dotenv
11from cogs.utils.settings import get_baseurl 12from cogs.utils.settings import get_baseurl
12 13
@@ -19,12 +20,13 @@ class UniversalSearcher:
19 """Handles searching for files via copyparty API.""" 20 """Handles searching for files via copyparty API."""
20 21
21 @staticmethod 22 @staticmethod
22 async def search_copyparty(query: str, max_results: int = 25): 23 async def search_copyparty(query: str, max_results: int = 25, file_extensions: List[str] = None):
23 """Search for files using copyparty API (jPOST method). 24 """Search for files using copyparty API (jPOST method).
24 25
25 Args: 26 Args:
26 query: Search query string 27 query: Search query string
27 max_results: Maximum number of results to return 28 max_results: Maximum number of results to return
29 file_extensions: Optional list of file extensions to filter by (e.g., ['mp3', 'wav'])
28 30
29 Returns: 31 Returns:
30 List of matching file URLs 32 List of matching file URLs
@@ -34,7 +36,16 @@ class UniversalSearcher:
34 36
35 query = query.strip() 37 query = query.strip()
36 logger.info(f"Starting copyparty search for query: '{query}'") 38 logger.info(f"Starting copyparty search for query: '{query}'")
37 query = f'name like "*{query}*"' 39
40 # Build the query with optional extension filtering
41 name_query = f'name like "*{query}*"'
42 if file_extensions:
43 # Create extension filters using copyparty's .ext$ syntax
44 ext_filters = [f"name like \"*.{ext.lower()}\"" for ext in file_extensions]
45 ext_query = " or ".join(ext_filters)
46 query = f"{name_query} and ({ext_query})"
47 else:
48 query = name_query
38 49
39 try: 50 try:
40 baseurl = get_baseurl() 51 baseurl = get_baseurl()
@@ -115,7 +126,7 @@ class UniversalSearcher:
115 126
116 @staticmethod 127 @staticmethod
117 async def search_copyparty_autocomplete(query: str, max_results: int = 10): 128 async def search_copyparty_autocomplete(query: str, max_results: int = 10):
118 """Wrapper for autocomplete searches using copyparty. 129 """Wrapper for autocomplete searches using copyparty (general search - no file type filtering).
119 130
120 Args: 131 Args:
121 query: Search query string 132 query: Search query string
@@ -128,3 +139,21 @@ class UniversalSearcher:
128 return [] 139 return []
129 140
130 return await UniversalSearcher.search_copyparty(query, max_results) 141 return await UniversalSearcher.search_copyparty(query, max_results)
142
143 @staticmethod
144 async def search_copyparty_autocomplete_audio(query: str, max_results: int = 10):
145 """Wrapper for autocomplete searches using copyparty (audio files only).
146
147 Args:
148 query: Search query string
149 max_results: Maximum number of results to return
150
151 Returns:
152 List of matching file URLs (audio files only)
153 """
154 if not query:
155 return []
156
157 # Audio file extensions for music commands
158 audio_extensions = ['mp3', 'wav', 'flac', 'ogg', 'm4a', 'aac', 'mp4', "mkv"]
159 return await UniversalSearcher.search_copyparty(query, max_results, audio_extensions)