mirror of
https://github.com/Benexl/FastAnime.git
synced 2025-12-08 05:40:38 -08:00
refactor: streamline authentication and search command preparation in dynamic search
This commit is contained in:
@@ -1,7 +1,5 @@
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
|
||||||
import tempfile
|
|
||||||
|
|
||||||
from .....core.constants import APP_CACHE_DIR, SCRIPTS_DIR
|
from .....core.constants import APP_CACHE_DIR, SCRIPTS_DIR
|
||||||
from .....libs.media_api.params import MediaSearchParams
|
from .....libs.media_api.params import MediaSearchParams
|
||||||
@@ -36,14 +34,11 @@ def dynamic_search(ctx: Context, state: State) -> State | InternalDirective:
|
|||||||
|
|
||||||
# Prepare the search script
|
# Prepare the search script
|
||||||
auth_header = ""
|
auth_header = ""
|
||||||
if ctx.media_api.is_authenticated() and hasattr(ctx.media_api, "token"):
|
profile = ctx.auth.get_auth()
|
||||||
auth_header = f"Bearer {ctx.media_api.token}"
|
if ctx.media_api.is_authenticated() and profile:
|
||||||
|
auth_header = f"Bearer {profile.token}"
|
||||||
|
|
||||||
# Create a temporary search script
|
search_command = SEARCH_TEMPLATE_SCRIPT
|
||||||
with tempfile.NamedTemporaryFile(
|
|
||||||
mode="w", suffix=".sh", delete=False
|
|
||||||
) as temp_script:
|
|
||||||
script_content = SEARCH_TEMPLATE_SCRIPT
|
|
||||||
|
|
||||||
replacements = {
|
replacements = {
|
||||||
"GRAPHQL_ENDPOINT": "https://graphql.anilist.co",
|
"GRAPHQL_ENDPOINT": "https://graphql.anilist.co",
|
||||||
@@ -54,16 +49,8 @@ def dynamic_search(ctx: Context, state: State) -> State | InternalDirective:
|
|||||||
}
|
}
|
||||||
|
|
||||||
for key, value in replacements.items():
|
for key, value in replacements.items():
|
||||||
script_content = script_content.replace(f"{{{key}}}", str(value))
|
search_command = search_command.replace(f"{{{key}}}", str(value))
|
||||||
|
|
||||||
temp_script.write(script_content)
|
|
||||||
temp_script_path = temp_script.name
|
|
||||||
|
|
||||||
try:
|
|
||||||
# Make the script executable
|
|
||||||
os.chmod(temp_script_path, 0o755)
|
|
||||||
|
|
||||||
# Use the selector's search functionality
|
|
||||||
try:
|
try:
|
||||||
# Prepare preview functionality
|
# Prepare preview functionality
|
||||||
preview_command = None
|
preview_command = None
|
||||||
@@ -75,21 +62,17 @@ def dynamic_search(ctx: Context, state: State) -> State | InternalDirective:
|
|||||||
|
|
||||||
choice = ctx.selector.search(
|
choice = ctx.selector.search(
|
||||||
prompt="Search Anime",
|
prompt="Search Anime",
|
||||||
search_command=f"bash {temp_script_path} {{q}}",
|
search_command=search_command,
|
||||||
preview=preview_command,
|
preview=preview_command,
|
||||||
header="Type to search for anime dynamically",
|
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
choice = ctx.selector.search(
|
choice = ctx.selector.search(
|
||||||
prompt="Search Anime",
|
prompt="Search Anime",
|
||||||
search_command=f"bash {temp_script_path} {{q}}",
|
search_command=search_command,
|
||||||
header="Type to search for anime dynamically",
|
|
||||||
)
|
)
|
||||||
except NotImplementedError:
|
except NotImplementedError:
|
||||||
feedback.error("Dynamic search is not supported by your current selector")
|
feedback.error("Dynamic search is not supported by your current selector")
|
||||||
feedback.info(
|
feedback.info("Please use the regular search option or switch to fzf selector")
|
||||||
"Please use the regular search option or switch to fzf selector"
|
|
||||||
)
|
|
||||||
return InternalDirective.MAIN
|
return InternalDirective.MAIN
|
||||||
|
|
||||||
if not choice:
|
if not choice:
|
||||||
@@ -100,7 +83,6 @@ def dynamic_search(ctx: Context, state: State) -> State | InternalDirective:
|
|||||||
logger.error("Search results file not found")
|
logger.error("Search results file not found")
|
||||||
return InternalDirective.MAIN
|
return InternalDirective.MAIN
|
||||||
|
|
||||||
try:
|
|
||||||
with open(SEARCH_RESULTS_FILE, "r", encoding="utf-8") as f:
|
with open(SEARCH_RESULTS_FILE, "r", encoding="utf-8") as f:
|
||||||
raw_data = json.load(f)
|
raw_data = json.load(f)
|
||||||
|
|
||||||
@@ -120,9 +102,7 @@ def dynamic_search(ctx: Context, state: State) -> State | InternalDirective:
|
|||||||
or media_item.title.native
|
or media_item.title.native
|
||||||
or "Unknown"
|
or "Unknown"
|
||||||
)
|
)
|
||||||
year = (
|
year = media_item.start_date.year if media_item.start_date else "Unknown"
|
||||||
media_item.start_date.year if media_item.start_date else "Unknown"
|
|
||||||
)
|
|
||||||
status = media_item.status.value if media_item.status else "Unknown"
|
status = media_item.status.value if media_item.status else "Unknown"
|
||||||
genres = (
|
genres = (
|
||||||
", ".join([genre.value for genre in media_item.genres[:3]])
|
", ".join([genre.value for genre in media_item.genres[:3]])
|
||||||
@@ -150,15 +130,3 @@ def dynamic_search(ctx: Context, state: State) -> State | InternalDirective:
|
|||||||
page_info=search_result.page_info,
|
page_info=search_result.page_info,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
except (json.JSONDecodeError, KeyError, Exception) as e:
|
|
||||||
logger.error(f"Error processing search results: {e}")
|
|
||||||
feedback.error("Failed to process search results")
|
|
||||||
return InternalDirective.MAIN
|
|
||||||
|
|
||||||
finally:
|
|
||||||
# Clean up the temporary script
|
|
||||||
try:
|
|
||||||
os.unlink(temp_script_path)
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
|
|||||||
@@ -122,7 +122,7 @@ class FzfSelector(BaseSelector):
|
|||||||
"--prompt",
|
"--prompt",
|
||||||
f"{prompt.title()}: ",
|
f"{prompt.title()}: ",
|
||||||
"--header",
|
"--header",
|
||||||
header or self.header,
|
self.header,
|
||||||
"--header-first",
|
"--header-first",
|
||||||
"--bind",
|
"--bind",
|
||||||
f"change:reload({search_command})",
|
f"change:reload({search_command})",
|
||||||
|
|||||||
Reference in New Issue
Block a user