Added config file support
This commit is contained in:
parent
940ea2801f
commit
401d8de03f
2 changed files with 45 additions and 41 deletions
|
@ -1,4 +1,5 @@
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
from pathlib import Path
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
# Initialize classes from lyrics_dl/providers
|
# Initialize classes from lyrics_dl/providers
|
||||||
|
@ -53,3 +54,33 @@ class LyricsDl:
|
||||||
self.logger.info(f"[{provider.name}] No lyrics was found!")
|
self.logger.info(f"[{provider.name}] No lyrics was found!")
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def process_file(self, path: Path, force: bool = False) -> None:
|
||||||
|
lyrics_path = path.with_suffix(".lrc")
|
||||||
|
|
||||||
|
if lyrics_path.exists() and not force:
|
||||||
|
self.logger.error("[lyrics-dl] Lyrics file already exists!")
|
||||||
|
return
|
||||||
|
|
||||||
|
# TODO handle errors
|
||||||
|
try:
|
||||||
|
song = Song.from_file(path)
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.error(f"[lyrics-dl] {path}: {e}")
|
||||||
|
return
|
||||||
|
|
||||||
|
lyrics = self.fetch_lyrics(song)
|
||||||
|
|
||||||
|
if not lyrics:
|
||||||
|
self.logger.error("[lyrics-dl] No lyrics was found!")
|
||||||
|
return
|
||||||
|
|
||||||
|
with open(lyrics_path, "w") as f:
|
||||||
|
f.write(lyrics)
|
||||||
|
|
||||||
|
def process_directory(self, path: Path, extensions: list[str], force: bool = False) -> None:
|
||||||
|
for file_path in path.rglob("*"):
|
||||||
|
if file_path.suffix[1:] not in extensions:
|
||||||
|
continue
|
||||||
|
|
||||||
|
self.process_file(file_path, force)
|
||||||
|
|
|
@ -1,62 +1,35 @@
|
||||||
import argparse
|
import argparse
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from lyrics_dl.core import Song
|
|
||||||
from lyrics_dl.config import LyricsDlConfig
|
from lyrics_dl.config import LyricsDlConfig
|
||||||
from lyrics_dl.logger import DefaultLogger
|
from lyrics_dl.logger import DefaultLogger
|
||||||
from lyrics_dl import LyricsDl
|
from lyrics_dl import LyricsDl
|
||||||
|
|
||||||
|
|
||||||
logger = DefaultLogger()
|
|
||||||
config = LyricsDlConfig()
|
|
||||||
lyrics_dl = LyricsDl(config=config, logger=logger)
|
|
||||||
|
|
||||||
|
|
||||||
def process_file(path, force=False):
|
|
||||||
lyrics_path = path.with_suffix(".lrc")
|
|
||||||
|
|
||||||
if lyrics_path.exists() and not force:
|
|
||||||
logger.error("[lyrics-dl] Lyrics file already exists!")
|
|
||||||
return
|
|
||||||
|
|
||||||
# TODO handle errors
|
|
||||||
try:
|
|
||||||
song = Song.from_file(path)
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"[lyrics-dl] {path}: {e}")
|
|
||||||
return
|
|
||||||
|
|
||||||
lyrics = lyrics_dl.fetch_lyrics(song)
|
|
||||||
|
|
||||||
if not lyrics:
|
|
||||||
logger.error("[lyrics-dl] No lyrics was found!")
|
|
||||||
return
|
|
||||||
|
|
||||||
with open(lyrics_path, "w") as f:
|
|
||||||
f.write(lyrics)
|
|
||||||
|
|
||||||
|
|
||||||
def process_directory(path, extensions):
|
|
||||||
for file_path in path.rglob("*"):
|
|
||||||
if file_path.suffix[1:] not in extensions:
|
|
||||||
continue
|
|
||||||
|
|
||||||
process_file(file_path)
|
|
||||||
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("path", type=Path, help="Path to the song file or directory")
|
parser.add_argument("path", type=Path, help="Path to the song file or directory")
|
||||||
|
parser.add_argument("-c", "--config", type=Path, help="Config file for lyrics-dl")
|
||||||
parser.add_argument("-e", "--extensions", type=str, help="Music files extensions, separated by a comma. For example: wav,flac,mp3")
|
parser.add_argument("-e", "--extensions", type=str, help="Music files extensions, separated by a comma. For example: wav,flac,mp3")
|
||||||
parser.add_argument("-f", "--force-override", action="store_true", help="Force override .lrc file, if it already exists")
|
parser.add_argument("-f", "--force-override", action="store_true", help="Force override .lrc file, if it already exists")
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
logger = DefaultLogger()
|
||||||
|
|
||||||
|
if args.config:
|
||||||
|
config = LyricsDlConfig.from_file(args.config)
|
||||||
|
else:
|
||||||
|
config = LyricsDlConfig()
|
||||||
|
|
||||||
|
lyrics_dl = LyricsDl(config=config, logger=logger)
|
||||||
|
|
||||||
|
|
||||||
if args.path.is_dir():
|
if args.path.is_dir():
|
||||||
if not args.extensions:
|
if not args.extensions:
|
||||||
extensions = ["flac", "alac", "mp3", "m4a", "mp4", "aac", "wav", "opus", "ogg"]
|
extensions = ["flac", "alac", "mp3", "m4a", "mp4", "aac", "wav", "opus", "ogg"]
|
||||||
else:
|
else:
|
||||||
extensions = args.extensions.split(",")
|
extensions = args.extensions.split(",")
|
||||||
|
|
||||||
process_directory(args.path, extensions)
|
lyrics_dl.process_directory(args.path, extensions, args.force_override)
|
||||||
else:
|
else:
|
||||||
process_file(args.path)
|
lyrics_dl.process_file(args.path, args.force_override)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue