added some error handling for parsing, added get artist

This commit is contained in:
aj 2018-03-28 14:47:41 -07:00
parent 2134e358c1
commit 8014a1e3a6
16 changed files with 313 additions and 205 deletions

View File

@ -6,7 +6,7 @@ import sarsoo.fmframework.util.Reference;
public class Driver {
public static void main(String[] args) {
Reference.setUserName("Sarsoo");
Reference.setUserName("sarsoo");
MainMenu main = new MainMenu();
main.setVisible(true);

View File

@ -1,5 +1,7 @@
package sarsoo.fmframework.error;
import sarsoo.fmframework.util.Reference;
public class ApiCallException extends Exception {
private int failureCode;
@ -42,6 +44,8 @@ public class ApiCallException extends Exception {
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";
case 400:
return "Bad Request";
default:
return null;
}

View File

@ -1,6 +1,5 @@
package sarsoo.fmframework.gui;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
@ -16,15 +15,16 @@ import javax.swing.JPanel;
import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.net.Network;
public class AlbumView extends JFrame{
JPanel info = new JPanel();
public class AlbumView extends JFrame {
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 musicBrainz = new JButton("Open MusicBrainz");
@ -36,22 +36,21 @@ public class AlbumView extends JFrame{
setLayout(new GridLayout(7,0));
setSize(300, 300);
setResizable(false);
info.setLayout(new GridLayout());
buttons.setLayout(new FlowLayout());
buttons2.setLayout(new FlowLayout());
// info.add(name);
// info.add(listeners);
// info.add(playCount);
// info.add(userPlayCount);
buttons.add(open);
buttons.add(viewArtist);
if(album.getMbid() != null)
buttons2.add(musicBrainz);
buttons2.add(rym);
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
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");
@ -78,12 +77,13 @@ public class AlbumView extends JFrame{
Network.openURL(album.getRymURL());;
}
});
add(name);
add(artist);
add(listeners);
add(playCount);
add(userPlayCount);
// add(info);
add(buttons);
add(buttons2);
}

View File

@ -16,12 +16,13 @@ import sarsoo.fmframework.music.FMObj;
import sarsoo.fmframework.net.Network;
public class FMObjView extends JFrame{
JPanel info = new JPanel();
JPanel buttons = new JPanel();
JLabel name = new JLabel();
JLabel listeners = new JLabel();
JLabel playCount = new JLabel();
JLabel userPlayCount = new JLabel();
JButton open = new JButton("View Online");
JButton musicBrainz = new JButton("Open MusicBrainz");
@ -31,13 +32,11 @@ public class FMObjView extends JFrame{
setLayout(new GridLayout(5,0));
setSize(300, 300);
setResizable(false);
info.setLayout(new GridLayout());
buttons.setLayout(new FlowLayout());
// info.add(name);
// info.add(listeners);
// info.add(playCount);
// info.add(userPlayCount);
buttons.add(open);
if(obj.getMbid() != null)
buttons.add(musicBrainz);
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);

View File

@ -11,9 +11,12 @@ import java.util.Locale;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.music.Track;
import sarsoo.fmframework.net.Network;
import sarsoo.fmframework.util.GetObject;
import sarsoo.fmframework.util.Reference;
@ -21,6 +24,7 @@ import sarsoo.fmframework.util.Reference;
public class MainMenu extends JFrame{
JButton getAlbum = new JButton("Get Album");
JButton getArtist = new JButton("Get Artist");
JButton viewTDE = new JButton("View TDE");
JButton viewBPHQ = new JButton("View BPHQ");
JButton viewLastTrack = new JButton("View Last Track");
@ -28,13 +32,28 @@ public class MainMenu extends JFrame{
public MainMenu() {
super("fmframework");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(2,2));
setLayout(new GridLayout(3,2));
setSize(300, 300);
setResizable(false);
getAlbum.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
GetObject.getAlbum().view();;
Album album = GetObject.getAlbum();
if(album != null) {
album.view();
}else {
JOptionPane.showMessageDialog(null, "No Album Found", "Album Error", JOptionPane.ERROR_MESSAGE);
}
}
});
getArtist.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Artist artist = GetObject.getArtist();
if(artist != null) {
artist.view();
}else {
JOptionPane.showMessageDialog(null, "No Artist Found", "Artist Error", JOptionPane.ERROR_MESSAGE);
}
}
});
viewTDE.addActionListener(new ActionListener() {
@ -49,12 +68,18 @@ public class MainMenu extends JFrame{
});
viewLastTrack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
GetObject.getLastTrack().view();
Track track = GetObject.getLastTrack();
if(track != null) {
track.view();
}else {
JOptionPane.showMessageDialog(null, "No Track Found", "Track Error", JOptionPane.ERROR_MESSAGE);
}
}
});
add(viewLastTrack);
add(getAlbum);
add(getArtist);
add(viewTDE);
add(viewBPHQ);
}

