turn sort functions fully pythonic

This commit is contained in:
Danilo Reyes
2026-02-28 20:05:33 -06:00
parent fcd898873c
commit ebb27daf0c
2 changed files with 32 additions and 14 deletions

View File

@@ -83,14 +83,15 @@ def parse_gallery(gdl_list: str, user: User) -> None:
gallery.run_command(args.flag_verbose) 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""" """Fix instagram links"""
args = get_args() args = get_args()
if "instagram" not in link: if "instagram" not in link:
return [] return []
if isinstance(args.post_type, list): use_type = args.post_type if post_type is None else post_type
return ["-o", f"include={','.join(args.post_type)}"] if isinstance(use_type, list):
return ["-o", f"include={args.post_type}"] return ["-o", f"include={','.join(use_type)}"]
return ["-o", f"include={use_type}"]
def video_command(video: Video): def video_command(video: Video):
@@ -218,7 +219,7 @@ def save_comic(link: str) -> None:
w_file.write(link + "\n") 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""" """Filters out the URL to use the appropiate downloader"""
args = get_args() args = get_args()
# Creates an array which will store any links that should use youtube-dl # Creates an array which will store any links that should use youtube-dl

View File

@@ -9,6 +9,7 @@ import logging
import shlex import shlex
import subprocess import subprocess
import shutil import shutil
import random
from typing import Sequence from typing import Sequence
from pathlib import Path from pathlib import Path
import yaml import yaml
@@ -97,20 +98,36 @@ def list_lines(i: int, line: str) -> str:
def sort_txt_file(file_path: Path): def sort_txt_file(file_path: Path):
"""Sort every line alphabetically """Sort every line alphabetically
remove duplicated and empty lines""" remove duplicated and empty lines"""
file = str(file_path.resolve()) path = file_path.resolve()
run(["sort", "-u", file, "-o", file], VERBOSE_G) with open(path, "r", encoding="utf-8") as open_file:
run(["sed", "-i", "/^$/d", file], VERBOSE_G) lines = [ln.strip() for ln in open_file]
run(["sed", "-i", "-e", "s,http:,https:,", file], VERBOSE_G)
# fix this using strip on python normalized = []
# line.strip("/") for ln in lines:
run(["sed", "-i", "-e", "s,/$,,", file], VERBOSE_G) # trailing / 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): def randomize_txt_file(file_path: Path):
"""Randomize the order of the """Randomize the order of the
lines of the txt file""" lines of the txt file"""
file = str(file_path.resolve()) path = file_path.resolve()
run(["sort", "-R", file, "-o", file], VERBOSE_G) 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): def parse_list(file):