Skip to content

Commit

Permalink
Disallow adding duplicate scrobblers
Browse files Browse the repository at this point in the history
Signed-off-by: Lilly Rose Berner <[email protected]>
  • Loading branch information
LostLuma committed Feb 18, 2025
1 parent d0871f3 commit b28bcba
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.concurrent.Executors;

import net.pixaurora.catculator.api.http.Client;
import net.pixaurora.kitten_heart.impl.scrobble.scrobbler.Scrobbler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -75,6 +76,11 @@ public static void stop() {
CLIENT.close();
}

public static void addScrobbler(Scrobbler scrobbler) throws IOException {
KitTunes.SCROBBLER_CACHE.execute(scrobblers -> scrobblers.addScrobbler(scrobbler));
KitTunes.SCROBBLER_CACHE.save();

}
private static String buildUserAgent() {
return "Kit Tunes/" + Constants.MOD_VERSION + " (+" + Constants.HOMEPAGE + ")";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public List<Scrobbler> scrobblers() {
}

public void addScrobbler(Scrobbler scrobbler) {
this.scrobblers.removeIf(item -> item.equals(scrobbler));
this.scrobblers.add(scrobbler);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import net.pixaurora.kitten_heart.impl.network.XMLHelper;
import net.pixaurora.kitten_heart.impl.scrobble.ScrobblerType;

public class LastFMScrobbler implements Scrobbler {
public class LastFMScrobbler extends Scrobbler {
public static final String API_KEY = "6f9e533b5f6631a5aa3070f5e757de8c";
public static final String SHARED_SECRET = "97fbf9a3d76ba36dfb5a2f6c3215bf49";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import net.pixaurora.kit_tunes.api.music.history.ListenRecord;
import net.pixaurora.kit_tunes.api.scrobble.ScrobblerId;
import net.pixaurora.kitten_heart.impl.Constants;
import net.pixaurora.kitten_heart.impl.KitTunes;
import net.pixaurora.kitten_heart.impl.config.Serialization;
import net.pixaurora.kitten_heart.impl.config.dispatch.DispatchType;
import net.pixaurora.kitten_heart.impl.error.KitTunesException;
Expand All @@ -24,7 +23,7 @@
import java.util.Objects;
import java.util.Optional;

public class ListenBrainzScrobbler implements Scrobbler {
public class ListenBrainzScrobbler extends Scrobbler {
private final Session session;

public static final String DEFAULT_INSTANCE_URL = "https://api.listenbrainz.org";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
import net.pixaurora.kitten_heart.impl.scrobble.ScrobblerType;
import net.pixaurora.kitten_heart.impl.scrobble.SimpleScrobbler;

public interface Scrobbler extends SimpleScrobbler, SpecifiesType<Scrobbler> {
public abstract class Scrobbler implements SimpleScrobbler, SpecifiesType<Scrobbler> {
public static final DispatchGroup<Scrobbler, ScrobblerType<? extends Scrobbler>> TYPES = new DispatchGroup<>(
"scrobbler", Arrays.asList(LastFMScrobbler.TYPE, LegacyLastFMScrobbler.TYPE, ListenBrainzScrobbler.TYPE));

public String username();
public abstract String username();

public default ScrobblerId id() {
public ScrobblerId id() {
return new ScrobblerId(this.username(), this.type().name());
}

@Override
public boolean equals(Object other) {
return other instanceof Scrobbler && this.id().equals(((Scrobbler) other).id());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected void firstInit() {
try {
ListenBrainzScrobbler scrobbler = ListenBrainzScrobbler.fromToken(KitTunes.CLIENT, instanceUrl, authorizationToken);

this.saveScrobbler(scrobbler);
KitTunes.addScrobbler(scrobbler);
this.setStatus(SUCCESS, true);
} catch (KitTunesException | IOException e) {
this.setStatus(FAILURE, false);
Expand All @@ -95,11 +95,6 @@ private void setStatus(Component component, boolean success) {
}
}

public void saveScrobbler(ListenBrainzScrobbler scrobbler) throws IOException {
KitTunes.SCROBBLER_CACHE.execute(scrobblers -> scrobblers.addScrobbler(scrobbler));
KitTunes.SCROBBLER_CACHE.save();
}

private WidgetContainer<PushableTextLines> addComponentBox(Component component, @Nullable WidgetContainer<?> previous) {
Point offset = Point.of(0, 10);
WidgetContainer<PushableTextLines> widget = this.addWidget(PushableTextLines.regular());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,6 @@ public void onExit() {
super.onExit();
}

public void saveScrobbler(T scrobbler) throws IOException {
KitTunes.SCROBBLER_CACHE.execute(scrobblers -> scrobblers.addScrobbler(scrobbler));
KitTunes.SCROBBLER_CACHE.save();
}

@Override
public void tick() {
if (this.awaitedScrobbler.isPresent()) {
Expand All @@ -133,8 +128,8 @@ public void tick() {
if (awaitedScrobbler.isComplete()) {
try {
T scrobbler = awaitedScrobbler.get();
this.saveScrobbler(scrobbler);

KitTunes.addScrobbler(scrobbler);
this.setMessage(SETUP_COMPLETED);
} catch (ExecutionException | InterruptedException | IOException e) {
this.sendError(KitTunesException.convert(e));
Expand Down

0 comments on commit b28bcba

Please sign in to comment.