View File

@ -1,6 +1,5 @@
package sarsoo.fmframework.gui;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
@ -17,35 +16,41 @@ import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Track;
import sarsoo.fmframework.net.Network;
public class TrackView extends JFrame{
JPanel info = new JPanel();
public class TrackView extends JFrame {
JPanel buttons = new JPanel();
JPanel buttons2 = new JPanel();
JLabel name = new JLabel();
JLabel album = 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 viewAlbum = new JButton("View Album");
JButton musicBrainz = new JButton("Open MusicBrainz");
JButton genius = new JButton("Open Genius");
public TrackView(Track track) {
super(track.getName());
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new GridLayout(8,0));
setSize(300, 300);
setResizable(false);
info.setLayout(new GridLayout());
setLayout(new GridLayout(8, 1));
// setSize(300, 300);
// setResizable(false);
buttons.setLayout(new FlowLayout());
buttons2.setLayout(new FlowLayout());
buttons.add(open);
if (track.getMbid() != null)
buttons.add(musicBrainz);
buttons2.add(viewArtist);
if (track.getAlbum() != null)
buttons2.add(viewAlbum);
if (track.getArtist() != null)
buttons2.add(genius);
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
@ -56,7 +61,6 @@ public class TrackView extends JFrame{
playCount.setText(numberFormat.format(track.getPlayCount()) + " Scrobbles");
userPlayCount.setText(numberFormat.format(track.getUserPlayCount()) + " Your Scrobbles");
open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Network.openURL(track.getUrl());
@ -69,7 +73,7 @@ public class TrackView extends JFrame{
});
musicBrainz.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Network.openURL(track.getMusicBrainzURL());;
Network.openURL(track.getMusicBrainzURL());
}
});
viewAlbum.addActionListener(new ActionListener() {
@ -77,6 +81,12 @@ public class TrackView extends JFrame{
track.getAlbum().view();
}
});
genius.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Network.openURL(track.getLyricsURL());
}
});
add(name);
add(album);
add(artist);
@ -85,5 +95,6 @@ public class TrackView extends JFrame{
add(userPlayCount);
add(buttons);
add(buttons2);
pack();
}
}

View File

