implemented scrobbling
This commit is contained in:
parent
43ea2868eb
commit
6328348b22
@ -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() {
|
||||
|
@ -204,6 +204,10 @@ public class RootController {
|
||||
|
||||
@FXML
|
||||
protected void handleAuth(ActionEvent event) {
|
||||
authenticate();
|
||||
}
|
||||
|
||||
public void authenticate() {
|
||||
try {
|
||||
|
||||
Config config = FmFramework.getSessionConfig();
|
||||
|
@ -1,11 +1,27 @@
|
||||
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 {
|
||||
|
||||
@ -42,11 +58,114 @@ public class ScrobblePaneController {
|
||||
@FXML
|
||||
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
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
@ -39,6 +39,10 @@ public class Scrobble {
|
||||
return album;
|
||||
}
|
||||
|
||||
public void setAlbum(Album album) {
|
||||
this.album = album;
|
||||
}
|
||||
|
||||
public Artist getArtist() {
|
||||
return track.getArtist();
|
||||
}
|
||||
|
@ -48,8 +48,20 @@
|
||||
<Label text="hour:" GridPane.halignment="CENTER" GridPane.rowIndex="6" />
|
||||
<Label text="minute:" GridPane.halignment="CENTER" GridPane.rowIndex="7" />
|
||||
<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" />
|
||||
<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>
|
||||
</GridPane>
|
||||
</children>
|
||||
|
Loading…
Reference in New Issue
Block a user