first implementation of scrobble chart

This commit is contained in:
aj 2019-01-12 10:34:08 +00:00
parent c037d7414a
commit 11836840bf
3 changed files with 152 additions and 15 deletions

View File

@ -124,6 +124,75 @@ public class FmUserNetwork extends FmNetwork {
return 0;
}
public int getScrobbleCountByDate(int day, int month, int year) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getScrobblesByDate " + day + "." + month + "." + year);
LocalDate startDate = LocalDate.of(year, month, day);
ZoneId zoneId = ZoneId.systemDefault();
long epoch = startDate.atStartOfDay(zoneId).toEpochSecond();
long endEpoch = epoch + (24*60*60);
try {
HttpResponse<JsonNode> response = Unirest.get("http://ws.audioscrobbler.com/2.0/").
header("Accept", "application/json").
header("User-Agent", "fmframework").
queryString("method","user.getrecenttracks").
queryString("user", userName).
queryString("from", epoch).
queryString("to", endEpoch).
queryString("limit", 1).
queryString("api_key", key).
queryString("format", "json").
asJson();
int total = new JSONObject(response.getBody().toString()).getJSONObject("recenttracks").getJSONObject("@attr").getInt("total");
return total;
} catch (UnirestException e) {
e.printStackTrace();
}
return 0;
}
public int getScrobbleCountByDeltaDay(int day) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getScrobblesByDeltaDay " + day);
LocalDate local = LocalDate.now();
ZoneId zoneId = ZoneId.systemDefault();
long epoch = local.atStartOfDay(zoneId).toEpochSecond();
epoch -= (day * (24*60*60));
long endEpoch = epoch + (24*60*60);
try {
HttpResponse<JsonNode> response = Unirest.get("http://ws.audioscrobbler.com/2.0/").
header("Accept", "application/json").
header("User-Agent", "fmframework").
queryString("method","user.getrecenttracks").
queryString("user", userName).
queryString("from", epoch).
queryString("to", endEpoch).
queryString("limit", 1).
queryString("api_key", key).
queryString("format", "json").
asJson();
int total = new JSONObject(response.getBody().toString()).getJSONObject("recenttracks").getJSONObject("@attr").getInt("total");
return total;
} catch (UnirestException e) {
e.printStackTrace();
}
return 0;
}
public ArrayList<Tag> getTags(){
if (ConsoleHandler.isVerbose())

View File

@ -3,12 +3,21 @@ package sarsoo.fmframework.fx.controller;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.concurrent.CountDownLatch;
import javax.swing.JOptionPane;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import sarsoo.fmframework.fm.FmNetwork;
import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.fx.ArtistTab;
import sarsoo.fmframework.fx.FmFramework;
import sarsoo.fmframework.fx.TrackTab;
import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Wiki;
import sarsoo.fmframework.net.Key;
@ -16,6 +25,7 @@ import sarsoo.fmframework.net.Network;
import sarsoo.fmframework.util.Maths;
import sarsoo.fmframework.util.Reference;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
@ -23,39 +33,97 @@ public class ScrobbleChartPaneController {
@FXML
private ToolBar toolBar;
@FXML
private Button buttonRefresh;
@FXML
private ChoiceBox dropDownTimeRange;
@FXML
private LineChart lineChartScrobbles;
@FXML
private BorderPane chartBorderPane;
@FXML
public void initialize() {
// dropDownTimeRange.setItems(FXCollections.observableArrayList("week", "month",
// "3 month", "6 month", "year"));
dropDownTimeRange.getItems().addAll("week", "30 day", "90 day", "180 day", "year");
}
@FXML
protected void handleRefresh(ActionEvent event) {
refresh();
}
}
public void populate(Album album) {
}
public void refresh() {
Service<Void> service = new Service<Void>() {
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@Override
protected Void call() throws Exception {
FmUserNetwork net = new FmUserNetwork(Key.getKey(), "sarsoo");
String value = (String) dropDownTimeRange.getValue();
int dayLength = 0;
switch (value) {
case "week":
dayLength = 7;
break;
case "30 day":
dayLength = 30;
break;
case "90 day":
dayLength = 90;
break;
case "180 day":
dayLength = 180;
break;
case "year":
dayLength = 365;
break;
}
XYChart.Series series = new XYChart.Series();
int counter;
for (counter = 0; counter < dayLength; counter++) {
int scrobble = net.getScrobbleCountByDeltaDay(dayLength - counter - 1);
// System.out.println(scrobble);
series.getData().add(new XYChart.Data(String.valueOf(dayLength - counter), scrobble));
}
final CountDownLatch latch = new CountDownLatch(1);
Platform.runLater(new Runnable() {
@Override
public void run() {
try {
lineChartScrobbles.getData().clear();
lineChartScrobbles.getData().add(series);
} finally {
latch.countDown();
}
}
});
latch.await();
// Keep with the background work
return null;
}
};
}
};
service.start();
}
}

View File

@ -20,7 +20,7 @@
</ToolBar>
</bottom>
<center>
<LineChart fx:id="lineChartScrobbles" BorderPane.alignment="CENTER">
<LineChart fx:id="lineChartScrobbles" legendVisible="false" BorderPane.alignment="CENTER">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>