implemented scrobbling

This commit is contained in:
aj 2019-05-17 15:30:39 +01:00
parent 43ea2868eb
commit 6328348b22
5 changed files with 154 additions and 13 deletions

View File

@ -32,7 +32,7 @@ public class FmAuthNetwork extends FmUserNetwork {
this.secretKey = secretKey; 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())); Logger.getLog().log(new LogEntry("scrobble").addArg(scrobble.toString()));
@ -50,6 +50,8 @@ public class FmAuthNetwork extends FmUserNetwork {
JSONObject obj = makeAuthPostRequest("track.scrobble", params); JSONObject obj = makeAuthPostRequest("track.scrobble", params);
return obj;
} }
public String getToken() { public String getToken() {

View File

@ -204,6 +204,10 @@ public class RootController {
@FXML @FXML
protected void handleAuth(ActionEvent event) { protected void handleAuth(ActionEvent event) {
authenticate();
}
public void authenticate() {
try { try {
Config config = FmFramework.getSessionConfig(); Config config = FmFramework.getSessionConfig();

View File

@ -1,11 +1,27 @@
package sarsoo.fmframework.fx.controller; 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.fxml.FXML;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.DatePicker; import javafx.scene.control.DatePicker;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.control.Slider; import javafx.scene.control.Slider;
import javafx.scene.control.TextField; 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 { public class ScrobblePaneController {
@ -42,11 +58,114 @@ public class ScrobblePaneController {
@FXML @FXML
public void initialize() { public void initialize() {
Config config = FmFramework.getSessionConfig();
if (config.getValue("session_key") == null) {
FmFramework.getController().authenticate();
}
}
@FXML
private void handleClear() {
textTrack.setText("");
textAlbum.setText("");
textArtist.setText("");
textAlbumArtist.setText("");
sliderHour.setValue(0);
sliderMinute.setValue(0);
sliderSecond.setValue(0);
datePicker.setValue(null);
} }
@FXML @FXML
private void handleScrobble() { 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();
} }
} }

View File

@ -39,6 +39,10 @@ public class Scrobble {
return album; return album;
} }
public void setAlbum(Album album) {
this.album = album;
}
public Artist getArtist() { public Artist getArtist() {
return track.getArtist(); return track.getArtist();
} }

View File

@ -48,8 +48,20 @@
<Label text="hour:" GridPane.halignment="CENTER" GridPane.rowIndex="6" /> <Label text="hour:" GridPane.halignment="CENTER" GridPane.rowIndex="6" />
<Label text="minute:" GridPane.halignment="CENTER" GridPane.rowIndex="7" /> <Label text="minute:" GridPane.halignment="CENTER" GridPane.rowIndex="7" />
<Label text="second:" GridPane.halignment="CENTER" GridPane.rowIndex="8" /> <Label text="second:" GridPane.halignment="CENTER" GridPane.rowIndex="8" />
<Button fx:id="buttonScrobble" mnemonicParsing="false" onAction="#handleScrobble" prefHeight="25.0" prefWidth="232.0" text="go" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="9" GridPane.valignment="CENTER" />
<Label fx:id="labelStatus" GridPane.halignment="CENTER" GridPane.rowIndex="9" /> <Label fx:id="labelStatus" GridPane.halignment="CENTER" GridPane.rowIndex="9" />
<GridPane GridPane.columnIndex="1" GridPane.rowIndex="9">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Button fx:id="buttonScrobble" mnemonicParsing="false" onAction="#handleScrobble" prefHeight="25.0" prefWidth="232.0" text="go" GridPane.halignment="CENTER" GridPane.valignment="CENTER" />
<Button fx:id="buttonClear" mnemonicParsing="false" onAction="#handleClear" prefHeight="25.0" prefWidth="232.0" text="clear" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.valignment="CENTER" />
</children>
</GridPane>
</children> </children>
</GridPane> </GridPane>
</children> </children>