Last active
September 28, 2021 07:30
-
-
Save michael-simons/c2fb92c387b2a7c7300ff686bac88177 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///usr/bin/env jbang "$0" "$@" ; exit $? | |
//JAVA 17 | |
//DEPS com.tagtraum:obstmusic:0.9.2 | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.TimeUnit; | |
import com.tagtraum.macos.music.Application; | |
import com.tagtraum.macos.music.Epls; | |
public class WhatsPlaying { | |
static Integer previousTrackId; | |
public static void main(String... args) { | |
var application = Application.getInstance(); | |
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> { | |
if (application.getPlayerState() != Epls.PLAYING) { | |
return; | |
} | |
var trackHandle = application.getCurrentTrack(); | |
var currentTrackId = Integer.valueOf(trackHandle.getId()); | |
if (currentTrackId.equals(previousTrackId) | |
|| application.getPlayerPosition() < trackHandle.getDuration() / 2.0) { | |
return; | |
} | |
var track = new Track( | |
new Artist(trackHandle.getArtist()), | |
new Genre(trackHandle.getArtist()), | |
trackHandle.getName(), | |
trackHandle.getAlbum(), | |
trackHandle.getYear(), | |
new TrackNumber(trackHandle.getTrackNumber(), trackHandle.getTrackCount()), | |
new DiscNumber(trackHandle.getDiscNumber(), trackHandle.getDiscCount()), | |
trackHandle.getRating(), | |
trackHandle.getComment(), | |
trackHandle.isCompilation(), | |
trackHandle.getDuration() | |
); | |
System.out.println("Played " + track); | |
// Instead why not sent it to a webservice? | |
previousTrackId = trackHandle.getId(); | |
}, 0, 5, TimeUnit.SECONDS); | |
} | |
} | |
record Artist(String name) { | |
} | |
record Genre(String name) { | |
} | |
record TrackNumber(int value, int total) { | |
} | |
record DiscNumber(int value, int total) { | |
} | |
record Track(Artist artist, Genre genre, String name, String album, int year, TrackNumber tracknumber, | |
DiscNumber discNumber, int rating, String comment, boolean compilation, double duration) { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment