Skip to content

Commit

Permalink
autopep
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenixthrush committed Nov 19, 2024
1 parent 7177ef7 commit 06bd36f
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 113 deletions.
64 changes: 43 additions & 21 deletions src/aniworld/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,27 @@ def process_url(url):
return (season, episode, title, url)

with ThreadPoolExecutor(max_workers=8) as executor:
future_to_url = {executor.submit(process_url, url): url for url in season_data}
future_to_url = {executor.submit(
process_url, url): url for url in season_data}

results = []
for future in as_completed(future_to_url):
try:
result = future.result(timeout=5) # Timeout for future result
# Timeout for future result
result = future.result(timeout=5)
results.append(result)
logging.debug("Processed result: %s", result)
except TimeoutError as e:
logging.error("Timeout processing %s: %s", future_to_url[future], e)
logging.error("Timeout processing %s: %s",
future_to_url[future], e)

sorted_results = sorted(
results,
key=lambda x: (x[0] if x[0] > 0 else 999, x[1])
)

season_episode_map = {title: url for _, _, title, url in sorted_results}
season_episode_map = {title: url for _,
_, title, url in sorted_results}
self.episode_map = season_episode_map

episode_list = list(self.episode_map.keys())
Expand All @@ -142,7 +146,8 @@ def process_url(url):
name="Action",
values=["Watch", "Download", "Syncplay"],
max_height=4,
value=[["Watch", "Download", "Syncplay"].index(aniworld_globals.DEFAULT_ACTION)],
value=[["Watch", "Download", "Syncplay"].index(
aniworld_globals.DEFAULT_ACTION)],
scroll_exit=True
)
logging.debug("Action selector created")
Expand Down Expand Up @@ -183,7 +188,8 @@ def process_url(url):
name="Provider",
values=["Vidoza", "Streamtape", "VOE"], # Doodstream broken
max_height=3,
value=[["Vidoza", "Streamtape", "VOE"].index(aniworld_globals.DEFAULT_PROVIDER)],
value=[["Vidoza", "Streamtape", "VOE"].index(
aniworld_globals.DEFAULT_PROVIDER)],
scroll_exit=True
)
logging.debug("Provider selector created")
Expand Down Expand Up @@ -211,7 +217,8 @@ def process_url(url):
)

self.action_selector.when_value_edited = self.update_directory_visibility
logging.debug("Set update_directory_visibility as callback for action_selector")
logging.debug(
"Set update_directory_visibility as callback for action_selector")

def setup_signal_handling(self):
def signal_handler(_signal_number, _frame):
Expand Down Expand Up @@ -262,14 +269,16 @@ def on_ok(self):
logging.debug("Output directory: %s", output_directory)
if not output_directory and not self.directory_field.hidden:
logging.debug("No output directory provided")
npyscreen.notify_confirm("Please provide a directory.", title="Error")
npyscreen.notify_confirm(
"Please provide a directory.", title="Error")
return

selected_episodes = self.episode_selector.get_selected_objects()
action_selected = self.action_selector.get_selected_objects()
language_selected = self.language_selector.get_selected_objects()
provider_selected = self.provider_selector.get_selected_objects()
aniskip_selected = self.aniskip_selector.get_selected_objects()[0] == "Enable"
aniskip_selected = self.aniskip_selector.get_selected_objects()[
0] == "Enable"

logging.debug("Selected episodes: %s", selected_episodes)
logging.debug("Action selected: %s", action_selected)
Expand All @@ -279,18 +288,21 @@ def on_ok(self):

if not (selected_episodes and action_selected and language_selected):
logging.debug("No episodes or action or language selected")
npyscreen.notify_confirm("No episodes selected.", title="Selection")
npyscreen.notify_confirm(
"No episodes selected.", title="Selection")
return

lang = self.get_language_code(language_selected[0])
logging.debug("Language code: %s", lang)
provider_selected = self.validate_provider(provider_selected)
logging.debug("Validated provider: %s", provider_selected)