@ -24,11 +24,6 @@ public class Album extends FMObj{
this.artist = artist;
}
public String toString() {
return name + " - " + artist.getName();
}
public Artist getArtist() {
return artist;
}
@ -40,18 +35,34 @@ public class Album extends FMObj{
return album;
}
public ArrayList<Track> getTrackList(){
return trackList;
}
public Track getTrack(int track) {
return trackList.get(track);
}
public ArrayList<Track> getTrackList(){
return trackList;
}
public ArrayList<Tag> getTags(){
return tagList;
}
public String getRymURL() {
return "https://rateyourmusic.com/release/album/" + getArtist().getName().replaceAll(" ", "_").toLowerCase() + "/" + getName().replaceAll(" ", "_").toLowerCase();
}
@Override
public String getMusicBrainzURL() {
return "https://musicbrainz.org/release/" + mbid;
}
@Override
public void view() {
AlbumView view = new AlbumView(this);
view.setVisible(true);
}
@Override
public boolean equals(Object obj) {
if(obj.getClass() != this.getClass()) return false;
@ -64,19 +75,8 @@ public class Album extends FMObj{
return false;
}
public String getRymURL() {
return "https://rateyourmusic.com/release/album/" + getArtist().getName().replaceAll(" ", "_").toLowerCase() + "/" + getName().replaceAll(" ", "_").toLowerCase();
}
@Override
public void view() {
AlbumView view = new AlbumView(this);
view.setVisible(true);
}
@Override
public String getMusicBrainzURL() {
return "https://musicbrainz.org/release/" + mbid;
public String toString() {
return name + " - " + artist.getName();
}
}

View File

@ -22,10 +22,6 @@ public class Artist extends FMObj{
super(name, url, mbid, listeners, playCount, userPlayCount, wiki);
}
public String toString() {
return name;
}
public static Artist getArtist(String name, String username) {
String url = Network.getArtistInfoUrl(name, username);
Document response = Network.getResponse(url);
@ -52,6 +48,15 @@ public class Artist extends FMObj{
return tagList;
}
@Override
public String getMusicBrainzURL() {
return "https://musicbrainz.org/artist/" + mbid;
}
public String getRymURL() {
return "https://rateyourmusic.com/artist/" + getName().replaceAll(" ", "_").toLowerCase();
}
@Override
public boolean equals(Object obj) {
if(obj.getClass() != this.getClass()) return false;
@ -63,14 +68,8 @@ public class Artist extends FMObj{
return false;
}
@Override
public String getMusicBrainzURL() {
return "https://musicbrainz.org/artist/" + mbid;
}
public String getRymURL() {
return "https://rateyourmusic.com/artist/" + getName().replaceAll(" ", "_").toLowerCase();
public String toString() {
return name;
}

View File

@ -1,11 +1,9 @@
package sarsoo.fmframework.music;
import java.awt.Component;
import java.util.ArrayList;
import org.w3c.dom.Document;
import sarsoo.fmframework.gui.FMObjView;
import sarsoo.fmframework.gui.TrackView;
import sarsoo.fmframework.net.Network;
import sarsoo.fmframework.parser.Parser;
@ -48,6 +46,19 @@ public class Track extends FMObj{
this.album = album;
}
@Override
public String getMusicBrainzURL() {
return "https://musicbrainz.org/artist/" + mbid;
}
public String getLyricsURL() {
return Network.getLyricsUrl(name, artist.getName());
}
public Album getAlbum() {
return album;
}
@Override
public boolean equals(Object obj) {
if(obj.getClass() != this.getClass()) return false;
@ -60,18 +71,14 @@ public class Track extends FMObj{
return false;
}
@Override
public String getMusicBrainzURL() {
return "https://musicbrainz.org/artist/" + mbid;
}
@Override
public void view() {
TrackView view = new TrackView(this);
view.setVisible(true);
}
public Album getAlbum() {
return album;
public String toString() {
return name + " - " + artist.getName();
}
}

View File

@ -51,20 +51,12 @@ public class Network {
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
} catch (
MalformedURLException e) {
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ApiCallException e) {
System.out.println(e.getFailureCode() + " " + e.getError());
e.printStackTrace();
System.err.println(e.getFailureCode() + " " + e.getError());
}
return null;
@ -72,7 +64,7 @@ public class Network {
public static String getArtistInfoUrl(String artist, String username) {
String urlString = String.format(
"http://ws.audioscrobbler.com/2.0/?method=artist.getInfo&artist=%s&autocorrect=1&username=%s&api_key=%s",
"http://ws.audioscrobbler.com/2.0/?method=artist.getInfo&artist=%s&autocorrect=0&username=%s&api_key=%s",
artist, username, Key.getKey());
return urlString;
@ -80,7 +72,7 @@ public class Network {
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",
"http://ws.audioscrobbler.com/2.0/?method=artist.getInfo&mbid=%s&autocorrect=0&username=%s&api_key=%s",
mbid, username, Key.getKey());
return urlString;
@ -88,22 +80,31 @@ public class Network {
public static String getAlbumInfoUrl(String album, String artist, String username) {
String urlString = String.format(
"http://ws.audioscrobbler.com/2.0/?method=album.getInfo&album=%s&artist=%s&autocorrect=1&username=%s&api_key=%s",
"http://ws.audioscrobbler.com/2.0/?method=album.getInfo&album=%s&artist=%s&autocorrect=0&username=%s&api_key=%s",
album, artist, username, Key.getKey());
return urlString;
}
public static String getTrackInfoUrl(String name, String artist, String username) {
String urlString = String.format(
"http://ws.audioscrobbler.com/2.0/?method=track.getInfo&track=%s&artist=%s&autocorrect=1&username=%s&api_key=%s",
"http://ws.audioscrobbler.com/2.0/?method=track.getInfo&track=%s&artist=%s&autocorrect=0&username=%s&api_key=%s",
name, artist, username, Key.getKey());
return urlString;
}
public static String getLastTrackUrl(String username) {
String urlString = String.format(
"http://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&limit=1&user=%s&api_key=%s",
username, Key.getKey());
"http://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&limit=1&user=%s&api_key=%s", username,
Key.getKey());
return urlString;
}
public static String getLyricsUrl(String trackName, String artistName) {
String track = trackName.replaceAll(" ", "-");
// track = track.replaceAll("[^a-zA-Z ]", "").toLowerCase();
String artist = artistName.replaceAll(" ", "-");
// artist = artist.replaceAll("[^a-zA-Z ]", "").toLowerCase();
String urlString = String.format("http://genius.com/%s-%s-lyrics", artist, track);
return urlString;
}

View File

@ -24,14 +24,25 @@ import sarsoo.fmframework.util.Reference;
public class Parser {
public static Album parseAlbum(Document doc) {
try {
String name = doc.getElementsByTagName("name").item(0).getTextContent();
String artist = doc.getElementsByTagName("artist").item(0).getTextContent();
String mbid = doc.getElementsByTagName("mbid").item(0).getTextContent();
String mbid = null;
try {
mbid = doc.getElementsByTagName("mbid").item(0).getTextContent();
} catch (NullPointerException e) {
System.err.println("Null Mbid for " + name);
}
String url = doc.getElementsByTagName("url").item(0).getTextContent();
int listeners = Integer.parseInt(doc.getElementsByTagName("listeners").item(0).getTextContent());
int playCount = Integer.parseInt(doc.getElementsByTagName("playcount").item(0).getTextContent());
int userPlayCount = Integer.parseInt(doc.getElementsByTagName("userplaycount").item(0).getTextContent());
int userPlayCount = 0;
try {
userPlayCount = Integer.parseInt(doc.getElementsByTagName("userplaycount").item(0).getTextContent());
} catch (Exception e) {
System.err.println("Couldn't parse userPlayCount, possibly unscrobbled");
}
// Node trackListNode = doc.getElementsByTagName("tracks").item(0);
// NodeList trackNodeList = trackListNode.getChildNodes();
@ -70,47 +81,83 @@ public class Parser {
Album album = new Album(name, url, mbid, artistObj, listeners, playCount, userPlayCount, wiki);
return album;
} catch (NullPointerException e) {
System.err.println("Could Not Parse Album");
return null;
}
}
public static Artist parseArtist(Document doc) {
try {
String name = doc.getElementsByTagName("name").item(0).getTextContent();
String mbid = doc.getElementsByTagName("mbid").item(0).getTextContent();
String mbid = null;
try {
mbid = doc.getElementsByTagName("mbid").item(0).getTextContent();
} catch (NullPointerException e) {
System.err.println("Null Mbid for " + name);
}
String url = doc.getElementsByTagName("url").item(0).getTextContent();
// String streamable =
// doc.getElementsByTagName("streamable").item(0).getTextContent();
int listeners = Integer.parseInt(doc.getElementsByTagName("listeners").item(0).getTextContent());
int playCount = Integer.parseInt(doc.getElementsByTagName("playcount").item(0).getTextContent());
int userPlayCount = Integer.parseInt(doc.getElementsByTagName("userplaycount").item(0).getTextContent());
int userPlayCount = 0;
try {
userPlayCount = Integer.parseInt(doc.getElementsByTagName("userplaycount").item(0).getTextContent());
} catch (Exception e) {
System.err.println("Couldn't parse userPlayCount, possibly unscrobbled");
}
// System.out.println(listeners);
Artist artist = new Artist(name, url, mbid, listeners, playCount, userPlayCount, false, false, null);
return artist;
} catch (NullPointerException e) {
System.err.println("Could Not Parse Artist");
return null;
}
}
public static Track parseTrack(Document doc) {
try {
String name = doc.getElementsByTagName("name").item(0).getTextContent();
String artistName = doc.getElementsByTagName("artist").item(0).getFirstChild().getTextContent();
String mbid = doc.getElementsByTagName("mbid").item(0).getTextContent();
String mbid = null;
try {
mbid = doc.getElementsByTagName("mbid").item(0).getTextContent();
} catch (NullPointerException e) {
System.err.println("Null Mbid for " + name + " - " + artistName);
}
String url = doc.getElementsByTagName("url").item(0).getTextContent();
int listeners = Integer.parseInt(doc.getElementsByTagName("listeners").item(0).getTextContent());
int playCount = Integer.parseInt(doc.getElementsByTagName("playcount").item(0).getTextContent());
int userPlayCount = Integer.parseInt(doc.getElementsByTagName("userplaycount").item(0).getTextContent());
int userPlayCount = 0;
try {
userPlayCount = Integer.parseInt(doc.getElementsByTagName("userplaycount").item(0).getTextContent());
} catch (Exception e) {
System.err.println("Couldn't parse userPlayCount, possibly unscrobbled");
}
// String albumName =
// doc.getElementsByTagName("album").item(0).getTextContent();
// System.out.println(albumName);
Artist artistObj = Artist.getArtist(artistName, Reference.getUserName());
// System.out.println(userPlayCount);
Track track = new Track(name, url, mbid, artistObj, listeners, playCount, userPlayCount, null);
return track;
} catch (NullPointerException e) {
System.err.println("Could Not Parse Track");
return null;
}
}
public static Track parseLastTrack(Document doc) {
try {
String name = doc.getElementsByTagName("name").item(0).getTextContent();
String artistName = doc.getElementsByTagName("artist").item(0).getTextContent();
@ -124,6 +171,10 @@ public class Parser {
track.setAlbum(album);
return track;
} catch (NullPointerException e) {
System.err.println("Could Not Parse Track");
return null;
}
}

View File

@ -1,20 +0,0 @@
package sarsoo.fmframework.test;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.net.Network;
import sarsoo.fmframework.parser.AlbumParser;
class AlbumParserTest {
@Test
void test() {
//Document doc = Network.apiAlbumInfoCall("Pink Floyd", "The Wall", "sarsoo");
//Album album = AlbumParser.parseAlbum(doc);
}
}

View File

@ -20,4 +20,11 @@ class AlbumTest {
assertEquals(album.getArtist().getName(), "Eminem");
}
@Test
void testListeners() {
Album album = Album.getAlbum("Recovery", "Eminem", "sarsoo");
assertNotNull(album.getListeners());
}
}

View File

@ -6,6 +6,7 @@ import sarsoo.fmframework.gui.ArtistListView;
import sarsoo.fmframework.music.Artist;
public class ArtistList extends ArrayList<Artist> {
private String groupName = null;
public ArtistList() {

View File

@ -5,6 +5,7 @@ import javax.swing.JOptionPane;
import org.w3c.dom.Document;
import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.music.Track;
import sarsoo.fmframework.net.Network;
import sarsoo.fmframework.parser.Parser;
@ -12,8 +13,21 @@ import sarsoo.fmframework.parser.Parser;
public class GetObject {
public static Album getAlbum() {
String artistName = JOptionPane.showInputDialog(null, "Enter Artist Name");
if (artistName != null) {
String albumName = JOptionPane.showInputDialog(null, "Enter Album Name");
return Album.getAlbum(albumName, artistName, "sarsoo");
if (albumName != null) {
return Album.getAlbum(albumName, artistName, Reference.getUserName());
}
}
return null;
}
public static Artist getArtist() {
String artistName = JOptionPane.showInputDialog(null, "Enter Artist Name");
if (artistName != null) {
return Artist.getArtist(artistName, Reference.getUserName());
}
return null;
}
public static Track getLastTrack() {

View File

@ -4,6 +4,7 @@ import sarsoo.fmframework.music.Artist;
public class Reference {
private static String userName;
private static boolean isHeadless = true;
public static String getUserName() {
return userName;
@ -13,6 +14,14 @@ public class Reference {
userName = userNameIn;
}
public static boolean getIsHeadless() {
return isHeadless;
}
public static void setIsHeadless(boolean headlessIn) {
isHeadless = headlessIn;
}
public static ArtistList getTDE() {
ArtistList tde = new ArtistList("TDE");