From bc917167d26f99f56f5e36ab93ac00321eac3fb4 Mon Sep 17 00:00:00 2001 From: nathom Date: Tue, 29 Jun 2021 21:04:27 -0700 Subject: [PATCH] Make streamrip into a module Signed-off-by: nathom --- pyproject.toml | 2 +- requirements.txt | 6 ------ rip/__main__.py | 3 +++ {streamrip => rip}/cli.py | 6 +++--- {streamrip => rip}/config.py | 4 ++-- {streamrip => rip}/config.toml | 0 {streamrip => rip}/core.py | 24 +++++++++++------------- {streamrip => rip}/db.py | 12 +++++++++++- streamrip/__init__.py | 2 ++ streamrip/bases.py | 26 +++++++++++++------------- streamrip/tracklists.py | 3 ++- 11 files changed, 48 insertions(+), 40 deletions(-) delete mode 100644 requirements.txt create mode 100644 rip/__main__.py rename {streamrip => rip}/cli.py (98%) rename {streamrip => rip}/config.py (97%) rename {streamrip => rip}/config.toml (100%) rename {streamrip => rip}/core.py (98%) rename {streamrip => rip}/db.py (89%) diff --git a/pyproject.toml b/pyproject.toml index 22991e9..c5ba413 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,7 +15,7 @@ classifiers = [ ] [tool.poetry.scripts] -rip = "streamrip.cli:main" +rip = "rip.cli:main" [tool.poetry.dependencies] python = "^3.8" diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index ef523e9..0000000 --- a/requirements.txt +++ /dev/null @@ -1,6 +0,0 @@ -click -pathvalidate -requests -mutagen>=1.45.1 -tqdm -tomlkit diff --git a/rip/__main__.py b/rip/__main__.py new file mode 100644 index 0000000..4e28416 --- /dev/null +++ b/rip/__main__.py @@ -0,0 +1,3 @@ +from .cli import main + +main() diff --git a/streamrip/cli.py b/rip/cli.py similarity index 98% rename from streamrip/cli.py rename to rip/cli.py index 6e1f355..e31803e 100644 --- a/streamrip/cli.py +++ b/rip/cli.py @@ -9,10 +9,10 @@ from hashlib import md5 import click import requests -from . import __version__ -from .clients import TidalClient +from streamrip import __version__ +from streamrip.clients import TidalClient from .config import Config -from .constants import CACHE_DIR, CONFIG_DIR, CONFIG_PATH, QOBUZ_FEATURED_KEYS +from streamrip.constants import CACHE_DIR, CONFIG_DIR, CONFIG_PATH, QOBUZ_FEATURED_KEYS from .core import MusicDL logging.basicConfig(level="WARNING") diff --git a/streamrip/config.py b/rip/config.py similarity index 97% rename from streamrip/config.py rename to rip/config.py index 56226e6..2c2fede 100644 --- a/streamrip/config.py +++ b/rip/config.py @@ -10,8 +10,8 @@ from typing import Any, Dict import click import tomlkit -from .constants import CONFIG_DIR, CONFIG_PATH, DOWNLOADS_DIR -from .exceptions import InvalidSourceError +from streamrip.constants import CONFIG_DIR, CONFIG_PATH, DOWNLOADS_DIR +from streamrip.exceptions import InvalidSourceError logger = logging.getLogger("streamrip") diff --git a/streamrip/config.toml b/rip/config.toml similarity index 100% rename from streamrip/config.toml rename to rip/config.toml diff --git a/streamrip/core.py b/rip/core.py similarity index 98% rename from streamrip/core.py rename to rip/core.py index e13a69a..da38fb2 100644 --- a/streamrip/core.py +++ b/rip/core.py @@ -14,8 +14,8 @@ import click import requests from tqdm import tqdm -from .bases import Track, Video, YoutubeVideo -from .clients import ( +from streamrip.bases import Track, Video, YoutubeVideo +from streamrip.clients import ( Client, DeezerClient, QobuzClient, @@ -23,7 +23,7 @@ from .clients import ( TidalClient, ) from .config import Config -from .constants import ( +from streamrip.constants import ( CONFIG_PATH, DB_PATH, DEEZER_DYNAMIC_LINK_REGEX, @@ -35,15 +35,15 @@ from .constants import ( YOUTUBE_URL_REGEX, ) from .db import MusicDB -from .exceptions import ( +from streamrip.exceptions import ( AuthenticationError, MissingCredentials, NonStreamable, NoResultsFound, ParsingError, ) -from .tracklists import Album, Artist, Label, Playlist, Tracklist -from .utils import extract_deezer_dynamic_link, extract_interpreter_url +from streamrip.tracklists import Album, Artist, Label, Playlist, Tracklist +from streamrip.utils import extract_deezer_dynamic_link, extract_interpreter_url logger = logging.getLogger("streamrip") @@ -98,7 +98,7 @@ class MusicDL(list): "soundcloud": SoundCloudClient(), } - self.db: Union[MusicDB, list] + self.db: MusicDB db_settings = self.config.session["database"] if db_settings["enabled"]: path = db_settings["path"] @@ -109,7 +109,7 @@ class MusicDL(list): self.config.file["database"]["path"] = DB_PATH self.config.save() else: - self.db = [] + self.db = MusicDB(None, empty=True) def handle_urls(self, urls): """Download a url. @@ -191,7 +191,6 @@ class MusicDL(list): session[key] for key in ("artwork", "conversion", "filepaths") ) return { - "database": self.db, "parent_folder": session["downloads"]["folder"], "folder_format": filepaths["folder_format"], "track_format": filepaths["track_format"], @@ -254,15 +253,14 @@ class MusicDL(list): click.secho(f"{item!s} is not available, skipping.", fg="red") continue - item.download(**arguments) + if item.download(**arguments) and hasattr(item, "id"): + self.db.add(item.id) + if isinstance(item, Track): item.tag() if arguments["conversion"]["enabled"]: item.convert(**arguments["conversion"]) - if self.db != [] and hasattr(item, "id"): - self.db.add(item.id) - def get_client(self, source: str) -> Client: """Get a client given the source and log in. diff --git a/streamrip/db.py b/rip/db.py similarity index 89% rename from streamrip/db.py rename to rip/db.py index 3102eb2..e76f352 100644 --- a/streamrip/db.py +++ b/rip/db.py @@ -11,12 +11,15 @@ logger = logging.getLogger("streamrip") class MusicDB: """Simple interface for the downloaded track database.""" - def __init__(self, db_path: Union[str, os.PathLike]): + def __init__(self, db_path: Union[str, os.PathLike], empty=False): """Create a MusicDB object. :param db_path: filepath of the database :type db_path: Union[str, os.PathLike] """ + if empty: + self.path = None + self.path = db_path if not os.path.exists(self.path): self.create() @@ -39,6 +42,9 @@ class MusicDB: :type item_id: str :rtype: bool """ + if self.path is None: + return False + logger.debug("Checking database for ID %s", item_id) with sqlite3.connect(self.path) as conn: return ( @@ -55,6 +61,10 @@ class MusicDB: :type item_id: str """ logger.debug("Adding ID %s", item_id) + + if self.path is None: + return + with sqlite3.connect(self.path) as conn: try: conn.execute( diff --git a/streamrip/__init__.py b/streamrip/__init__.py index 8551bca..bf2117e 100644 --- a/streamrip/__init__.py +++ b/streamrip/__init__.py @@ -1,3 +1,5 @@ """streamrip: the all in one music downloader.""" __version__ = "0.6.7" + +from . import clients, converter, bases, tracklists, constants diff --git a/streamrip/bases.py b/streamrip/bases.py index 845559d..bbb14a3 100644 --- a/streamrip/bases.py +++ b/streamrip/bases.py @@ -163,15 +163,15 @@ class Track: os.makedirs(self.folder, exist_ok=True) - if self.id in kwargs.get("database", []): - self.downloaded = True - self.tagged = True - self.path = self.final_path - decho( - f"{self['title']} already logged in database, skipping.", - fg="magenta", - ) - return False # because the track was not downloaded + # if self.id in kwargs.get("database", []): + # self.downloaded = True + # self.tagged = True + # self.path = self.final_path + # decho( + # f"{self['title']} already logged in database, skipping.", + # fg="magenta", + # ) + # return False # because the track was not downloaded if os.path.isfile(self.final_path): # track already exists self.downloaded = True @@ -263,10 +263,10 @@ class Track: if not kwargs.get("stay_temp", False): self.move(self.final_path) - database = kwargs.get("database") - if database: - database.add(self.id) - logger.debug(f"{self.id} added to database") + # database = kwargs.get("database") + # if database: + # database.add(self.id) + # logger.debug(f"{self.id} added to database") logger.debug("Downloaded: %s -> %s", self.path, self.final_path) diff --git a/streamrip/tracklists.py b/streamrip/tracklists.py index ee18b71..34a5a44 100644 --- a/streamrip/tracklists.py +++ b/streamrip/tracklists.py @@ -14,7 +14,8 @@ from pathvalidate import sanitize_filename from .bases import Booklet, Track, Tracklist, Video from .clients import Client from .constants import ALBUM_KEYS, FLAC_MAX_BLOCKSIZE, FOLDER_FORMAT -from .db import MusicDB + +# from .db import MusicDB from .exceptions import InvalidSourceError, NonStreamable from .metadata import TrackMetadata from .utils import (