Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 35 additions & 20 deletions lyrics2mp3/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import taglib
from rich.progress import Progress

import os

Expand Down Expand Up @@ -132,13 +133,21 @@ def get_lyrics_from_dir(self, dir, ignore_artist=False, simulate=False):
if not (dir is None or os.path.isdir(dir)):
raise LookupError(f'Directory "{dir}" not found.')

for dir_path, _, files in os.walk(dir):
for file in files:
self.file_lyrics(
os.path.join(dir_path, file),
ignore_artist=ignore_artist,
simulate=simulate,
)
total_files = sum(len(files) for _, _, files in os.walk(dir))

with Progress() as progress:
task = progress.add_task("Processing files", total=total_files)

for dir_path, _, files in os.walk(dir):
for file in files:
self.file_lyrics(
os.path.join(dir_path, file),
ignore_artist=ignore_artist,
simulate=simulate,
progress=progress,
task=task,
)
progress.update(task, advance=1)

def get_lyrics_from_m3u(self, m3u, ignore_artist=False, simulate=False):
if not os.path.isfile(m3u):
Expand All @@ -147,28 +156,34 @@ def get_lyrics_from_m3u(self, m3u, ignore_artist=False, simulate=False):
if not m3u.endswith(".m3u"):
raise SyntaxError(f'Playlist "{m3u}" is not M3U format.')

with open(m3u) as m:
for line in m:
line = line.strip()
if line.upper().startswith("#EXT"):
continue

self.file_lyrics(line, ignore_artist=ignore_artist, simulate=simulate)

def file_lyrics(self, file_path, ignore_artist=False, simulate=False):
with Progress() as progress:
with open(m3u) as m:
total_lines = sum(1 for line in m if not line.upper().startswith("#EXT"))
m.seek(0)
task = progress.add_task("Processing files", total=total_lines)

for line in m:
line = line.strip()
if line.upper().startswith("#EXT"):
continue
self.file_lyrics(line, ignore_artist=ignore_artist, simulate=simulate,
progress=progress, task=task)
progress.update(task, advance=1)

def file_lyrics(self, file_path, ignore_artist=False, simulate=False, progress=None, task=None):
self.parse_file(file_path, ignore_artist=ignore_artist, simulate=simulate)
if not self.verbose:
self.report_progress(inline=True)
self.report_progress(progress=progress, task=task)

def report_progress(self, inline=False):
def report_progress(self, progress=None, task=None):
l_sum = self.have_lyrics + self.added_lyrics + self.no_lyrics_found + self.err
txt = f"{self.added_lyrics} added, {self.no_lyrics_found} not found, {self.err} errored."
if not self.overwrite:
txt = f"{self.have_lyrics} existing, {txt}"

txt = f"{l_sum} processed: {txt}"
if inline:
print(f"\r{txt}", end="")
if progress:
progress.update(task, description=txt)
else:
print(txt)

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
beautifulsoup4
requests>=2
pytaglib
rich
lyricsgenius>=3.0.1,<4.0