new network implemented, daily count working again

This commit is contained in:
aj 2018-06-20 23:22:40 +01:00
parent 7dcd3b9994
commit 3ed4d691b7
16 changed files with 241 additions and 72 deletions

View File

@ -14,6 +14,10 @@ apply plugin: 'eclipse'
mainClassName = 'sarsoo.fmframework.fx.FmFramework' mainClassName = 'sarsoo.fmframework.fx.FmFramework'
version = '0.1.0'
sourceCompatibility = 1.8
targetCompatibility = 1.8
// In this section you declare where to find the dependencies of your project // In this section you declare where to find the dependencies of your project
repositories { repositories {
// Use 'jcenter' for resolving your dependencies. // Use 'jcenter' for resolving your dependencies.
@ -36,6 +40,7 @@ dependencies {
testCompile 'junit:junit:4.12' testCompile 'junit:junit:4.12'
} }
/*
jar { jar {
manifest { manifest {
attributes 'Implementation-Title': 'fmframework', attributes 'Implementation-Title': 'fmframework',
@ -43,5 +48,17 @@ jar {
'Main-Class': 'sarsoo.fmframework.fx.FmFramework' 'Main-Class': 'sarsoo.fmframework.fx.FmFramework'
} }
baseName = 'fmframework' baseName = 'fmframework'
version = '0.1.0' version = version
}*/
//create a single Jar with all dependencies
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'fmframework',
'Implementation-Version': version,
'Main-Class': 'sarsoo.fmframework.fx.FmFramework'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
} }

View File

