added scrobble pane chart

This commit is contained in:
aj 2019-05-22 16:47:53 +01:00
parent 410dc9e75a
commit 7503faa70f
6 changed files with 173 additions and 10 deletions

View File

@ -1,6 +1,8 @@
package sarsoo.fmframework.fm;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.HashMap;
@ -232,6 +234,36 @@ public class FmUserNetwork extends FmNetwork {
return total;
}
public LocalDateTime getFirstScrobbleDateTime() {
Logger.getLog().log(new LogEntry("getFirstScrobbleDates"));
HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("user", userName);
parameters.put("limit", Integer.toString(1));
JSONObject obj = makeGetRequest("user.getrecenttracks", parameters);
int page = obj.getJSONObject("recenttracks").getJSONObject("@attr").getInt("totalPages");
parameters.put("page", Integer.toString(page));
JSONObject dateJson = makeGetRequest("user.getrecenttracks", parameters);
JSONArray trackArray = dateJson.getJSONObject("recenttracks").getJSONArray("track");
long uts;
if(trackArray.length() == 1) {
uts = ((JSONObject) trackArray.get(0)).getJSONObject("date").getLong("uts");
}else {
uts = ((JSONObject) trackArray.get(1)).getJSONObject("date").getLong("uts");
}
return LocalDateTime.ofInstant(Instant.ofEpochSecond(uts), ZoneId.systemDefault());
}
public FMObjList getTopTracks(String period, int number) {

View File

@ -1,17 +1,25 @@
package sarsoo.fmframework.fx.controller;
import java.text.NumberFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Locale;
import javafx.fxml.FXML;
import javafx.scene.chart.AreaChart;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.control.Label;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.GridPane;
import sarsoo.fmframework.config.Config;
import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.fx.FmFramework;
import sarsoo.fmframework.music.FMObj;
import sarsoo.fmframework.music.Scrobble;
import sarsoo.fmframework.util.MonthScrobbles;
import sarsoo.fmframework.util.ScrobbleCountCalendar;
public class ScrobblesViewPaneController {
@ -22,7 +30,7 @@ public class ScrobblesViewPaneController {
private GridPane gridPane;
@FXML
private AreaChart areaChart;
private AreaChart<String, Integer> areaChart;
@FXML
public void initialize() {
@ -30,9 +38,9 @@ public class ScrobblesViewPaneController {
}
public void populate(FMObj obj) {
ArrayList<Scrobble> scrobbles = obj.getScrobbles();
if (scrobbles != null) {
if (scrobbles.size() > 0) {
@ -52,16 +60,36 @@ public class ScrobblesViewPaneController {
gridPane.add(trackName, 0, counter);
gridPane.add(artistName, 2, counter);
gridPane.add(date, 3, counter);
if(scrobble.getAlbum() != null) {
if (scrobble.getAlbum() != null) {
Label albumName = new Label(scrobble.getAlbum().getName().toLowerCase());
gridPane.add(albumName, 1, counter);
}
}
Config config = FmFramework.getSessionConfig();
FmUserNetwork net = new FmUserNetwork(config.getValue("api_key"), config.getValue("username"));
LocalDate date = net.getFirstScrobbleDateTime().toLocalDate();
ScrobbleCountCalendar calendar = new ScrobbleCountCalendar(date);
XYChart.Series<String, Integer> series = new XYChart.Series<>();
for (Scrobble scrobble : scrobbles) {
LocalDateTime scrobbleDate = scrobble.getDateTime();
calendar.addCount(scrobbleDate.getMonth(), scrobbleDate.getYear());
}
for(MonthScrobbles month: calendar.getMonthScrobbles()) {
series.getData().add(new Data<String, Integer>(month.toString(), month.getCount()));
}
areaChart.getData().add(series);
}
}
}
}
}

View File

@ -0,0 +1,46 @@
package sarsoo.fmframework.util;
import java.time.Month;
public class MonthScrobbles {
private final Month month;
private final int year;
private int count = 0;
public MonthScrobbles(Month left, int right) {
this.month = left;
this.year = right;
}
public Month getMonth() {
return month;
}
public int getYear() {
return year;
}
public void addCount() {
count++;
}
public int getCount() {
return count;
}
@Override
public String toString() {
return month.toString() + " " + Integer.toString(year);
}
@Override
public boolean equals(Object o) {
if (!(o instanceof MonthScrobbles))
return false;
MonthScrobbles pairo = (MonthScrobbles) o;
return this.month.toString().equals(pairo.getMonth().toString()) && (this.year == pairo.getYear());
}
}

View File

@ -0,0 +1,48 @@
package sarsoo.fmframework.util;
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import sarsoo.fmframework.log.Logger;
import sarsoo.fmframework.log.entry.ErrorEntry;
public class ScrobbleCountCalendar {
public ArrayList<MonthScrobbles> months;
public ScrobbleCountCalendar(LocalDate date) {
months = new ArrayList<>();
LocalDate now = LocalDate.now();
long monthsDiff = ChronoUnit.MONTHS.between(date, now) + 1;
for(int i = 0; i < monthsDiff; i++) {
LocalDate counter = date.plusMonths(i);
months.add(new MonthScrobbles(counter.getMonth(), counter.getYear()));
}
}
public void addCount(Month monthIn, int year) {
for(MonthScrobbles month: months) {
if(month.getMonth().getValue() == monthIn.getValue() && year == month.getYear()) {
month.addCount();
break;
}
}
Logger.getLog().logError(new ErrorEntry("ScrobbleCountCalendar").addArg("add count").addArg("month not found"));
}
public ArrayList<MonthScrobbles> getMonthScrobbles(){
return months;
}
}

View File

@ -8,7 +8,7 @@
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>
<AnchorPane stylesheets="@../styles/mainPane.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sarsoo.fmframework.fx.controller.ScrobblesViewPaneController">
<AnchorPane prefHeight="386.0" prefWidth="512.0" stylesheets="@../styles/mainPane.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sarsoo.fmframework.fx.controller.ScrobblesViewPaneController">
<children>
<SplitPane fx:id="splitPane" dividerPositions="0.4909819639278557" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
@ -26,7 +26,7 @@
</ScrollPane>
<AnchorPane fx:id="areaChartAnchorPane" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<AreaChart fx:id="areaChart" layoutX="-93.0" layoutY="69.0" prefHeight="798.0" prefWidth="504.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<AreaChart fx:id="areaChart" layoutX="-93.0" layoutY="69.0" legendVisible="false" prefHeight="798.0" prefWidth="504.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>

View File

@ -80,6 +80,15 @@ public class FmUserNetworkTest {
scrobbles.stream().forEach(System.out::println);
System.out.println(scrobbles.size());
assertEquals(53, scrobbles.size());
assertTrue(true);
}
@Test
public void testFirstScrobbleDateTime() {
FmUserNetwork net = new FmUserNetwork(Key.getKey(), "sarsoo");
System.out.println(net.getFirstScrobbleDateTime());
assertTrue(true);
}
}