Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow adding duplicate scrobblers #19

Merged
Merged
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
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