cleared unused imports, added rym handling

moved link handling from object classes to network class, objects now
return assembled url strings
fmobj now abstract to add musicbeanz url returning
added artist to track from parser
This commit is contained in:
aj 2018-03-26 13:48:41 -07:00
parent 6ceb77393c
commit 99325cd5f9
10 changed files with 132 additions and 63 deletions

View File

@ -1,7 +1,5 @@
package sarsoo.fmframework.drive; package sarsoo.fmframework.drive;
import sarsoo.fmframework.gui.AlbumView;
import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.util.GetObject; import sarsoo.fmframework.util.GetObject;
public class Driver { public class Driver {
@ -10,6 +8,8 @@ public class Driver {
GetObject.getAlbum().view(); GetObject.getAlbum().view();
//Network.getRecentTracaksUrl("sarsoo");
} }
} }

View File

@ -1,50 +1,58 @@
package sarsoo.fmframework.gui; package sarsoo.fmframework.gui;
import java.awt.Desktop;
import java.awt.FlowLayout; import java.awt.FlowLayout;
import java.awt.GridLayout; import java.awt.GridLayout;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.net.URI;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.util.Locale; import java.util.Locale;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel; import javax.swing.JPanel;
import sarsoo.fmframework.music.Album; import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.FMObj; import sarsoo.fmframework.net.Network;
public class AlbumView extends JFrame{ public class AlbumView extends JFrame{
JPanel info = new JPanel(); JPanel info = new JPanel();
JPanel buttons = new JPanel(); JPanel buttons = new JPanel();
JPanel buttons2 = new JPanel();
JLabel name = new JLabel(); JLabel name = new JLabel();
JLabel artist = new JLabel();
JLabel listeners = new JLabel(); JLabel listeners = new JLabel();
JLabel playCount = new JLabel(); JLabel playCount = new JLabel();
JLabel userPlayCount = new JLabel(); JLabel userPlayCount = new JLabel();
JButton open = new JButton("View Online"); 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 AlbumView(Album album) { public AlbumView(Album album) {
super(album.toString()); super(album.getName());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new GridLayout(5,0)); setLayout(new GridLayout(7,0));
setSize(300, 200); setSize(300, 300);
setResizable(false); setResizable(false);
info.setLayout(new GridLayout()); info.setLayout(new GridLayout());
buttons.setLayout(new FlowLayout()); buttons.setLayout(new FlowLayout());
buttons2.setLayout(new FlowLayout());
// info.add(name); // info.add(name);
// info.add(listeners); // info.add(listeners);
// info.add(playCount); // info.add(playCount);
// info.add(userPlayCount); // info.add(userPlayCount);
buttons.add(open); buttons.add(open);
buttons.add(viewArtist);
buttons2.add(musicBeanz);
buttons2.add(rym);
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US); NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
name.setText(album.getName()); name.setText(album.getName());
name.setHorizontalTextPosition(JLabel.CENTER); name.setHorizontalTextPosition(JLabel.CENTER);
artist.setText(album.getArtist().getName());
listeners.setText(numberFormat.format(album.getListeners()) + " Listeners"); listeners.setText(numberFormat.format(album.getListeners()) + " Listeners");
playCount.setText(numberFormat.format(album.getPlayCount()) + " Scrobbles"); playCount.setText(numberFormat.format(album.getPlayCount()) + " Scrobbles");
userPlayCount.setText(numberFormat.format(album.getUserPlayCount()) + " Your Scrobbles"); userPlayCount.setText(numberFormat.format(album.getUserPlayCount()) + " Your Scrobbles");
@ -52,20 +60,31 @@ public class AlbumView extends JFrame{
open.addActionListener(new ActionListener() { open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
try { Network.openURL(album.getUrl());
Desktop desktop = java.awt.Desktop.getDesktop();
URI oURL = new URI(album.getUrl());
desktop.browse(oURL);
} catch (Exception e) {
e.printStackTrace();
} }
});
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(name);
add(artist);
add(listeners); add(listeners);
add(playCount); add(playCount);
add(userPlayCount); add(userPlayCount);
// add(info); // add(info);
add(buttons); add(buttons);
add(buttons2);
} }
} }

View File