@ -358,14 +358,23 @@ public class FmNetwork {
} }
public Album refresh(Album album) { public Album refresh(Album album) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>refreshAlbum: " + album.getName() + " " + album.getArtist().getName());
return getAlbum(album.getName(), album.getArtist().getName()); return getAlbum(album.getName(), album.getArtist().getName());
} }
public Artist refresh(Artist artist) { public Artist refresh(Artist artist) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>refreshArtist: " + artist.getName());
return getArtist(artist.getName()); return getArtist(artist.getName());
} }
public Track refresh(Track track) { public Track refresh(Track track) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>refreshTrack: " + track.getName() + " " + track.getArtist().getName());
Track refreshedTrack = getTrack(track.getName(), track.getArtist().getName()); Track refreshedTrack = getTrack(track.getName(), track.getArtist().getName());
refreshedTrack.setAlbum(refresh(track.getAlbum())); refreshedTrack.setAlbum(refresh(track.getAlbum()));

View File

@ -1,5 +1,9 @@
package sarsoo.fmframework.fm; package sarsoo.fmframework.fm;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
@ -8,33 +12,36 @@ import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest; import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException; import com.mashape.unirest.http.exceptions.UnirestException;
import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.music.Tag;
import sarsoo.fmframework.music.Track; import sarsoo.fmframework.music.Track;
import sarsoo.fmframework.util.ConsoleHandler;
import sarsoo.fmframework.util.FMObjList;
public class FmUserNetwork extends FmNetwork{ public class FmUserNetwork extends FmNetwork {
public enum TaggingType {
ARTIST, ALBUM, TRACK
}
public FmUserNetwork(String key, String userName) { public FmUserNetwork(String key, String userName) {
super(key, userName); super(key, userName);
} }
public User getUser() { public User getUser() {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getUser");
try { try {
HttpResponse<JsonNode> response = Unirest.get("http://ws.audioscrobbler.com/2.0/"). HttpResponse<JsonNode> response = Unirest.get("http://ws.audioscrobbler.com/2.0/")
header("Accept", "application/json"). .header("Accept", "application/json").header("User-Agent", "fmframework")
header("User-Agent", "fmframework"). .queryString("method", "user.getinfo").queryString("user", userName).queryString("api_key", key)
queryString("method","user.getinfo"). .queryString("format", "json").asJson();
queryString("user", userName).
queryString("api_key", key).
queryString("format", "json").
asJson();
JSONObject obj = new JSONObject(response.getBody().toString()).getJSONObject("user"); JSONObject obj = new JSONObject(response.getBody().toString()).getJSONObject("user");
return new User(obj.getString("name"), return new User(obj.getString("name"), obj.getString("realname"), obj.getString("url"),
obj.getString("realname"), obj.getString("country"), obj.getInt("age"), obj.getString("gender").charAt(0),
obj.getString("url"),
obj.getString("country"),
obj.getInt("age"),
obj.getString("gender").charAt(0),
obj.getInt("playcount")); obj.getInt("playcount"));
} catch (UnirestException e) { } catch (UnirestException e) {
@ -45,33 +52,37 @@ public class FmUserNetwork extends FmNetwork{
} }
public String getUserRealName() { public String getUserRealName() {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getUserRealname");
return getUser().getRealName(); return getUser().getRealName();
} }
public int getUserScrobbleCount() { public int getUserScrobbleCount() {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getUserScrobbleCount");
return getUser().getScrobbleCount(); return getUser().getScrobbleCount();
} }
public Track getLastTrack() { public Track getLastTrack() {
try { if (ConsoleHandler.isVerbose())
HttpResponse<JsonNode> response = Unirest.get("http://ws.audioscrobbler.com/2.0/"). ConsoleHandler.getConsole().write(">>getLastTrack");
header("Accept", "application/json").
header("User-Agent", "fmframework").
queryString("method","user.getrecenttracks").
queryString("user", userName).
queryString("api_key", key).
queryString("format", "json").
queryString("limit", "1").
asJson();
JSONArray obj = new JSONObject(response.getBody().toString()).getJSONObject("recenttracks").getJSONArray("track"); 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("api_key", key).queryString("format", "json").queryString("limit", "1").asJson();
JSONArray obj = new JSONObject(response.getBody().toString()).getJSONObject("recenttracks")
.getJSONArray("track");
JSONObject track = (JSONObject) obj.get(0); JSONObject track = (JSONObject) obj.get(0);
Track trackObj = getTrack(track.getString("name"), track.getJSONObject("artist").getString("#text")); Track trackObj = getTrack(track.getString("name"), track.getJSONObject("artist").getString("#text"));
trackObj.setAlbum(getAlbum(track.getJSONObject("album").getString("#text"), track.getJSONObject("artist").getString("#text"))); trackObj.setAlbum(getAlbum(track.getJSONObject("album").getString("#text"),
track.getJSONObject("artist").getString("#text")));
return trackObj; return trackObj;
@ -81,4 +92,127 @@ public class FmUserNetwork extends FmNetwork{
return null; return null;
} }
public int getScrobblesToday() {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getScrobblesToday");
LocalDate local = LocalDate.now();
ZoneId zoneId = ZoneId.systemDefault();
long epoch = local.atStartOfDay(zoneId).toEpochSecond();
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("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())
ConsoleHandler.getConsole().write(">>getTags");
try {
HttpResponse<JsonNode> response = Unirest.get("http://ws.audioscrobbler.com/2.0/").
header("Accept", "application/json").
header("User-Agent", "fmframework").
queryString("method","user.gettoptags").
queryString("user", userName).
queryString("api_key", key).
queryString("format", "json").
asJson();
JSONArray tagJsonArray = new JSONObject(response.getBody().toString()).getJSONObject("toptags").getJSONArray("tag");
JSONObject tagJson;
ArrayList<Tag> tags = new ArrayList<Tag>();
int counter;
for(counter = 0; counter < tagJsonArray.length(); counter++) {
tagJson = (JSONObject) tagJsonArray.get(counter);
Tag tag = new Tag(tagJson.getString("name"), tagJson.getString("url"), tagJson.getInt("count"));
tags.add(tag);
}
return tags;
} catch (UnirestException e) {
e.printStackTrace();
}
return null;
}
public FMObjList getTag(String tagName) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getTag: " + tagName);
try {
HttpResponse<JsonNode> response = Unirest.get("http://ws.audioscrobbler.com/2.0/").
header("Accept", "application/json").
header("User-Agent", "fmframework").
queryString("method","user.getpersonaltags").
queryString("user", userName).
queryString("tag", tagName).
queryString("taggingtype", "artist").
queryString("limit", 70).
queryString("api_key", key).
queryString("format", "json").
asJson();
JSONArray tagJsonArray = new JSONObject(response.getBody().toString()).getJSONObject("taggings").getJSONObject("artists").getJSONArray("artist");
JSONObject artistJson;
FMObjList list = new FMObjList();
list.setGroupName(tagName);
int counter;
for(counter = 0; counter < tagJsonArray.length(); counter++) {
artistJson = (JSONObject) tagJsonArray.get(counter);
Artist artist = getArtist(artistJson.getString("name"));
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">Tag: " + tagName + ", " + artist.getName());
list.add(artist);
}
return list;
} catch (UnirestException e) {
e.printStackTrace();
}
return null;
}
} }

View File

@ -32,7 +32,6 @@ import sarsoo.fmframework.music.Track;
import sarsoo.fmframework.net.Key; import sarsoo.fmframework.net.Key;
import sarsoo.fmframework.util.ConsoleHandler; import sarsoo.fmframework.util.ConsoleHandler;
import sarsoo.fmframework.util.FMObjList; import sarsoo.fmframework.util.FMObjList;
import sarsoo.fmframework.util.Getter;
import sarsoo.fmframework.util.Reference; import sarsoo.fmframework.util.Reference;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.chart.*; import javafx.scene.chart.*;
@ -69,7 +68,7 @@ public class ControllerMain {
FmUserNetwork net = new FmUserNetwork(Key.getKey(), Reference.getUserName()); FmUserNetwork net = new FmUserNetwork(Key.getKey(), Reference.getUserName());
String scrobblesToday = numberFormat.format(0); String scrobblesToday = numberFormat.format(net.getScrobblesToday());
String scrobbles = numberFormat.format(net.getUserScrobbleCount()); String scrobbles = numberFormat.format(net.getUserScrobbleCount());
TrackTab tab = new TrackTab(net.getLastTrack()); TrackTab tab = new TrackTab(net.getLastTrack());
@ -105,7 +104,9 @@ public class ControllerMain {
} }
public void refreshTagMenu() { public void refreshTagMenu() {
tags = Getter.getUserTags(Reference.getUserName()); FmUserNetwork net = new FmUserNetwork(Key.getKey(), Reference.getUserName());
tags = net.getTags();
Collections.sort(tags); Collections.sort(tags);
@ -131,7 +132,7 @@ public class ControllerMain {
protected Void call() throws Exception { protected Void call() throws Exception {
FMObjListTab tab = new FMObjListTab( FMObjListTab tab = new FMObjListTab(
Getter.getUserTag(Reference.getUserName(), name)); net.getTag(name));
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
Platform.runLater(new Runnable() { Platform.runLater(new Runnable() {
@ -167,24 +168,26 @@ public class ControllerMain {
@Override @Override
protected Void call() throws Exception { protected Void call() throws Exception {
int total = Getter.getScrobbles(Reference.getUserName()); FmUserNetwork net = new FmUserNetwork(Key.getKey(), Reference.getUserName());
FMObjList rap = Getter.getUserTag(Reference.getUserName(), "rap"); int total = net.getUserScrobbleCount();
FMObjList classicRap = Getter.getUserTag(Reference.getUserName(), "classic rap");
FMObjList grime = Getter.getUserTag(Reference.getUserName(), "grime");
FMObjList classicRock = Getter.getUserTag(Reference.getUserName(), "classic rock"); FMObjList rap = net.getTag("rap");
FMObjList popPunk = Getter.getUserTag(Reference.getUserName(), "pop punk"); FMObjList classicRap = net.getTag("classic rap");
FMObjList electronic = Getter.getUserTag(Reference.getUserName(), "electronic"); FMObjList grime = net.getTag("grime");
FMObjList metal = Getter.getUserTag(Reference.getUserName(), "metal");
FMObjList indie = Getter.getUserTag(Reference.getUserName(), "indie"); FMObjList classicRock = net.getTag("classic rock");
FMObjList rock = Getter.getUserTag(Reference.getUserName(), "rock"); FMObjList popPunk = net.getTag("pop punk");
FMObjList jazz = Getter.getUserTag(Reference.getUserName(), "jazz"); FMObjList electronic = net.getTag("electronic");
FMObjList blues = Getter.getUserTag(Reference.getUserName(), "blues"); FMObjList metal = net.getTag("metal");
FMObjList core = Getter.getUserTag(Reference.getUserName(), "core"); FMObjList indie = net.getTag("indie");
FMObjList rnb = Getter.getUserTag(Reference.getUserName(), "rnb"); FMObjList rock = net.getTag("rock");
FMObjList soulFunk = Getter.getUserTag(Reference.getUserName(), "soulfunk"); FMObjList jazz = net.getTag("jazz");
FMObjList punk = Getter.getUserTag(Reference.getUserName(), "punk"); FMObjList blues = net.getTag("blues");
FMObjList core = net.getTag("core");
FMObjList rnb = net.getTag("rnb");
FMObjList soulFunk = net.getTag("soulfunk");
FMObjList punk = net.getTag("punk");
int rapTotal = rap.getTotalUserScrobbles() + classicRap.getTotalUserScrobbles() int rapTotal = rap.getTotalUserScrobbles() + classicRap.getTotalUserScrobbles()
+ grime.getTotalUserScrobbles(); + grime.getTotalUserScrobbles();

View File

@ -21,7 +21,6 @@ import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.music.FMObj; import sarsoo.fmframework.music.FMObj;
import sarsoo.fmframework.net.Key; import sarsoo.fmframework.net.Key;
import sarsoo.fmframework.util.FMObjList; import sarsoo.fmframework.util.FMObjList;
import sarsoo.fmframework.util.Getter;
import sarsoo.fmframework.util.Maths; import sarsoo.fmframework.util.Maths;
import sarsoo.fmframework.util.Reference; import sarsoo.fmframework.util.Reference;
import javafx.scene.layout.*; import javafx.scene.layout.*;
@ -53,7 +52,7 @@ public class FMObjListPaneController {
double percent = Maths.getPercentListening(list, Reference.getUserName()); double percent = Maths.getPercentListening(list, Reference.getUserName());
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US); NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
labelTotalScrobbles.setText("Σ " + list.getTotalUserScrobbles()); labelTotalScrobbles.setText("" + list.getTotalUserScrobbles());
labelPercent.setText(String.format("%.2f%%", percent)); labelPercent.setText(String.format("%.2f%%", percent));
Collections.sort(list); Collections.sort(list);
@ -124,7 +123,7 @@ public class FMObjListPaneController {
@FXML @FXML
protected void handleRefresh(ActionEvent event) { protected void handleRefresh(ActionEvent event) {
list = Getter.getUserTag(Reference.getUserName(), list.getGroupName()); list = new FmUserNetwork(Key.getKey(), Reference.getUserName()).getTag(list.getGroupName());
double percent = Maths.getPercentListening(list, Reference.getUserName()); double percent = Maths.getPercentListening(list, Reference.getUserName());

View File

@ -27,7 +27,6 @@ import sarsoo.fmframework.music.FMObj;
import sarsoo.fmframework.music.Track; import sarsoo.fmframework.music.Track;
import sarsoo.fmframework.net.Key; import sarsoo.fmframework.net.Key;
import sarsoo.fmframework.util.FMObjList; import sarsoo.fmframework.util.FMObjList;
import sarsoo.fmframework.util.Getter;
import sarsoo.fmframework.util.Maths; import sarsoo.fmframework.util.Maths;
import sarsoo.fmframework.util.Reference; import sarsoo.fmframework.util.Reference;
import javafx.scene.layout.*; import javafx.scene.layout.*;
@ -144,17 +143,21 @@ public class FMObjListPaneEditController {
FmNetwork net = new FmNetwork(Key.getKey(), Reference.getUserName()); FmNetwork net = new FmNetwork(Key.getKey(), Reference.getUserName());
FMObjList newList = new FMObjList();
int counter; int counter;
for (counter = 0; counter < list.size(); counter++) { for (counter = 0; counter < list.size(); counter++) {
net.refresh(list.get(counter));
newList.add(net.refresh(list.get(counter)));
} }
setList(newList);
} }
public void refresh() { public void refresh() {
double percent = Maths.getPercentListening(list, Reference.getUserName()); double percent = Maths.getPercentListening(list, Reference.getUserName());
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US); NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
labelTotalScrobbles.setText("Σ " + list.getTotalUserScrobbles()); labelTotalScrobbles.setText("" + list.getTotalUserScrobbles());
labelPercent.setText(String.format("%.2f%%", percent)); labelPercent.setText(String.format("%.2f%%", percent));
Collections.sort(list); Collections.sort(list);

View File

@ -66,6 +66,7 @@ public class Artist extends FMObj implements Serializable{
return null; return null;
} }
@Deprecated
public static Artist getArtistByMbid(String mbid, String username) { public static Artist getArtistByMbid(String mbid, String username) {
String url = URLBuilder.getArtistInfoMbidUrl(mbid, username); String url = URLBuilder.getArtistInfoMbidUrl(mbid, username);
Document response = Network.getResponse(url); Document response = Network.getResponse(url);

View File

@ -13,6 +13,13 @@ public class Tag implements Comparable<Tag>, Serializable{
private String summary; private String summary;
private String content; private String content;
public Tag(String name, String url, int count) {
this.setName(name);
this.setUrl(url);
this.count = count;
}
@Deprecated
public Tag(String name, String url) { public Tag(String name, String url) {
this.setName(name); this.setName(name);
this.setUrl(url); this.setUrl(url);

View File

@ -9,6 +9,7 @@ import java.net.URLEncoder;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
@Deprecated
public class Authenticator { public class Authenticator {
public String write() { public String write() {
URL url; URL url;

View File

@ -16,6 +16,7 @@ import org.xml.sax.SAXException;
import sarsoo.fmframework.error.ApiCallException; import sarsoo.fmframework.error.ApiCallException;
@Deprecated
public class Network { public class Network {
public static Document getResponse(String urlString) { public static Document getResponse(String urlString) {

View File

@ -1,13 +0,0 @@
package sarsoo.fmframework.net;
public class ParamAssembler {
public static byte[] getToken() {
return null;
}
public static void getSignature() {
}
}

View File

@ -7,6 +7,7 @@ import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
@Deprecated
public class TestCall { public class TestCall {
public static void test(String urlString) { public static void test(String urlString) {

View File

@ -7,6 +7,7 @@ import java.time.LocalDateTime;
import sarsoo.fmframework.music.Track; import sarsoo.fmframework.music.Track;
@Deprecated
public class URLBuilder { public class URLBuilder {
public enum FMObjType{ public enum FMObjType{

View File

@ -12,6 +12,7 @@ import sarsoo.fmframework.parser.Parser;
import sarsoo.fmframework.util.FMObjList; import sarsoo.fmframework.util.FMObjList;
import sarsoo.fmframework.util.Reference; import sarsoo.fmframework.util.Reference;
@Deprecated
public class User { public class User {
protected String userName; protected String userName;

View File

@ -4,12 +4,16 @@ import static org.junit.Assert.*;
import org.junit.Test; import org.junit.Test;
import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.music.FMObj;
import sarsoo.fmframework.util.FMObjList;
public class FmUserNetworkTest { public class FmUserNetworkTest {
@Test @Test
public void test() { public void test() {
FmUserNetwork network = new FmUserNetwork("54a9f5c4c36f5d2cba0d4ffe3846e8b4", "sarsoo"); FmUserNetwork network = new FmUserNetwork("54a9f5c4c36f5d2cba0d4ffe3846e8b4", "sarsoo");
System.out.println(network.getLastTrack()); network.getScrobblesToday();
} }
} }

View File

@ -12,7 +12,7 @@ public class FMObjListTest {
// Album album = new Album(); // Album album = new Album();
fail("Not yet implemented"); // fail("Not yet implemented");
} }
} }