Skip to content

Commit

Permalink
0.3.3: fix scrobble with stopped playback, some refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
dbeley committed Nov 20, 2023
1 parent c9edca5 commit 729fa27
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion mpdscrobble/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
A simple Last.fm scrobbler for MPD.
"""

__version__ = "0.3.2"
__version__ = "0.3.3"

name = "mpdscrobble"
13 changes: 5 additions & 8 deletions mpdscrobble/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .utils import (
read_config,
create_networks,
get_mpdscrobble_config
)
from .mpdscrobble import MPDScrobbleMPDConnection, MPDScrobbleTrack, MPDScrobbleNetwork

Expand All @@ -28,18 +29,20 @@ def loop(
current_song = client.mpdscrobble_currentsong()
if current_song and cached_song:
logger.debug(
f"Current: {current_song.debug()}\nCached: {cached_song.debug()}"
"Current: %s\nCached: %s", current_song.debug(), cached_song.debug()
)
if cached_song != current_song:
if cached_song.percentage > constants.SCROBBLE_PERCENTAGE:
if not args.dry_run:
logger.info("Sending scrobble for %s.", current_song)
networks.mpdscrobble_scrobble(cached_song)
else:
logger.warning("Dry-run mode enabled.")
networks.mpdscrobble_update_now_playing(current_song)
if not current_song and cached_song:
if cached_song.percentage > constants.SCROBBLE_PERCENTAGE:
if not args.dry_run:
logger.info("Sending scrobble for %s. Detected stopped playback.", current_song)
networks.mpdscrobble_scrobble(cached_song)
else:
logger.warning("Dry-run mode enabled")
Expand All @@ -58,13 +61,7 @@ def main():

networks = create_networks(config)

host = constants.DEFAULT_HOST
port = constants.DEFAULT_PORT
if "mpdscrobble" in config:
if "host" in config["mpdscrobble"]:
host = config["mpdscrobble"]["host"]
if "port" in config["mpdscrobble"]:
port = config["mpdscrobble"]["port"]
host, port = get_mpdscrobble_config(config)

client = MPDScrobbleMPDConnection()
client.mpdscrobble_connect(host, port)
Expand Down
12 changes: 12 additions & 0 deletions mpdscrobble/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import configparser
import os

from mpdscrobble import constants
from .mpdscrobble import (
MPDScrobbleLastFMNetwork,
MPDScrobbleMalojaNetwork,
Expand All @@ -19,6 +20,17 @@ def read_config(config_file: str) -> configparser.ConfigParser:
return config


def get_mpdscrobble_config(config: configparser.ConfigParser) -> tuple[str, int]:
host = constants.DEFAULT_HOST
port = constants.DEFAULT_PORT
if "mpdscrobble" in config:
if "host" in config["mpdscrobble"]:
host = config["mpdscrobble"]["host"]
if "port" in config["mpdscrobble"]:
port = int(config["mpdscrobble"]["port"])
return host, port


def create_networks(config: configparser.ConfigParser) -> MPDScrobbleNetwork:
networks = []
for i in config.sections():
Expand Down

0 comments on commit 729fa27

Please sign in to comment.