added reference to get username from one point, added artistlist

This commit is contained in:
aj 2018-03-26 20:11:53 -07:00
parent 99325cd5f9
commit 383f76ffe3
14 changed files with 347 additions and 20 deletions

View File

@ -1,15 +1,17 @@
package sarsoo.fmframework.drive;
import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.util.ArtistList;
import sarsoo.fmframework.util.GetObject;
import sarsoo.fmframework.util.Reference;
public class Driver {
public static void main(String[] args) {
GetObject.getAlbum().view();
Reference.setUserName("sarsoo");
//Network.getRecentTracaksUrl("sarsoo");
Reference.getTDE().view();
}
}

View File

@ -0,0 +1,50 @@
package sarsoo.fmframework.error;
public class ApiCallException extends Exception {
private int failureCode;
public ApiCallException(int failureCode) {
this.failureCode = failureCode;
}
public int getFailureCode() {
return failureCode;
}
public String getError() {
switch(failureCode) {
case 2:
return "Invalid service - This service does not exist";
case 3:
return "Invalid Method - No method with that name in this package";
case 4:
return "Authentication Failed - You do not have permissions to access the service";
case 5:
return "Invalid format - This service doesn't exist in that format";
case 6:
return "Invalid parameters - Your request is missing a required parameter";
case 7:
return "Invalid resource specified";
case 8:
return "Operation failed - Something else went wrong";
case 9:
return "Invalid session key - Please re-authenticate";
case 10:
return "Invalid API key - You must be granted a valid key by last.fm";
case 11:
return "Service Offline - This service is temporarily offline. Try again later.";
case 13:
return "Invalid method signature supplied";
case 16:
return "There was a temporary error processing your request. Please try again";
case 26:
return "Suspended API key - Access for your account has been suspended, please contact Last.fm";
case 29:
return "Rate limit exceeded - Your IP has made too many requests in a short period";
default:
return null;
}
}
}

View File

@ -27,7 +27,7 @@ public class AlbumView extends JFrame{
JLabel userPlayCount = new JLabel();
JButton open = new JButton("View Online");
JButton viewArtist = new JButton("View Artist");
JButton musicBeanz = new JButton("Open MusicBeanz");
JButton musicBrainz = new JButton("Open MusicBrainz");
JButton rym = new JButton("Open RYM");
public AlbumView(Album album) {
@ -45,7 +45,7 @@ public class AlbumView extends JFrame{
// info.add(userPlayCount);
buttons.add(open);
buttons.add(viewArtist);
buttons2.add(musicBeanz);
buttons2.add(musicBrainz);
buttons2.add(rym);
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
@ -68,9 +68,9 @@ public class AlbumView extends JFrame{
album.getArtist().view();
}
});
musicBeanz.addActionListener(new ActionListener() {
musicBrainz.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Network.openURL(album.getMusicBeanzURL());;
Network.openURL(album.getMusicBrainzURL());;
}
});
rym.addActionListener(new ActionListener() {

View File

@ -0,0 +1,132 @@
package sarsoo.fmframework.gui;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.net.Network;
import sarsoo.fmframework.util.ArtistList;
public class ArtistListView extends JFrame{
// JPanel info = new JPanel();
// JPanel buttons = new JPanel();
// JPanel buttons2 = new JPanel();
// JLabel name = new JLabel();
// JLabel artist = new JLabel();
// JLabel listeners = new JLabel();
// JLabel playCount = new JLabel();
// JLabel userPlayCount = new JLabel();
// JButton open = new JButton("View Online");
// JButton viewArtist = new JButton("View Artist");
// JButton musicBeanz = new JButton("Open MusicBeanz");
// JButton rym = new JButton("Open RYM");
//
public ArtistListView(ArtistList artists, String title) {
super(title);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new GridLayout(artists.size() + 2,0));
// setSize(300, 300);
setResizable(false);
// info.setLayout(new GridLayout());
// buttons.setLayout(new FlowLayout());
// buttons2.setLayout(new FlowLayout());
//
// buttons.add(open);
// buttons.add(viewArtist);
// buttons2.add(musicBeanz);
// buttons2.add(rym);
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
JPanel headerLabels = new JPanel();
headerLabels.setLayout(new GridLayout(1, 5));
headerLabels.add(new JLabel("Name"));
headerLabels.add(new JLabel("User Scrobbles"));
headerLabels.add(new JLabel("Total Scrobbles"));
headerLabels.add(new JLabel(""));
headerLabels.add(new JLabel(""));
add(headerLabels);
int counter;
for(counter = 0; counter < artists.size(); counter++) {
Artist artist = artists.get(counter);
JLabel name = new JLabel(artist.getName());
JLabel userPlays = new JLabel(Integer.toString(artist.getUserPlayCount()));
JLabel plays = new JLabel(numberFormat.format(artist.getPlayCount()));
JButton openExternal = new JButton("Open Online");
openExternal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Network.openURL(artist.getUrl());
}
});
JButton openInternal = new JButton("Open Artist");
openInternal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
artist.view();
}
});
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 5));
panel.add(name);
panel.add(userPlays);
panel.add(plays);
panel.add(openInternal);
panel.add(openExternal);
add(panel);
}
JLabel totalScrobbles = new JLabel(numberFormat.format(artists.getTotalUserScrobbles()) + " Total Plays");
add(totalScrobbles);
pack();
// name.setText(album.getName());
// name.setHorizontalTextPosition(JLabel.CENTER);
// artist.setText(album.getArtist().getName());
// listeners.setText(numberFormat.format(album.getListeners()) + " Listeners");
// playCount.setText(numberFormat.format(album.getPlayCount()) + " Scrobbles");
// userPlayCount.setText(numberFormat.format(album.getUserPlayCount()) + " Your Scrobbles");
//
//
// open.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent arg0) {
// Network.openURL(album.getUrl());
// }
// });
// viewArtist.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent arg0) {
// album.getArtist().view();
// }
// });
// musicBeanz.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent arg0) {
// Network.openURL(album.getMusicBeanzURL());;
// }
// });
// rym.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent arg0) {
// Network.openURL(album.getRymURL());;
// }
// });
//
// add(name);
// add(artist);
// add(listeners);
// add(playCount);
// add(userPlayCount);
//// add(info);
// add(buttons);
// add(buttons2);
}
}

