From 6328348b22e2911483ce815a22acecafa4f4e04e Mon Sep 17 00:00:00 2001 From: aj Date: Fri, 17 May 2019 15:30:39 +0100 Subject: [PATCH] implemented scrobbling --- .../sarsoo/fmframework/fm/FmAuthNetwork.java | 4 +- .../fx/controller/RootController.java | 4 + .../fx/controller/ScrobblePaneController.java | 141 ++++++++++++++++-- .../sarsoo/fmframework/music/Scrobble.java | 4 + .../fmframework/fx/ui/ScrobblePane.fxml | 14 +- 5 files changed, 154 insertions(+), 13 deletions(-) diff --git a/src/main/java/sarsoo/fmframework/fm/FmAuthNetwork.java b/src/main/java/sarsoo/fmframework/fm/FmAuthNetwork.java index 5b4ae0c..161951b 100644 --- a/src/main/java/sarsoo/fmframework/fm/FmAuthNetwork.java +++ b/src/main/java/sarsoo/fmframework/fm/FmAuthNetwork.java @@ -32,7 +32,7 @@ public class FmAuthNetwork extends FmUserNetwork { this.secretKey = secretKey; } - public void scrobble(Scrobble scrobble, String sk) { + public JSONObject scrobble(Scrobble scrobble, String sk) { Logger.getLog().log(new LogEntry("scrobble").addArg(scrobble.toString())); @@ -50,6 +50,8 @@ public class FmAuthNetwork extends FmUserNetwork { JSONObject obj = makeAuthPostRequest("track.scrobble", params); + return obj; + } public String getToken() { diff --git a/src/main/java/sarsoo/fmframework/fx/controller/RootController.java b/src/main/java/sarsoo/fmframework/fx/controller/RootController.java index d16af4b..bf984ea 100644 --- a/src/main/java/sarsoo/fmframework/fx/controller/RootController.java +++ b/src/main/java/sarsoo/fmframework/fx/controller/RootController.java @@ -204,6 +204,10 @@ public class RootController { @FXML protected void handleAuth(ActionEvent event) { + authenticate(); + } + + public void authenticate() { try { Config config = FmFramework.getSessionConfig(); diff --git a/src/main/java/sarsoo/fmframework/fx/controller/ScrobblePaneController.java b/src/main/java/sarsoo/fmframework/fx/controller/ScrobblePaneController.java index 0670004..1289871 100644 --- a/src/main/java/sarsoo/fmframework/fx/controller/ScrobblePaneController.java +++ b/src/main/java/sarsoo/fmframework/fx/controller/ScrobblePaneController.java @@ -1,52 +1,171 @@ package sarsoo.fmframework.fx.controller; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.ZoneId; + +import org.json.JSONObject; + import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.DatePicker; import javafx.scene.control.Label; import javafx.scene.control.Slider; import javafx.scene.control.TextField; +import sarsoo.fmframework.config.Config; +import sarsoo.fmframework.fm.FmAuthNetwork; +import sarsoo.fmframework.fx.FmFramework; +import sarsoo.fmframework.music.Artist; +import sarsoo.fmframework.music.Artist.ArtistBuilder; +import sarsoo.fmframework.music.Scrobble; +import sarsoo.fmframework.music.Track; +import sarsoo.fmframework.music.Track.TrackBuilder; +import sarsoo.fmframework.music.Album.AlbumBuilder; public class ScrobblePaneController { - + @FXML private TextField textTrack; - + @FXML private TextField textAlbum; - + @FXML private TextField textArtist; - + @FXML private TextField textAlbumArtist; - + @FXML private Slider sliderHour; - + @FXML private Slider sliderMinute; - + @FXML private Slider sliderSecond; - + @FXML private Label labelStatus; - + @FXML private DatePicker datePicker; - + @FXML private Button buttonScrobble; @FXML public void initialize() { + Config config = FmFramework.getSessionConfig(); + + if (config.getValue("session_key") == null) { + FmFramework.getController().authenticate(); + } + } @FXML - private void handleScrobble() { + private void handleClear() { + textTrack.setText(""); + textAlbum.setText(""); + textArtist.setText(""); + textAlbumArtist.setText(""); + sliderHour.setValue(0); + sliderMinute.setValue(0); + sliderSecond.setValue(0); + + datePicker.setValue(null); + } + + @FXML + private void handleScrobble() { + if (validateText()) { + if (validateDate()) { + Config config = FmFramework.getSessionConfig(); + + if (config.getValue("session_key") != null) { + FmAuthNetwork net = new FmAuthNetwork(config.getValue("api_key"), + config.getValue("api_secret"), + config.getValue("username")); + + Artist artist = new ArtistBuilder(textArtist.getText()).build(); + Track track = new TrackBuilder(textTrack.getText(), artist).build(); + + Scrobble scrobble = new Scrobble(getEpochDate(), track); + + if(textAlbum.getText().length() > 0) { + Artist albumArtist = new ArtistBuilder(textAlbumArtist.getText()).build(); + scrobble.setAlbum(new AlbumBuilder(textAlbum.getText(), albumArtist).build()); + } + + JSONObject obj = net.scrobble(scrobble, config.getValue("session_key")); + + if(obj.getJSONObject("scrobbles").getJSONObject("@attr").getInt("accepted") == 1) { + labelStatus.setText("sent!"); + }else { + labelStatus.setText("failed"); + } + + }else { + labelStatus.setText("unauthorized"); + } + } else { + labelStatus.setText("wrong date"); + } + } else { + labelStatus.setText("wrong text"); + } + } + + private boolean validateText() { + String track = textTrack.getText(); + String album = textAlbum.getText(); + String artist = textArtist.getText(); + String albumArtist = textAlbumArtist.getText(); + + if (track.length() > 0 && artist.length() > 0) { + if (album.length() > 0 ^ albumArtist.length() > 0) { + return false; + } else { + return true; + } + } + + return false; + } + + private boolean validateDate() { + if (datePicker.getValue() != null) { + + LocalDateTime now = LocalDateTime.now(); + LocalDateTime selected = getLocalDateTime(); + + if (selected.isAfter(now.minusWeeks(2))) { + if (selected.isBefore(now)) { + return true; + } + return false; + } + return false; + } + return false; + } + + public LocalDateTime getLocalDateTime() { + + LocalDate date = datePicker.getValue(); + LocalTime time = LocalTime.of((int) sliderHour.getValue(), (int) sliderMinute.getValue(), + (int) sliderSecond.getValue()); + + return LocalDateTime.of(date, time); + } + + public long getEpochDate() { + ZoneId zone = ZoneId.systemDefault(); + return getLocalDateTime().atZone(zone).toEpochSecond(); } } \ No newline at end of file diff --git a/src/main/java/sarsoo/fmframework/music/Scrobble.java b/src/main/java/sarsoo/fmframework/music/Scrobble.java index 4315af8..53edb43 100644 --- a/src/main/java/sarsoo/fmframework/music/Scrobble.java +++ b/src/main/java/sarsoo/fmframework/music/Scrobble.java @@ -39,6 +39,10 @@ public class Scrobble { return album; } + public void setAlbum(Album album) { + this.album = album; + } + public Artist getArtist() { return track.getArtist(); } diff --git a/src/main/resources/sarsoo/fmframework/fx/ui/ScrobblePane.fxml b/src/main/resources/sarsoo/fmframework/fx/ui/ScrobblePane.fxml index e5df7ae..088bbd7 100644 --- a/src/main/resources/sarsoo/fmframework/fx/ui/ScrobblePane.fxml +++ b/src/main/resources/sarsoo/fmframework/fx/ui/ScrobblePane.fxml @@ -48,8 +48,20 @@