Skip to content

Commit

Permalink
Track choice WIP.
Browse files Browse the repository at this point in the history
  • Loading branch information
sheriferson committed Nov 21, 2023
1 parent e8c9592 commit d16a6cf
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 11 deletions.
29 changes: 23 additions & 6 deletions src/scrobble/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from scrobble.lastfm import get_lastfm_client
from scrobble.musicbrainz import CD, UserAgent, init_musicbrainz
from scrobble.pushover import send_notification
from scrobble.utils import prepare_tracks
from scrobble.utils import prepare_tracks, choose_tracks

import importlib.metadata

Expand All @@ -19,6 +19,13 @@

APP = typer.Typer()

@APP.command()
def musicbrainz():
raise NotImplementedError('Scrobbling a MusicBrainz release is not implemented yet.')

@APP.command()
def discogs():
raise NotImplementedError('Scrobbling a Discogs release is not implemented yet.')

@APP.command()
def cd(
Expand All @@ -38,20 +45,30 @@ def cd(
notify: Annotated[bool, typer.Option(
help='--notify will send a push notification via Pushover with CD information.'
)] = False,
choice: Annotated[bool, typer.Option(
help='--choice will give you a list of options of more than one CD is matched. '
release_choice: Annotated[bool, typer.Option(
help='--release-choice will give you a list of options of more than one CD is matched. '
'Otherwise, the app will go with the first match.'
)] = True,
track_choice: Annotated[bool, typer.Option(
help='--track-choice will give you a list of tracks in the release to choose to scrobble '
'instead of scrobbling the entire release.'
)] = False,
):

init_musicbrainz(USERAGENT)

cd = CD.find_cd(barcode, choice)
cd = CD.find_cd(barcode, release_choice)

if track_choice:
tracks_to_scrobble = choose_tracks(cd.tracks)
else:
tracks_to_scrobble = cd.tracks

prepped_tracks = prepare_tracks(cd, tracks_to_scrobble, playbackend)

prepped_tracks = prepare_tracks(cd, playbackend)
if verbose:
print(cd)
for track in cd.tracks:
for track in tracks_to_scrobble:
print(track)

if not dryrun:
Expand Down
35 changes: 32 additions & 3 deletions src/scrobble/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import tomllib
from dataclasses import dataclass
from datetime import datetime
import subprocess
from typing import Optional

from scrobble.musicbrainz import CD
from scrobble.musicbrainz import Track


@dataclass
Expand Down Expand Up @@ -63,9 +65,9 @@ def read_api_keys(self, config_path: str) -> dict:
return keys


def prepare_tracks(cd: CD, playbackend: str = 'now') -> list[dict]:
def prepare_tracks(cd: CD, tracks: list[Track], playbackend: str = 'now') -> list[dict]:
total_run_time: int = 0
for track in cd.tracks:
for track in tracks:
total_run_time += track.track_length

if playbackend != 'now':
Expand All @@ -84,7 +86,7 @@ def prepare_tracks(cd: CD, playbackend: str = 'now') -> list[dict]:
elapsed: int = 0

prepped_tracks = []
for track in cd.tracks:
for track in tracks:
elapsed += track.track_length
prepped_tracks.append(
{
Expand All @@ -96,3 +98,30 @@ def prepare_tracks(cd: CD, playbackend: str = 'now') -> list[dict]:
)

return prepped_tracks

def find_command(command: str):
try:
command_check = subprocess.check_output(
f'which {command}',
shell=True,
encoding='UTF-8'
).rstrip()
except subprocess.CalledProcessError:
command_check = None

return command_check


def choose_tracks(tracks: list[Track]) -> list[Track]:
gum_path = find_command('gum')
if gum_path:
track_dict: dict = {str(track): track for track in tracks}
choices = ' '.join(['"'+track_str+'"' for track_str in track_dict.keys()])
picked_tracks = subprocess.check_output(
f"{gum_path} choose {choices} --no-limit",
shell=True,
encoding='UTF-8').rstrip()
else:
raise NotImplementedError("Haven't implemented basic choosing yet")

return [track for track in tracks if str(track) in picked_tracks]
12 changes: 10 additions & 2 deletions tests/test__utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from scrobble.utils import Config
from scrobble.utils import Config, find_command

def test_valid_config_has_lastfm_api():
test_config = Config(config_path='tests/resources/scrobble_complete_valid.toml')
Expand All @@ -15,4 +15,12 @@ def test_valid_config_lastfm_api_key():

def test_valid_config_lastfm_username():
test_config = Config(config_path='tests/resources/scrobble_complete_valid.toml')
assert test_config.lastfm_username == 'thespeckofme'
assert test_config.lastfm_username == 'thespeckofme'

def test_find_command_succeeding():
command_check = find_command('ls')
assert command_check is not None

def test_find_command_returning_none():
bad_command_check = find_command('badfakecommand')
assert bad_command_check is None

0 comments on commit d16a6cf

Please sign in to comment.