added get last track and track view, added main menu
This commit is contained in:
parent
383f76ffe3
commit
2134e358c1
@ -1,17 +1,16 @@
|
||||
package sarsoo.fmframework.drive;
|
||||
|
||||
import sarsoo.fmframework.music.Artist;
|
||||
import sarsoo.fmframework.util.ArtistList;
|
||||
import sarsoo.fmframework.util.GetObject;
|
||||
import sarsoo.fmframework.gui.MainMenu;
|
||||
import sarsoo.fmframework.util.Reference;
|
||||
|
||||
public class Driver {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Reference.setUserName("sarsoo");
|
||||
Reference.setUserName("Sarsoo");
|
||||
|
||||
//Network.getRecentTracaksUrl("sarsoo");
|
||||
Reference.getTDE().view();
|
||||
MainMenu main = new MainMenu();
|
||||
main.setVisible(true);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
61
fmframework/src/sarsoo/fmframework/gui/MainMenu.java
Normal file
61
fmframework/src/sarsoo/fmframework/gui/MainMenu.java
Normal file
@ -0,0 +1,61 @@
|
||||
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.net.Network;
|
||||
import sarsoo.fmframework.util.GetObject;
|
||||
import sarsoo.fmframework.util.Reference;
|
||||
|
||||
public class MainMenu extends JFrame{
|
||||
|
||||
JButton getAlbum = new JButton("Get Album");
|
||||
JButton viewTDE = new JButton("View TDE");
|
||||
JButton viewBPHQ = new JButton("View BPHQ");
|
||||
JButton viewLastTrack = new JButton("View Last Track");
|
||||
|
||||
public MainMenu() {
|
||||
super("fmframework");
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setLayout(new GridLayout(2,2));
|
||||
setSize(300, 300);
|
||||
setResizable(false);
|
||||
|
||||
getAlbum.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
GetObject.getAlbum().view();;
|
||||
}
|
||||
});
|
||||
viewTDE.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
Reference.getTDE().view();
|
||||
}
|
||||
});
|
||||
viewBPHQ.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
Reference.getBPHQ().view();
|
||||
}
|
||||
});
|
||||
viewLastTrack.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
GetObject.getLastTrack().view();
|
||||
}
|
||||
});
|
||||
|
||||
add(viewLastTrack);
|
||||
add(getAlbum);
|
||||
add(viewTDE);
|
||||
add(viewBPHQ);
|
||||
}
|
||||
}
|
89
fmframework/src/sarsoo/fmframework/gui/TrackView.java
Normal file
89
fmframework/src/sarsoo/fmframework/gui/TrackView.java
Normal file
@ -0,0 +1,89 @@
|
||||
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.Track;
|
||||
import sarsoo.fmframework.net.Network;
|
||||
|
||||
public class TrackView extends JFrame{
|
||||
JPanel info = new JPanel();
|
||||
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");
|
||||
|
||||
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());
|
||||
buttons.setLayout(new FlowLayout());
|
||||
buttons2.setLayout(new FlowLayout());
|
||||
|
||||
buttons.add(open);
|
||||
buttons.add(musicBrainz);
|
||||
buttons2.add(viewArtist);
|
||||
buttons2.add(viewAlbum);
|
||||
|
||||
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
|
||||
|
||||
name.setText(track.getName());
|
||||
album.setText(track.getAlbum().getName());
|
||||
artist.setText(track.getArtist().getName());
|
||||
listeners.setText(numberFormat.format(track.getListeners()) + " Listeners");
|
||||
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());
|
||||
}
|
||||
});
|
||||
viewArtist.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
track.getArtist().view();
|
||||
}
|
||||
});
|
||||
musicBrainz.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
Network.openURL(track.getMusicBrainzURL());;
|
||||
}
|
||||
});
|
||||
viewAlbum.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
track.getAlbum().view();
|
||||
}
|
||||
});
|
||||
add(name);
|
||||
add(album);
|
||||
add(artist);
|
||||
add(listeners);
|
||||
add(playCount);
|
||||
add(userPlayCount);
|
||||
add(buttons);
|
||||
add(buttons2);
|
||||
}
|
||||
}
|
@ -1,9 +1,12 @@
|
||||
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;
|
||||
|
||||
@ -41,6 +44,10 @@ public class Track extends FMObj{
|
||||
return tagList;
|
||||
}
|
||||
|
||||
public void setAlbum(Album album) {
|
||||
this.album = album;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if(obj.getClass() != this.getClass()) return false;
|
||||
@ -57,4 +64,14 @@ public class Track extends FMObj{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -100,6 +100,13 @@ public class Network {
|
||||
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());
|
||||
return urlString;
|
||||
}
|
||||
|
||||
public static void openURL(String url) {
|
||||
try {
|
||||
Desktop desktop = java.awt.Desktop.getDesktop();
|
||||
|
@ -1,11 +1,25 @@
|
||||
package sarsoo.fmframework.parser;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
|
||||
import javax.xml.transform.OutputKeys;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.NamedNodeMap;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
import sarsoo.fmframework.music.Album;
|
||||
import sarsoo.fmframework.music.Artist;
|
||||
import sarsoo.fmframework.music.Track;
|
||||
import sarsoo.fmframework.music.Wiki;
|
||||
import sarsoo.fmframework.util.Reference;
|
||||
|
||||
public class Parser {
|
||||
|
||||
@ -19,35 +33,39 @@ public class Parser {
|
||||
int playCount = Integer.parseInt(doc.getElementsByTagName("playcount").item(0).getTextContent());
|
||||
int userPlayCount = Integer.parseInt(doc.getElementsByTagName("userplaycount").item(0).getTextContent());
|
||||
|
||||
// Node trackListNode = doc.getElementsByTagName("tracks").item(0);
|
||||
// NodeList trackNodeList = trackListNode.getChildNodes();
|
||||
// if (trackListNode != null) {
|
||||
// int counter = 0;
|
||||
// while (trackNodeList.item(counter) != null) {
|
||||
// Node track = trackNodeList.item(counter);
|
||||
// //System.out.println(track.getTextContent());
|
||||
// String trackName = track.getFirstChild().getTextContent();
|
||||
// String trackUrl = track.getFirstChild().getNextSibling().getNextSibling().getTextContent();
|
||||
// //String trackDuration = track.getFirstChild().getNextSibling().getNextSibling().getNextSibling().getNextSibling().getTextContent();
|
||||
// int trackNumber = Integer.parseInt(track.getAttributes().getNamedItem("rank").getNodeValue());
|
||||
// // String trackName =
|
||||
// // trackListNode.getFirstChild().getFirstChild().getTextContent();
|
||||
// // String trackUrl = trackListNode.getNextSibling().getTextContent();
|
||||
// System.out.println(trackUrl);
|
||||
// counter++;
|
||||
// }
|
||||
// }
|
||||
// Node trackListNode = doc.getElementsByTagName("tracks").item(0);
|
||||
// NodeList trackNodeList = trackListNode.getChildNodes();
|
||||
// if (trackListNode != null) {
|
||||
// int counter = 0;
|
||||
// while (trackNodeList.item(counter) != null) {
|
||||
// Node track = trackNodeList.item(counter);
|
||||
// //System.out.println(track.getTextContent());
|
||||
// String trackName = track.getFirstChild().getTextContent();
|
||||
// String trackUrl =
|
||||
// track.getFirstChild().getNextSibling().getNextSibling().getTextContent();
|
||||
// //String trackDuration =
|
||||
// track.getFirstChild().getNextSibling().getNextSibling().getNextSibling().getNextSibling().getTextContent();
|
||||
// int trackNumber =
|
||||
// Integer.parseInt(track.getAttributes().getNamedItem("rank").getNodeValue());
|
||||
// // String trackName =
|
||||
// // trackListNode.getFirstChild().getFirstChild().getTextContent();
|
||||
// // String trackUrl = trackListNode.getNextSibling().getTextContent();
|
||||
// System.out.println(trackUrl);
|
||||
// counter++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// Node wikiNode = doc.getElementsByTagName("wiki").item(0);
|
||||
// String published = wikiNode.getFirstChild().getTextContent();
|
||||
// String summary = wikiNode.getFirstChild().getNextSibling().getTextContent();
|
||||
// String content = wikiNode.getFirstChild().getNextSibling().getNextSibling().getTextContent();
|
||||
// Node wikiNode = doc.getElementsByTagName("wiki").item(0);
|
||||
// String published = wikiNode.getFirstChild().getTextContent();
|
||||
// String summary = wikiNode.getFirstChild().getNextSibling().getTextContent();
|
||||
// String content =
|
||||
// wikiNode.getFirstChild().getNextSibling().getNextSibling().getTextContent();
|
||||
|
||||
// System.out.println(published);
|
||||
// System.out.println(summary);
|
||||
// System.out.println(content);
|
||||
|
||||
Artist artistObj = Artist.getArtist(artist, "Sarsoo");
|
||||
Artist artistObj = Artist.getArtist(artist, Reference.getUserName());
|
||||
Wiki wiki = new Wiki();
|
||||
|
||||
Album album = new Album(name, url, mbid, artistObj, listeners, playCount, userPlayCount, wiki);
|
||||
@ -60,12 +78,13 @@ public class Parser {
|
||||
String name = doc.getElementsByTagName("name").item(0).getTextContent();
|
||||
String mbid = doc.getElementsByTagName("mbid").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 playCount = Integer.parseInt(doc.getElementsByTagName("playcount").item(0).getTextContent());
|
||||
int userPlayCount = Integer.parseInt(doc.getElementsByTagName("userplaycount").item(0).getTextContent());
|
||||
|
||||
//System.out.println(listeners);
|
||||
// System.out.println(listeners);
|
||||
|
||||
Artist artist = new Artist(name, url, mbid, listeners, playCount, userPlayCount, false, false, null);
|
||||
return artist;
|
||||
@ -81,12 +100,97 @@ public class Parser {
|
||||
int playCount = Integer.parseInt(doc.getElementsByTagName("playcount").item(0).getTextContent());
|
||||
int userPlayCount = Integer.parseInt(doc.getElementsByTagName("userplaycount").item(0).getTextContent());
|
||||
|
||||
Artist artistObj = Artist.getArtist(artistName, "sarsoo");
|
||||
Artist artistObj = Artist.getArtist(artistName, Reference.getUserName());
|
||||
|
||||
// System.out.println(userPlayCount);
|
||||
// System.out.println(userPlayCount);
|
||||
|
||||
Track track = new Track(name, url, mbid, artistObj, listeners, playCount, userPlayCount, null);
|
||||
return track;
|
||||
|
||||
}
|
||||
|
||||
public static Track parseLastTrack(Document doc) {
|
||||
|
||||
String name = doc.getElementsByTagName("name").item(0).getTextContent();
|
||||
|
||||
String artistName = doc.getElementsByTagName("artist").item(0).getTextContent();
|
||||
|
||||
String albumName = doc.getElementsByTagName("album").item(0).getTextContent();
|
||||
|
||||
Track track = Track.getTrack(name, artistName, Reference.getUserName());
|
||||
|
||||
Album album = Album.getAlbum(albumName, artistName, Reference.getUserName());
|
||||
|
||||
track.setAlbum(album);
|
||||
|
||||
return track;
|
||||
|
||||
}
|
||||
|
||||
public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
|
||||
TransformerFactory tf = TransformerFactory.newInstance();
|
||||
Transformer transformer = tf.newTransformer();
|
||||
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
|
||||
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
|
||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
|
||||
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
|
||||
|
||||
transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
|
||||
}
|
||||
|
||||
public static void stripSpace(Node node) {
|
||||
Node child = node.getFirstChild();
|
||||
while (child != null) {
|
||||
// save the sibling of the node that will
|
||||
// perhaps be removed and set to null
|
||||
Node c = child.getNextSibling();
|
||||
if ((child.getNodeType() == Node.TEXT_NODE && child.getNodeValue().trim().length() == 0)
|
||||
|| ((child.getNodeType() != Node.TEXT_NODE) && (child.getNodeType() != Node.ELEMENT_NODE)))
|
||||
node.removeChild(child);
|
||||
else // process children recursively
|
||||
stripSpace(child);
|
||||
child = c;
|
||||
}
|
||||
}
|
||||
|
||||
public static void compact(Node node, String indent) {
|
||||
if (node == null)
|
||||
return;
|
||||
switch (node.getNodeType()) {
|
||||
case Node.ELEMENT_NODE: {
|
||||
System.out.print(node.getNodeName() + '[');
|
||||
indent += blanks(node.getNodeName().length() + 1);
|
||||
NamedNodeMap attrs = node.getAttributes();
|
||||
boolean first = true;
|
||||
for (int i = 0; i < attrs.getLength(); i++) {
|
||||
if (!first)
|
||||
System.out.print('\n' + indent);
|
||||
System.out.print('@' + attrs.item(i).getNodeName() + '[' + attrs.item(i).getNodeValue() + ']');
|
||||
first = false;
|
||||
}
|
||||
for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
|
||||
if (!first)
|
||||
System.out.print('\n' + indent);
|
||||
compact(child, indent);
|
||||
first = false;
|
||||
}
|
||||
System.out.print(']');
|
||||
break;
|
||||
}
|
||||
case Node.TEXT_NODE: {
|
||||
System.out.print(node.getNodeValue().trim());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// production of string of spaces with a lazy StringBuffer
|
||||
private static StringBuffer blanks = new StringBuffer();
|
||||
|
||||
private static String blanks(int n) {
|
||||
for (int i = blanks.length(); i < n; i++)
|
||||
blanks.append(' ');
|
||||
return blanks.substring(0, n);
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,12 @@ package sarsoo.fmframework.util;
|
||||
|
||||
import javax.swing.JOptionPane;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import sarsoo.fmframework.music.Album;
|
||||
import sarsoo.fmframework.music.Track;
|
||||
import sarsoo.fmframework.net.Network;
|
||||
import sarsoo.fmframework.parser.Parser;
|
||||
|
||||
public class GetObject {
|
||||
public static Album getAlbum() {
|
||||
@ -11,4 +16,15 @@ public class GetObject {
|
||||
return Album.getAlbum(albumName, artistName, "sarsoo");
|
||||
}
|
||||
|
||||
public static Track getLastTrack() {
|
||||
|
||||
String url = Network.getLastTrackUrl(Reference.getUserName());
|
||||
Document doc = Network.getResponse(url);
|
||||
Parser.stripSpace(doc.getDocumentElement());
|
||||
Track track = Parser.parseLastTrack(doc);
|
||||
|
||||
return track;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -28,4 +28,16 @@ public class Reference {
|
||||
return tde;
|
||||
}
|
||||
|
||||
public static ArtistList getBPHQ() {
|
||||
ArtistList bphq = new ArtistList("British Post Hardcore Quintet");
|
||||
|
||||
bphq.add(Artist.getArtistByMbid("98edd2f1-d136-4c47-ab9b-c31839dd1d98", Reference.getUserName()));
|
||||
bphq.add(Artist.getArtist("Lower Than Atlantis", Reference.getUserName()));
|
||||
bphq.add(Artist.getArtist("Mallory Knox", Reference.getUserName()));
|
||||
bphq.add(Artist.getArtist("Don Broco", Reference.getUserName()));
|
||||
bphq.add(Artist.getArtist("Moose Blood", Reference.getUserName()));
|
||||
|
||||
return bphq;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user