selected_urls = [self.episode_map[episode] for episode in selected_episodes]
selected_urls = [self.episode_map[episode]
for episode in selected_episodes]
selected_str = "\n".join(selected_episodes)
logging.debug("Selected URLs: %s", selected_urls)
npyscreen.notify_confirm(f"Selected episodes:\n{selected_str}", title="Selection")
npyscreen.notify_confirm(f"Selected episodes:\n{
selected_str}", title="Selection")

if not self.directory_field.hidden:
anime_title = format_anime_title(self.parentApp.anime_slug)
Expand Down Expand Up @@ -632,15 +644,20 @@ def parse_arguments():
],
check=True
)
logging.debug("Started tailing the log file in a new Terminal window.")
logging.debug(
"Started tailing the log file in a new Terminal window.")
except subprocess.CalledProcessError as e:
logging.error("Failed to start tailing the log file: %s", e)
logging.error(
"Failed to start tailing the log file: %s", e)
elif platform.system() == "Windows":
try:
command = ('start cmd /c "powershell -NoExit -c Get-Content '
'-Wait \\"$env:TEMP\\aniworld.log\\""')
subprocess.Popen(command, shell=True) # pylint: disable=consider-using-with
logging.debug("Started tailing the log file in a new Terminal window.")
# pylint: disable=consider-using-with
subprocess.Popen(
command, shell=True)
logging.debug(
"Started tailing the log file in a new Terminal window.")
except subprocess.CalledProcessError as e:
logging.error("Failed to start tailing the log file: %s", e)
elif platform.system() == "Linux":
Expand Down Expand Up @@ -710,7 +727,8 @@ def handle_query(args):
e = int(match.group(2))
logging.debug("Parsed season: %d, episode: %d", s, e)

args.episode = [f"https://aniworld.to/anime/stream/{slug}/staffel-{s}/episode-{e}"]
args.episode = [
f"https://aniworld.to/anime/stream/{slug}/staffel-{s}/episode-{e}"]
logging.debug("Set episode URL: %s", args.episode)


Expand Down Expand Up @@ -755,8 +773,10 @@ def main():
animes = read_episode_file(args.episode_file)
for slug, seasons in animes.items():
if args.output == aniworld_globals.DEFAULT_DOWNLOAD_PATH:
args.output = os.path.join(args.output, slug.replace("-", " ").title())
execute_with_params(args, seasons, slug, language, anime_slug=slug)
args.output = os.path.join(
args.output, slug.replace("-", " ").title())
execute_with_params(args, seasons, slug,
language, anime_slug=slug)
sys.exit()

anime_title = get_anime_title(args)
Expand All @@ -769,7 +789,8 @@ def main():
if args.episode:
for episode_url in args.episode:
slug = episode_url.split('/')[-1]
execute_with_params(args, selected_episodes, anime_title, language, anime_slug=slug)
execute_with_params(args, selected_episodes,
anime_title, language, anime_slug=slug)
logging.debug("Execution complete. Exiting.")
sys.exit()
except KeyboardInterrupt:
Expand Down Expand Up @@ -876,7 +897,8 @@ def run_app(query):
set_terminal_size()
run_app(search_anime(slug=args.slug, link=args.link))
except npyscreen.wgwidget.NotEnoughSpaceForWidget:
logging.debug("Not enough space for widget. Asking user to resize terminal.")
logging.debug(
"Not enough space for widget. Asking user to resize terminal.")
clear_screen()
print("Please increase your current terminal size.")
logging.debug("Exiting due to terminal size.")
Expand Down
3 changes: 2 additions & 1 deletion src/aniworld/common/adventure.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ def adventure():
print("Thanks for playing! Goodbye!")
break

conversation_history.append({'role': 'user', 'content': user_input})
conversation_history.append(
{'role': 'user', 'content': user_input})

response = ollama.chat(
model='llama3.2',
Expand Down
Loading

0 comments on commit 06bd36f

Please sign in to comment.