Skip to content

Commit

Permalink
Improve the listen history JSON format a lot
Browse files Browse the repository at this point in the history
  • Loading branch information
Pixaurora committed Nov 8, 2024
1 parent 50cf230 commit 983c854
Show file tree
Hide file tree
Showing 21 changed files with 306 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

import net.pixaurora.kit_tunes.api.music.ListeningProgress;
import net.pixaurora.kit_tunes.api.music.Track;
import net.pixaurora.kit_tunes.api.music.history.ListenRecord;
import net.pixaurora.kit_tunes.api.resource.ResourcePath;

public interface TrackEndEvent {
public Optional<Track> track();
public Optional<Track> track();

public ResourcePath searchPath();
public Optional<ListenRecord> record();

public ListeningProgress progress();
public ResourcePath searchPath();

public ListeningProgress progress();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

import net.pixaurora.kit_tunes.api.music.ListeningProgress;
import net.pixaurora.kit_tunes.api.music.Track;
import net.pixaurora.kit_tunes.api.music.history.ListenRecord;
import net.pixaurora.kit_tunes.api.resource.ResourcePath;

public interface TrackMiddleEvent {
public Optional<Track> track();

public Optional<ListenRecord> record();

public ResourcePath searchPath();

public ListeningProgress progress();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

import net.pixaurora.kit_tunes.api.music.ListeningProgress;
import net.pixaurora.kit_tunes.api.music.Track;
import net.pixaurora.kit_tunes.api.music.history.ListenRecord;
import net.pixaurora.kit_tunes.api.resource.ResourcePath;

public interface TrackStartEvent {
public Optional<Track> track();
public Optional<Track> track();

public ResourcePath searchPath();
public Optional<ListenRecord> record();

public ListeningProgress progress();
public ResourcePath searchPath();

public ListeningProgress progress();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.pixaurora.kit_tunes.api.music.history;

import java.time.Duration;

public interface ListenDurations {
public Duration progress();

public Duration full();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package net.pixaurora.kit_tunes.api.music.history;

import java.time.Instant;
import java.util.List;
import java.util.Optional;

import net.pixaurora.kit_tunes.api.music.Album;
import net.pixaurora.kit_tunes.api.music.Track;
import net.pixaurora.kit_tunes.api.scrobble.ScrobblerId;

public class ListenRecord {
private final Track track;
private final Optional<Album> album;
private final Instant timestamp;

private final ListenDurations durations;

private final List<ScrobblerId> succeededScrobblers;

public ListenRecord(Track track, Optional<Album> album, Instant timestamp, ListenDurations durations,
List<ScrobblerId> succeededScrobblers) {
this.track = track;
this.album = album;
this.timestamp = timestamp;
this.durations = durations;
this.succeededScrobblers = succeededScrobblers;
}

public Track track() {
return this.track;
}

public Optional<Album> album() {
return this.album;
}

public Instant timestamp() {
return this.timestamp;
}

public ListenDurations durations() {
return this.durations;
}

public List<ScrobblerId> succeededScrobblers() {
return this.succeededScrobblers;
}

public void succeededFor(ScrobblerId scrobbler) {
this.succeededScrobblers.add(scrobbler);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.pixaurora.kitten_heart.impl.scrobble;
package net.pixaurora.kit_tunes.api.scrobble;

public class ScrobblerId {
private final String username;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static void handleTrackEnd(ListeningProgress progress) {
}

private static void handleTrackEnd(PlayingSong song) {
TrackEndEvent event = new TrackEventImpl(song.path(), song.track(), song.progress());
TrackEndEvent event = new TrackEventImpl(song);

processEvent(listener -> listener.onTrackEnd(event));
}
Expand All @@ -75,7 +75,7 @@ public static void tick() {

if (song.canSendMiddleEvent()) {
processEvent(listener -> listener
.onTrackMiddleReached(new TrackEventImpl(song.path(), song.track(), song.progress())));
.onTrackMiddleReached(new TrackEventImpl(song)));
}
}
}
Expand Down Expand Up @@ -123,11 +123,13 @@ private static TrackStartEvent createStartEvent(ResourcePath path, ListeningProg
MusicMetadata.asMutable().giveDuration(track.get(), songDuration);
}

PlayingSong song = new PlayingSong(path, track, progress, controls);

synchronized (PLAYING_TRACKS) {
PLAYING_TRACKS.put(progress, new PlayingSong(path, track, progress, controls));
PLAYING_TRACKS.put(progress, song);
}

return new TrackEventImpl(path, track, progress);
return new TrackEventImpl(song);
}

private static Duration songDuration(ResourcePath path) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import com.google.gson.JsonSerializationContext;

import net.pixaurora.catculator.api.http.Client;
import net.pixaurora.kit_tunes.api.music.history.ListenRecord;
import net.pixaurora.kitten_heart.impl.KitTunes;
import net.pixaurora.kitten_heart.impl.error.KitTunesException;
import net.pixaurora.kitten_heart.impl.music.history.ListenRecord;
import net.pixaurora.kitten_heart.impl.scrobble.SimpleScrobbler;
import net.pixaurora.kitten_heart.impl.scrobble.scrobbler.Scrobbler;

Expand Down Expand Up @@ -46,7 +46,7 @@ public void startScrobbling(Client client, ListenRecord track) {
public void completeScrobbling(Client client, ListenRecord track) {
this.handleScrobbling(scrobbler -> {
scrobbler.completeScrobbling(client, track);
track.succeededFor(scrobbler);
track.succeededFor(scrobbler.id());
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;

import net.pixaurora.kit_tunes.api.music.history.ListenRecord;
import net.pixaurora.kit_tunes.api.resource.ResourcePath;
import net.pixaurora.kit_tunes.api.scrobble.ScrobblerId;
import net.pixaurora.kitten_heart.impl.music.AlbumImpl;
import net.pixaurora.kitten_heart.impl.music.ArtistImpl;
import net.pixaurora.kitten_heart.impl.music.TrackImpl;
import net.pixaurora.kitten_heart.impl.music.history.ListenRecord;
import net.pixaurora.kitten_heart.impl.music.history.ListenRecordSerializer;
import net.pixaurora.kitten_heart.impl.resource.ResourcePathImpl;
import net.pixaurora.kitten_heart.impl.scrobble.ScrobblerIdSerializer;
import net.pixaurora.kitten_heart.impl.scrobble.scrobbler.Scrobbler;

public class Serialization {
Expand All @@ -33,7 +36,8 @@ private static final Gson createSerializer() {
.registerTypeAdapter(TrackImpl.TransformsToTrack.class, new TrackImpl.TransformsToTrack.Serializer())
.registerTypeAdapter(TrackImpl.FromData.class, new TrackImpl.FromData.Serializer())
.registerTypeAdapter(TrackImpl.FromPath.class, TrackImpl.FromPath.SERIALIZER)
.registerTypeAdapter(ListenRecord.class, new ListenRecord.Serializer())
.registerTypeAdapter(ListenRecord.class, new ListenRecordSerializer())
.registerTypeAdapter(ScrobblerId.class, new ScrobblerIdSerializer())
.registerTypeAdapter(Duration.class, new DurationSerializer())
.registerTypeAdapter(Instant.class, new InstantSerializer())
.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,26 @@
import net.pixaurora.kit_tunes.api.event.TrackStartEvent;
import net.pixaurora.kit_tunes.api.music.ListeningProgress;
import net.pixaurora.kit_tunes.api.music.Track;
import net.pixaurora.kit_tunes.api.music.history.ListenRecord;
import net.pixaurora.kit_tunes.api.resource.ResourcePath;
import net.pixaurora.kitten_heart.impl.music.progress.PlayingSong;

public class TrackEventImpl implements TrackStartEvent, TrackMiddleEvent, TrackEndEvent {
private final ResourcePath path;
private final Optional<Track> track;
private final Optional<ListenRecord> record;
private final ListeningProgress progress;

public TrackEventImpl(ResourcePath path, Optional<Track> track, ListeningProgress progress) {
public TrackEventImpl(ResourcePath path, Optional<Track> track, Optional<ListenRecord> record,
ListeningProgress progress) {
this.path = path;
this.track = track;
this.progress = progress;
this.record = record;
}

public TrackEventImpl(PlayingSong song) {
this(song.path(), song.track(), song.record(), song.progress());
}

@Override
Expand All @@ -34,4 +43,9 @@ public ResourcePath searchPath() {
public ListeningProgress progress() {
return this.progress;
}

@Override
public Optional<ListenRecord> record() {
return this.record;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,41 @@
import net.pixaurora.kit_tunes.api.event.TrackMiddleEvent;
import net.pixaurora.kit_tunes.api.event.TrackStartEvent;
import net.pixaurora.kit_tunes.api.listener.MusicEventListener;
import net.pixaurora.kit_tunes.api.music.ListeningProgress;
import net.pixaurora.kit_tunes.api.music.Track;
import net.pixaurora.kit_tunes.api.music.history.ListenRecord;
import net.pixaurora.kitten_heart.impl.KitTunes;
import net.pixaurora.kitten_heart.impl.music.history.ListenRecord;

public class ScrobblingMusicListener implements MusicEventListener {
@Override
public void onTrackStart(TrackStartEvent event) {
if (!event.track().isPresent()) {
if (!event.record().isPresent()) {
return;
}

Track track = event.track().get();
ListeningProgress progress = event.progress();

ListenRecord record = new ListenRecord(track, progress);
ListenRecord record = event.record().get();

KitTunes.SCROBBLER_CACHE.execute(
scrobblers -> scrobblers.startScrobbling(KitTunes.CLIENT, record));
}

@Override
public void onTrackMiddleReached(TrackMiddleEvent event) {
if (!event.track().isPresent()) {
if (!event.record().isPresent()) {
return;
}

ListenRecord record = new ListenRecord(event.track().get(), event.progress());
ListenRecord record = event.record().get();

KitTunes.SCROBBLER_CACHE
.execute(scrobblers -> scrobblers.completeScrobbling(KitTunes.CLIENT, record));
}

@Override
public void onTrackEnd(TrackEndEvent event) {
if (!event.track().isPresent()) {
if (!event.record().isPresent()) {
return;
}

ListenRecord record = new ListenRecord(event.track().get(), event.progress());
ListenRecord record = event.record().get();

KitTunes.LISTEN_HISTORY.execute(history -> history.record(record));
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package net.pixaurora.kitten_heart.impl.music.history;

import java.time.Duration;

import net.pixaurora.kit_tunes.api.music.history.ListenDurations;

public class ImmutableListenDurations implements ListenDurations {
private final Duration progress;
private final Duration full;

public ImmutableListenDurations(Duration progress, Duration full) {
this.progress = progress;
this.full = full;
}

@Override
public Duration progress() {
return this.progress;
}

@Override
public Duration full() {
return this.full;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
import java.util.List;
import java.util.function.Predicate;

import com.google.gson.annotations.SerializedName;

import net.pixaurora.kit_tunes.api.music.history.ListenRecord;

public class ListenHistory {
private final List<ListenRecord> history;
private Duration timespanStored;
@SerializedName("max_retention")
private Duration maxRetention;

public ListenHistory(Duration timespanStored, List<ListenRecord> history) {
this.timespanStored = timespanStored;
public ListenHistory(Duration maxRetention, List<ListenRecord> history) {
this.maxRetention = maxRetention;
this.history = history;
}

Expand All @@ -23,12 +28,12 @@ public List<ListenRecord> getHistory() {
return history;
}

public Duration timespanStored() {
return timespanStored;
public Duration maxRetention() {
return this.maxRetention;
}

public void timespanStored(Duration newValue) {
this.timespanStored = newValue;
this.maxRetention = newValue;
this.clearOldHistory();
}

Expand All @@ -40,7 +45,7 @@ public void record(ListenRecord record) {
private void clearOldHistory() {
Predicate<ListenRecord> isTooOld = record -> Duration
.between(record.timestamp(), Instant.now())
.compareTo(this.timespanStored) > 0;
.compareTo(this.maxRetention) > 0;

this.history.removeIf(isTooOld);
}
Expand Down
Loading

0 comments on commit 983c854

Please sign in to comment.