From ebb27daf0c681e9fef248c6999d8b95b32a0cf72 Mon Sep 17 00:00:00 2001 From: Danilo Reyes Date: Sat, 28 Feb 2026 20:05:33 -0600 Subject: [PATCH] turn sort functions fully pythonic --- src/download/download.py | 11 ++++++----- src/download/functions.py | 35 ++++++++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/download/download.py b/src/download/download.py index 00be6d2..8e07c38 100644 --- a/src/download/download.py +++ b/src/download/download.py @@ -83,14 +83,15 @@ def parse_gallery(gdl_list: str, user: User) -> None: gallery.run_command(args.flag_verbose) -def parse_instagram(link: str) -> list[str]: +def parse_instagram(link: str, post_type: list[str] | str | None = None) -> list[str]: """Fix instagram links""" args = get_args() if "instagram" not in link: return [] - if isinstance(args.post_type, list): - return ["-o", f"include={','.join(args.post_type)}"] - return ["-o", f"include={args.post_type}"] + use_type = args.post_type if post_type is None else post_type + if isinstance(use_type, list): + return ["-o", f"include={','.join(use_type)}"] + return ["-o", f"include={use_type}"] def video_command(video: Video): @@ -218,7 +219,7 @@ def save_comic(link: str) -> None: w_file.write(link + "\n") -def push_manager(user: User): +def push_manager(user: User) -> None: """Filters out the URL to use the appropiate downloader""" args = get_args() # Creates an array which will store any links that should use youtube-dl diff --git a/src/download/functions.py b/src/download/functions.py index 95f30ce..3a2ee09 100644 --- a/src/download/functions.py +++ b/src/download/functions.py @@ -9,6 +9,7 @@ import logging import shlex import subprocess import shutil +import random from typing import Sequence from pathlib import Path import yaml @@ -97,20 +98,36 @@ def list_lines(i: int, line: str) -> str: def sort_txt_file(file_path: Path): """Sort every line alphabetically remove duplicated and empty lines""" - file = str(file_path.resolve()) - run(["sort", "-u", file, "-o", file], VERBOSE_G) - run(["sed", "-i", "/^$/d", file], VERBOSE_G) - run(["sed", "-i", "-e", "s,http:,https:,", file], VERBOSE_G) - # fix this using strip on python - # line.strip("/") - run(["sed", "-i", "-e", "s,/$,,", file], VERBOSE_G) # trailing / + path = file_path.resolve() + with open(path, "r", encoding="utf-8") as open_file: + lines = [ln.strip() for ln in open_file] + + normalized = [] + for ln in lines: + if not ln: + continue + ln = ln.replace("http://", "https://") + ln = ln.rstrip("/") + normalized.append(ln) + + unique_sorted = sorted(set(normalized)) + with open(path, "w", encoding="utf-8") as open_file: + open_file.write("\n".join(unique_sorted)) + if unique_sorted: + open_file.write("\n") def randomize_txt_file(file_path: Path): """Randomize the order of the lines of the txt file""" - file = str(file_path.resolve()) - run(["sort", "-R", file, "-o", file], VERBOSE_G) + path = file_path.resolve() + with open(path, "r", encoding="utf-8") as open_file: + lines = [ln.rstrip("\n") for ln in open_file] + random.shuffle(lines) + with open(path, "w", encoding="utf-8") as open_file: + open_file.write("\n".join(lines)) + if lines: + open_file.write("\n") def parse_list(file):