From 11836840bfc43c340de6328790d3f0922d9d01ac Mon Sep 17 00:00:00 2001 From: aj Date: Sat, 12 Jan 2019 10:34:08 +0000 Subject: [PATCH] first implementation of scrobble chart --- .../sarsoo/fmframework/fm/FmUserNetwork.java | 69 +++++++++++++ .../ScrobbleChartPaneController.java | 96 ++++++++++++++++--- .../fmframework/fx/ui/ScrobbleChartPane.fxml | 2 +- 3 files changed, 152 insertions(+), 15 deletions(-) diff --git a/src/main/java/sarsoo/fmframework/fm/FmUserNetwork.java b/src/main/java/sarsoo/fmframework/fm/FmUserNetwork.java index 53a3e01..06ab3ef 100644 --- a/src/main/java/sarsoo/fmframework/fm/FmUserNetwork.java +++ b/src/main/java/sarsoo/fmframework/fm/FmUserNetwork.java @@ -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 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 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 getTags(){ if (ConsoleHandler.isVerbose()) diff --git a/src/main/java/sarsoo/fmframework/fx/controller/ScrobbleChartPaneController.java b/src/main/java/sarsoo/fmframework/fx/controller/ScrobbleChartPaneController.java index e10ba0d..1899375 100644 --- a/src/main/java/sarsoo/fmframework/fx/controller/ScrobbleChartPaneController.java +++ b/src/main/java/sarsoo/fmframework/fx/controller/ScrobbleChartPaneController.java @@ -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 service = new Service() { + @Override + protected Task createTask() { + return new Task() { + @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(); + } } diff --git a/src/main/resources/sarsoo/fmframework/fx/ui/ScrobbleChartPane.fxml b/src/main/resources/sarsoo/fmframework/fx/ui/ScrobbleChartPane.fxml index acd6cdc..2d27d64 100644 --- a/src/main/resources/sarsoo/fmframework/fx/ui/ScrobbleChartPane.fxml +++ b/src/main/resources/sarsoo/fmframework/fx/ui/ScrobbleChartPane.fxml @@ -20,7 +20,7 @@
- +