@ -1,11 +1,9 @@
package sarsoo.fmframework.gui; package sarsoo.fmframework.gui;
import java.awt.Desktop;
import java.awt.FlowLayout; import java.awt.FlowLayout;
import java.awt.GridLayout; import java.awt.GridLayout;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.net.URI;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.util.Locale; import java.util.Locale;
@ -15,6 +13,7 @@ import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
import sarsoo.fmframework.music.FMObj; import sarsoo.fmframework.music.FMObj;
import sarsoo.fmframework.net.Network;
public class FMObjView extends JFrame{ public class FMObjView extends JFrame{
JPanel info = new JPanel(); JPanel info = new JPanel();
@ -24,12 +23,13 @@ public class FMObjView extends JFrame{
JLabel playCount = new JLabel(); JLabel playCount = new JLabel();
JLabel userPlayCount = new JLabel(); JLabel userPlayCount = new JLabel();
JButton open = new JButton("View Online"); JButton open = new JButton("View Online");
JButton musicBeanz = new JButton("Open MusicBeanz");
public FMObjView(FMObj obj) { public FMObjView(FMObj obj) {
super(obj.toString()); super(obj.toString());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new GridLayout(5,0)); setLayout(new GridLayout(5,0));
setSize(300, 200); setSize(300, 300);
setResizable(false); setResizable(false);
info.setLayout(new GridLayout()); info.setLayout(new GridLayout());
buttons.setLayout(new FlowLayout()); buttons.setLayout(new FlowLayout());
@ -38,6 +38,7 @@ public class FMObjView extends JFrame{
// info.add(playCount); // info.add(playCount);
// info.add(userPlayCount); // info.add(userPlayCount);
buttons.add(open); buttons.add(open);
buttons.add(musicBeanz);
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US); NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
@ -50,13 +51,12 @@ public class FMObjView extends JFrame{
open.addActionListener(new ActionListener() { open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
try { Network.openURL(obj.getUrl());
Desktop desktop = java.awt.Desktop.getDesktop();
URI oURL = new URI(obj.getUrl());
desktop.browse(oURL);
} catch (Exception e) {
e.printStackTrace();
} }
});
musicBeanz.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Network.openURL(obj.getMusicBeanzURL());;
} }
}); });
add(name); add(name);

View File

@ -63,9 +63,19 @@ public class Album extends FMObj{
return false; return false;
} }
public String getRymURL() {
return "https://rateyourmusic.com/release/album/" + getArtist().getName().replaceAll(" ", "_").toLowerCase() + "/" + getName().replaceAll(" ", "_").toLowerCase();
}
@Override @Override
public void view() { public void view() {
AlbumView view = new AlbumView(this); AlbumView view = new AlbumView(this);
view.setVisible(true); view.setVisible(true);
} }
@Override
public String getMusicBeanzURL() {
return "https://musicbrainz.org/release/" + mbid;
}
} }

View File

@ -55,4 +55,16 @@ public class Artist extends FMObj{
return false; return false;
} }
@Override
public String getMusicBeanzURL() {
return "https://musicbrainz.org/artist/" + mbid;
}
public String getRymURL() {
return "https://rateyourmusic.com/artist/" + getName().replaceAll(" ", "_").toLowerCase();
}
} }

View File

@ -2,7 +2,7 @@ package sarsoo.fmframework.music;
import sarsoo.fmframework.gui.FMObjView; import sarsoo.fmframework.gui.FMObjView;
public class FMObj { public abstract class FMObj {
protected String name; protected String name;
protected String url; protected String url;
@ -61,4 +61,6 @@ public class FMObj {
return wiki; return wiki;
} }
abstract public String getMusicBeanzURL();
} }

View File

@ -21,8 +21,9 @@ public class Track extends FMObj{
this.artist = new Artist(artist); this.artist = new Artist(artist);
} }
public Track(String name, String url, String mbid, int listeners, int playCount, int userPlayCount, Wiki wiki) { public Track(String name, String url, String mbid, Artist artist, int listeners, int playCount, int userPlayCount, Wiki wiki) {
super(name, url, mbid, listeners, playCount, userPlayCount, wiki); super(name, url, mbid, listeners, playCount, userPlayCount, wiki);
this.artist = artist;
} }
public static Track getTrack(String name, String artist, String username) { public static Track getTrack(String name, String artist, String username) {
@ -51,4 +52,9 @@ public class Track extends FMObj{
return false; return false;
} }
@Override
public String getMusicBeanzURL() {
return "https://musicbrainz.org/artist/" + mbid;
}
} }

