From 9766944201b99d222e0e06c5a92954cd5eb9c360 Mon Sep 17 00:00:00 2001 From: 7x11x13 Date: Fri, 21 Mar 2025 14:13:09 -0400 Subject: [PATCH] A few type fixes --- free_bandcamp_downloader/__main__.py | 9 +++++---- free_bandcamp_downloader/bc_free_downloader.py | 18 ++++++++++-------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/free_bandcamp_downloader/__main__.py b/free_bandcamp_downloader/__main__.py index 2a2e7de..5ba84ce 100644 --- a/free_bandcamp_downloader/__main__.py +++ b/free_bandcamp_downloader/__main__.py @@ -54,7 +54,7 @@ import logging import sys import os import pprint -from typing import List, Set, Tuple +from typing import List, Set from docopt import docopt from configparser import ConfigParser @@ -63,6 +63,7 @@ from free_bandcamp_downloader.bc_free_downloader import ( AlbumInfo, BCFreeDownloader, BCFreeDownloaderOptions, + TralbumId, ) from free_bandcamp_downloader import logger @@ -141,17 +142,17 @@ def get_data_dir() -> str: return data_dir -def is_downloaded(downloaded_set, id: Tuple[str, int], url: str = None) -> bool: +def is_downloaded(downloaded_set, id: TralbumId, url: str = None) -> bool: return id in downloaded_set or url in downloaded_set -def add_to_dl_file(config: Config, id: Tuple[str, int]): +def add_to_dl_file(config: Config, id: TralbumId): history_file = config.parser["free-bandcamp-downloader"]["download-history-file"] with open(history_file, "a") as f: f.write(f"{id[0][0]}:{id[1]}\n") -def get_downloaded(config: Config) -> Set[Tuple[str, int | str]]: +def get_downloaded(config: Config) -> Set[TralbumId]: history_file = config.parser["free-bandcamp-downloader"]["download-history-file"] if not os.path.exists(history_file): with open(history_file, "w") as f: diff --git a/free_bandcamp_downloader/bc_free_downloader.py b/free_bandcamp_downloader/bc_free_downloader.py index cf5f669..3a321c1 100644 --- a/free_bandcamp_downloader/bc_free_downloader.py +++ b/free_bandcamp_downloader/bc_free_downloader.py @@ -13,7 +13,7 @@ from bs4 import BeautifulSoup from tqdm import tqdm from dataclasses import dataclass from http.cookiejar import MozillaCookieJar -from typing import Dict, List, Optional, Tuple, TypedDict +from typing import Dict, List, Literal, Optional, Tuple, TypedDict, Union from urllib.parse import urljoin from guerrillamail import GuerrillaMailSession from urllib3 import Retry @@ -21,9 +21,11 @@ from urllib3 import Retry from free_bandcamp_downloader import logger from free_bandcamp_downloader.bandcamp_http_adapter import BandcampHTTPAdapter +TralbumId = Tuple[Literal["album", "track", "url"], Union[int, str]] + class DownloadRet(TypedDict): - id: Tuple[str, int] + id: TralbumId file_name: str @@ -37,7 +39,7 @@ class AlbumInfo(TypedDict): class LabelReleaseInfo(TypedDict): type: str - id: Tuple[str, int] + id: TralbumId band_id: int url: str release_info: Optional[AlbumInfo] @@ -49,7 +51,7 @@ class LabelInfo(TypedDict): class PageInfo(TypedDict): - type: str + type: Literal["album", "song", "band"] info: LabelInfo | AlbumInfo @@ -86,7 +88,7 @@ class BCFreeDownloader: def __init__(self, options: BCFreeDownloaderOptions): self.options = options self.mail_session = None - self.queued_emails = {} # { ("album"|"track", id): {info} } + self.queued_emails: Dict[TralbumId, AlbumInfo] = {} self.session = None self.email = None self._init_session() @@ -311,15 +313,15 @@ class BCFreeDownloader: # returns either the result of download_album or download_label # with the `page_type` set to album|song|band # exception if download error - def download_url(self, url: str, force: bool = False): + def download_url(self, url: str): soup = self.get_url_soup(url) page_info = self.get_page_info(soup) page_type = page_info.get("type") if page_type == "album" or page_type == "song": - ret = self.download_album(soup, force) + ret = self.download_album(soup) else: - ret = self.download_label(soup, force) + ret = self.download_label(soup) ret["page_type"] = page_type