A few type fixes
This commit is contained in:
@@ -54,7 +54,7 @@ import logging
|
|||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import pprint
|
import pprint
|
||||||
from typing import List, Set, Tuple
|
from typing import List, Set
|
||||||
from docopt import docopt
|
from docopt import docopt
|
||||||
from configparser import ConfigParser
|
from configparser import ConfigParser
|
||||||
|
|
||||||
@@ -63,6 +63,7 @@ from free_bandcamp_downloader.bc_free_downloader import (
|
|||||||
AlbumInfo,
|
AlbumInfo,
|
||||||
BCFreeDownloader,
|
BCFreeDownloader,
|
||||||
BCFreeDownloaderOptions,
|
BCFreeDownloaderOptions,
|
||||||
|
TralbumId,
|
||||||
)
|
)
|
||||||
from free_bandcamp_downloader import logger
|
from free_bandcamp_downloader import logger
|
||||||
|
|
||||||
@@ -141,17 +142,17 @@ def get_data_dir() -> str:
|
|||||||
return data_dir
|
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
|
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"]
|
history_file = config.parser["free-bandcamp-downloader"]["download-history-file"]
|
||||||
with open(history_file, "a") as f:
|
with open(history_file, "a") as f:
|
||||||
f.write(f"{id[0][0]}:{id[1]}\n")
|
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"]
|
history_file = config.parser["free-bandcamp-downloader"]["download-history-file"]
|
||||||
if not os.path.exists(history_file):
|
if not os.path.exists(history_file):
|
||||||
with open(history_file, "w") as f:
|
with open(history_file, "w") as f:
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ from bs4 import BeautifulSoup
|
|||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from http.cookiejar import MozillaCookieJar
|
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 urllib.parse import urljoin
|
||||||
from guerrillamail import GuerrillaMailSession
|
from guerrillamail import GuerrillaMailSession
|
||||||
from urllib3 import Retry
|
from urllib3 import Retry
|
||||||
@@ -21,9 +21,11 @@ from urllib3 import Retry
|
|||||||
from free_bandcamp_downloader import logger
|
from free_bandcamp_downloader import logger
|
||||||
from free_bandcamp_downloader.bandcamp_http_adapter import BandcampHTTPAdapter
|
from free_bandcamp_downloader.bandcamp_http_adapter import BandcampHTTPAdapter
|
||||||
|
|
||||||
|
TralbumId = Tuple[Literal["album", "track", "url"], Union[int, str]]
|
||||||
|
|
||||||
|
|
||||||
class DownloadRet(TypedDict):
|
class DownloadRet(TypedDict):
|
||||||
id: Tuple[str, int]
|
id: TralbumId
|
||||||
file_name: str
|
file_name: str
|
||||||
|
|
||||||
|
|
||||||
@@ -37,7 +39,7 @@ class AlbumInfo(TypedDict):
|
|||||||
|
|
||||||
class LabelReleaseInfo(TypedDict):
|
class LabelReleaseInfo(TypedDict):
|
||||||
type: str
|
type: str
|
||||||
id: Tuple[str, int]
|
id: TralbumId
|
||||||
band_id: int
|
band_id: int
|
||||||
url: str
|
url: str
|
||||||
release_info: Optional[AlbumInfo]
|
release_info: Optional[AlbumInfo]
|
||||||
@@ -49,7 +51,7 @@ class LabelInfo(TypedDict):
|
|||||||
|
|
||||||
|
|
||||||
class PageInfo(TypedDict):
|
class PageInfo(TypedDict):
|
||||||
type: str
|
type: Literal["album", "song", "band"]
|
||||||
info: LabelInfo | AlbumInfo
|
info: LabelInfo | AlbumInfo
|
||||||
|
|
||||||
|
|
||||||
@@ -86,7 +88,7 @@ class BCFreeDownloader:
|
|||||||
def __init__(self, options: BCFreeDownloaderOptions):
|
def __init__(self, options: BCFreeDownloaderOptions):
|
||||||
self.options = options
|
self.options = options
|
||||||
self.mail_session = None
|
self.mail_session = None
|
||||||
self.queued_emails = {} # { ("album"|"track", id): {info} }
|
self.queued_emails: Dict[TralbumId, AlbumInfo] = {}
|
||||||
self.session = None
|
self.session = None
|
||||||
self.email = None
|
self.email = None
|
||||||
self._init_session()
|
self._init_session()
|
||||||
@@ -311,15 +313,15 @@ class BCFreeDownloader:
|
|||||||
# returns either the result of download_album or download_label
|
# returns either the result of download_album or download_label
|
||||||
# with the `page_type` set to album|song|band
|
# with the `page_type` set to album|song|band
|
||||||
# exception if download error
|
# 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)
|
soup = self.get_url_soup(url)
|
||||||
|
|
||||||
page_info = self.get_page_info(soup)
|
page_info = self.get_page_info(soup)
|
||||||
page_type = page_info.get("type")
|
page_type = page_info.get("type")
|
||||||
if page_type == "album" or page_type == "song":
|
if page_type == "album" or page_type == "song":
|
||||||
ret = self.download_album(soup, force)
|
ret = self.download_album(soup)
|
||||||
else:
|
else:
|
||||||
ret = self.download_label(soup, force)
|
ret = self.download_label(soup)
|
||||||
|
|
||||||
ret["page_type"] = page_type
|
ret["page_type"] = page_type
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user