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)
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

View File

@@ -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):