View File

@ -1,9 +1,11 @@
package sarsoo.fmframework.net; package sarsoo.fmframework.net;
import java.awt.Desktop;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL; import java.net.URL;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
@ -69,7 +71,6 @@ public class Network {
artist, username, Key.getKey()); artist, username, Key.getKey());
return urlString; return urlString;
} }
public static String getAlbumInfoUrl(String album, String artist, String username) { public static String getAlbumInfoUrl(String album, String artist, String username) {
@ -85,4 +86,34 @@ public class Network {
name, artist, username, Key.getKey()); name, artist, username, Key.getKey());
return urlString; return urlString;
} }
public static void openURL(String url) {
try {
Desktop desktop = java.awt.Desktop.getDesktop();
URI oURL = new URI(url);
desktop.browse(oURL);
} catch (Exception e) {
e.printStackTrace();
}
}
// public static String getRecentTracaksUrl(String username) {
//// Date date = new Date();
//// Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
//// Calendar cal2 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
////
//// cal.set(cal.YEAR, cal.MONTH, cal.DATE);
//// System.out.println(cal.getTime());
// //System.out.println(cal2.getTime());
//
// String urlString = String.format(
// "http://ws.audioscrobbler.com/2.0/"
// + "?method=user.getRecentTracks&"
// + "user=%s&"
// + "limit = 200&"
// + "from=%d&"
// + "api_key=%s",
// username, Key.getKey());
// return urlString;
// }
} }

View File

@ -1,18 +1,9 @@
package sarsoo.fmframework.parser; package sarsoo.fmframework.parser;
import javax.xml.parsers.*;
import java.io.IOException;
import java.io.InputStream;
import sarsoo.fmframework.music.Album; import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Artist; import sarsoo.fmframework.music.Artist;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class AlbumParser { public class AlbumParser {

View File

@ -1,8 +1,6 @@
package sarsoo.fmframework.parser; package sarsoo.fmframework.parser;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import sarsoo.fmframework.music.Album; import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Artist; import sarsoo.fmframework.music.Artist;
@ -40,10 +38,10 @@ public class Parser {
// } // }
// } // }
Node wikiNode = doc.getElementsByTagName("wiki").item(0); // Node wikiNode = doc.getElementsByTagName("wiki").item(0);
String published = wikiNode.getFirstChild().getTextContent(); // String published = wikiNode.getFirstChild().getTextContent();
String summary = wikiNode.getFirstChild().getNextSibling().getTextContent(); // String summary = wikiNode.getFirstChild().getNextSibling().getTextContent();
String content = wikiNode.getFirstChild().getNextSibling().getNextSibling().getTextContent(); // String content = wikiNode.getFirstChild().getNextSibling().getNextSibling().getTextContent();
// System.out.println(published); // System.out.println(published);
// System.out.println(summary); // System.out.println(summary);
@ -62,7 +60,7 @@ public class Parser {
String name = doc.getElementsByTagName("name").item(0).getTextContent(); String name = doc.getElementsByTagName("name").item(0).getTextContent();
String mbid = doc.getElementsByTagName("mbid").item(0).getTextContent(); String mbid = doc.getElementsByTagName("mbid").item(0).getTextContent();
String url = doc.getElementsByTagName("url").item(0).getTextContent(); String url = doc.getElementsByTagName("url").item(0).getTextContent();
String streamable = doc.getElementsByTagName("streamable").item(0).getTextContent(); // String streamable = doc.getElementsByTagName("streamable").item(0).getTextContent();
int listeners = Integer.parseInt(doc.getElementsByTagName("listeners").item(0).getTextContent()); int listeners = Integer.parseInt(doc.getElementsByTagName("listeners").item(0).getTextContent());
int playCount = Integer.parseInt(doc.getElementsByTagName("playcount").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 = Integer.parseInt(doc.getElementsByTagName("userplaycount").item(0).getTextContent());
@ -87,7 +85,7 @@ public class Parser {
// System.out.println(userPlayCount); // System.out.println(userPlayCount);
Track track = new Track(name, url, mbid, listeners, playCount, userPlayCount, null); Track track = new Track(name, url, mbid, artistObj, listeners, playCount, userPlayCount, null);
return track; return track;
} }