View File

@ -23,7 +23,7 @@ public class FMObjView extends JFrame{
JLabel playCount = new JLabel();
JLabel userPlayCount = new JLabel();
JButton open = new JButton("View Online");
JButton musicBeanz = new JButton("Open MusicBeanz");
JButton musicBrainz = new JButton("Open MusicBrainz");
public FMObjView(FMObj obj) {
super(obj.toString());
@ -38,7 +38,7 @@ public class FMObjView extends JFrame{
// info.add(playCount);
// info.add(userPlayCount);
buttons.add(open);
buttons.add(musicBeanz);
buttons.add(musicBrainz);
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
@ -54,9 +54,9 @@ public class FMObjView extends JFrame{
Network.openURL(obj.getUrl());
}
});
musicBeanz.addActionListener(new ActionListener() {
musicBrainz.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Network.openURL(obj.getMusicBeanzURL());;
Network.openURL(obj.getMusicBrainzURL());;
}
});
add(name);
@ -65,6 +65,8 @@ public class FMObjView extends JFrame{
add(userPlayCount);
// add(info);
add(buttons);
pack();
}

View File

@ -7,6 +7,7 @@ import org.w3c.dom.Document;
import sarsoo.fmframework.gui.AlbumView;
import sarsoo.fmframework.net.Network;
import sarsoo.fmframework.parser.Parser;
import sarsoo.fmframework.util.Reference;
public class Album extends FMObj{
protected Artist artist;
@ -15,7 +16,7 @@ public class Album extends FMObj{
public Album(String name, String artist) {
super(name, null, null, 0, 0, 0, null);
this.artist = Artist.getArtist(artist, "sarsoo");
this.artist = Artist.getArtist(artist, Reference.getUserName());
}
public Album(String name, String url, String mbid, Artist artist, int listeners, int playCount, int userPlayCount, Wiki wiki) {
@ -74,7 +75,7 @@ public class Album extends FMObj{
}
@Override
public String getMusicBeanzURL() {
public String getMusicBrainzURL() {
return "https://musicbrainz.org/release/" + mbid;
}

View File

@ -33,6 +33,13 @@ public class Artist extends FMObj{
return artist;
}
public static Artist getArtistByMbid(String mbid, String username) {
String url = Network.getArtistInfoMbidUrl(mbid, username);
Document response = Network.getResponse(url);
Artist artist = Parser.parseArtist(response);
return artist;
}
public ArrayList<Album> getAlbum(){
return albums;
}
@ -57,7 +64,7 @@ public class Artist extends FMObj{
}
@Override
public String getMusicBeanzURL() {
public String getMusicBrainzURL() {
return "https://musicbrainz.org/artist/" + mbid;
}

View File

@ -61,6 +61,6 @@ public abstract class FMObj {
return wiki;
}
abstract public String getMusicBeanzURL();
abstract public String getMusicBrainzURL();
}

View File

@ -54,7 +54,7 @@ public class Track extends FMObj{
}
@Override
public String getMusicBeanzURL() {
public String getMusicBrainzURL() {
return "https://musicbrainz.org/artist/" + mbid;
}
}

View File

@ -15,6 +15,8 @@ import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import sarsoo.fmframework.error.ApiCallException;
public class Network {
public static Document getResponse(String urlString) {
@ -22,10 +24,10 @@ public class Network {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Accept", "application/xml");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
throw new ApiCallException(conn.getResponseCode());
}
InputStream input = conn.getInputStream();
@ -60,6 +62,9 @@ public class Network {
e.printStackTrace();
} catch (ApiCallException e) {
System.out.println(e.getFailureCode() + " " + e.getError());
e.printStackTrace();
}
return null;
@ -72,6 +77,14 @@ public class Network {
return urlString;
}
public static String getArtistInfoMbidUrl(String mbid, String username) {
String urlString = String.format(
"http://ws.audioscrobbler.com/2.0/?method=artist.getInfo&mbid=%s&autocorrect=1&username=%s&api_key=%s",
mbid, username, Key.getKey());
return urlString;
}
public static String getAlbumInfoUrl(String album, String artist, String username) {
String urlString = String.format(

View File

@ -9,9 +9,15 @@ import sarsoo.fmframework.music.Album;
class AlbumTest {
@Test
void test() {
void testGetName() {
Album album = Album.getAlbum("Recovery", "Eminem", "sarsoo");
System.out.println(album.getArtist());
assertEquals(album.getName(), "Recovery");
}
@Test
void testGetArtist() {
Album album = Album.getAlbum("Recovery", "Eminem", "sarsoo");
assertEquals(album.getArtist().getName(), "Eminem");
}
}

View File

@ -0,0 +1,51 @@
package sarsoo.fmframework.util;
import java.util.ArrayList;
import sarsoo.fmframework.gui.ArtistListView;
import sarsoo.fmframework.music.Artist;
public class ArtistList extends ArrayList<Artist> {
private String groupName = null;
public ArtistList() {
super();
}
public ArtistList(String name) {
super();
this.groupName = name;
}
public int getTotalUserScrobbles() {
int counter;
int totalScrobbles = 0;
for (counter = 0; counter < size(); counter++) {
totalScrobbles += get(counter).getUserPlayCount();
}
return totalScrobbles;
}
public void view(String title) {
ArtistListView view = new ArtistListView(this, title);
view.setVisible(true);
}
public void view() {
if(groupName != null) {
ArtistListView view = new ArtistListView(this, getGroupName());
view.setVisible(true);
}else {
ArtistListView view = new ArtistListView(this, "Artist List View");
view.setVisible(true);
}
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String name) {
this.groupName = name;
}
}

View File

@ -0,0 +1,31 @@
package sarsoo.fmframework.util;
import sarsoo.fmframework.music.Artist;
public class Reference {
private static String userName;
public static String getUserName() {
return userName;
}
public static void setUserName(String userNameIn) {
userName = userNameIn;
}
public static ArtistList getTDE() {
ArtistList tde = new ArtistList("TDE");
tde.add(Artist.getArtist("Kendrick Lamar", Reference.getUserName()));
tde.add(Artist.getArtist("Jay Rock", Reference.getUserName()));
tde.add(Artist.getArtist("ScHoolboy Q", Reference.getUserName()));
tde.add(Artist.getArtist("Ab-Soul", Reference.getUserName()));
tde.add(Artist.getArtistByMbid("6fc5c0c6-bf05-4b29-bda0-5fa6cc863785", Reference.getUserName())); //Black Hippy
tde.add(Artist.getArtist("Isaiah Rashad", Reference.getUserName()));
tde.add(Artist.getArtist("SZA", Reference.getUserName()));
tde.add(Artist.getArtist("Sir", Reference.getUserName()));
return tde;
}
}

View File

@ -0,0 +1,32 @@
package sarsoo.fmframework.util;
import sarsoo.fmframework.music.Artist;
public class ScrobbleSumming {
public static int getTopDawgScrobbles() {
ArtistList tde = new ArtistList();
// Artist kendrick = Artist.getArtist("Kendrick Lamar", "sarsoo");
// Artist jay = Artist.getArtist("Jay Rock", "sarsoo");
// Artist school = Artist.getArtist("ScHoolboy Q", "sarsoo");
// Artist ab = Artist.getArtist("Ab-Soul", "sarsoo");
// Artist blackHippy = Artist.getArtist("Black Hippy", "sarsoo");
// Artist isaiah = Artist.getArtist("Isaiah Rashad", "sarsoo");
// Artist sza = Artist.getArtist("SZA", "sarsoo");
// Artist lance = Artist.getArtist("Lance Skiiiwalker", "sarsoo");
// Artist sir = Artist.getArtist("SiR", "sarsoo");
tde.add(Artist.getArtist("Kendrick Lamar", "sarsoo"));
tde.add(Artist.getArtist("Jay Rock", "sarsoo"));
tde.add(Artist.getArtist("ScHoolboy Q", "sarsoo"));
tde.add(Artist.getArtist("Ab-Soul", "sarsoo"));
tde.add(Artist.getArtist("Black Hippy", "sarsoo"));
tde.add(Artist.getArtist("Isaiah Rashad", "sarsoo"));
tde.add(Artist.getArtist("SZA", "sarsoo"));
tde.add(Artist.getArtist("Lance Skiiiwalker", "sarsoo"));
tde.add(Artist.getArtist("SiR", "sarsoo"));
return tde.getTotalUserScrobbles();
}
}