started wiki parsing and viewing

This commit is contained in:
aj 2018-03-29 14:06:39 -07:00
parent 853a8813ac
commit 167d93b30a
9 changed files with 350 additions and 166 deletions

View File

@ -1,7 +1,5 @@
package sarsoo.fmframework.error;
import sarsoo.fmframework.util.Reference;
public class ApiCallException extends Exception {
private int failureCode;

View File

@ -31,6 +31,7 @@ public class AlbumView extends JFrame {
JButton open = new JButton("View Online");
JButton viewArtist = new JButton("View Artist");
JButton viewWiki = new JButton("View Wiki");
JButton musicBrainz = new JButton("Open MusicBrainz");
JButton rym = new JButton("Open RYM");
@ -49,6 +50,8 @@ public class AlbumView extends JFrame {
buttons.add(open);
buttons.add(viewArtist);
if (album.getWiki() != null)
buttons2.add(viewWiki);
if (album.getMbid() != null)
buttons2.add(musicBrainz);
buttons2.add(rym);
@ -72,6 +75,11 @@ public class AlbumView extends JFrame {
album.getArtist().view();
}
});
viewWiki.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
album.getWiki().view(album.getName());
}
});
musicBrainz.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Network.openURL(album.getMusicBrainzURL());

View File

@ -23,6 +23,7 @@ public class FMObjView extends JFrame{
JLabel playCount = new JLabel();
JLabel userPlayCount = new JLabel();
JButton viewWiki = new JButton("View Wiki");
JButton open = new JButton("View Online");
JButton musicBrainz = new JButton("Open MusicBrainz");
@ -31,11 +32,13 @@ public class FMObjView extends JFrame{
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new GridLayout(5,0));
setSize(300, 300);
setResizable(false);
// setResizable(false);
buttons.setLayout(new FlowLayout());
buttons.add(open);
if (obj.getWiki() != null)
buttons.add(viewWiki);
if(obj.getMbid() != null)
buttons.add(musicBrainz);
@ -47,7 +50,11 @@ public class FMObjView extends JFrame{
playCount.setText(numberFormat.format(obj.getPlayCount()) + " Scrobbles");
userPlayCount.setText(numberFormat.format(obj.getUserPlayCount()) + " Your Scrobbles");
viewWiki.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
obj.getWiki().view(obj.getName());
}
});
open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Network.openURL(obj.getUrl());

View File

@ -29,6 +29,7 @@ public class TrackView extends JFrame {
JButton open = new JButton("View Online");
JButton viewArtist = new JButton("View Artist");
JButton viewAlbum = new JButton("View Album");
JButton viewWiki = new JButton("View Wiki");
JButton musicBrainz = new JButton("Open MusicBrainz");
JButton genius = new JButton("Open Genius");
@ -43,6 +44,8 @@ public class TrackView extends JFrame {
buttons2.setLayout(new FlowLayout());
buttons.add(open);
if (track.getWiki() != null)
buttons.add(viewWiki);
if (track.getMbid() != null)
buttons.add(musicBrainz);
buttons2.add(viewArtist);
@ -66,6 +69,11 @@ public class TrackView extends JFrame {
Network.openURL(track.getUrl());
}
});
viewWiki.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
track.getWiki().view(track.getName());
}
});
viewArtist.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
track.getArtist().view();

View File

@ -0,0 +1,153 @@
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.ArrayList;
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.FMObj;
import sarsoo.fmframework.music.Track;
import sarsoo.fmframework.music.Wiki;
import sarsoo.fmframework.net.Network;
public class WikiView extends JFrame {
// JPanel date = new JPanel();
// JPanel content = new JPanel();
// JPanel trackListPanel = new JPanel();
// JLabel dateLabel = new JLabel();
JLabel contentLabel = 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 viewWiki = new JButton("View Wiki");
// JButton musicBrainz = new JButton("Open MusicBrainz");
// JButton rym = new JButton("Open RYM");
public WikiView(Wiki wiki, String name) {
super(name);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new GridLayout(1,1));
setSize(400, 500);
// setResizable(false);
// buttons.setLayout(new FlowLayout());
// buttons2.setLayout(new FlowLayout());
//// System.out.println(album.getName());
//// if (album.getTrackList() != null)
//// buttons2.setLayout(new GridLayout(album.getTrackList().size(), 1));
//
// buttons.add(open);
// buttons.add(viewArtist);
// if (album.getWiki() != null)
// buttons2.add(viewWiki);
// if (album.getMbid() != null)
// buttons2.add(musicBrainz);
// buttons2.add(rym);
//
// NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
//
// dateLabel.setText(wiki.getDate());
contentLabel.setText("<html>" + wiki.getContent() + "<br><br>" + wiki.getDate() + "</html>");
// 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();
// }
// });
// viewWiki.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent arg0) {
// album.getWiki().view();
// }
// });
// musicBrainz.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent arg0) {
// Network.openURL(album.getMusicBrainzURL());
// ;
// }
// });
// rym.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent arg0) {
// Network.openURL(album.getRymURL());
// ;
// }
// });
// if (album.getTrackList() != null) {
// int counter;
// ArrayList<Track> trackList = album.getTrackList();
// for (counter = 0; counter < trackList.size(); counter++) {
// Track track = trackList.get(counter);
// JLabel name = new JLabel(track.getName());
//
// int playCountString = track.getUserPlayCount();
//
// JLabel userPlays;
// if (playCountString == 0)
// userPlays = new JLabel("0");
// else
// userPlays = new JLabel(Integer.toString(track.getUserPlayCount()));
//
// JLabel plays = new JLabel(numberFormat.format(track.getPlayCount()));
// JButton openExternal = new JButton("Open Online");
// openExternal.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent arg0) {
// Network.openURL(track.getUrl());
// }
// });
// JButton openInternal = new JButton("Open " + track.getClass().getSimpleName());
// openInternal.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent arg0) {
// track.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);
//
// trackListPanel.add(panel);
// }
// }
// add(name);
// add(artist);
// add(listeners);
// add(playCount);
// add(userPlayCount);
//
// add(trackListPanel);
// JPanel datePanel = new JPanel();
// datePanel.setLayout(new FlowLayout());
// datePanel.add(dateLabel);
// add(datePanel);
add(contentLabel);
// pack();
}
}

View File

@ -5,6 +5,7 @@ import java.util.ArrayList;
import org.w3c.dom.Document;
import sarsoo.fmframework.net.Network;
import sarsoo.fmframework.net.TestCall;
import sarsoo.fmframework.parser.Parser;
public class Artist extends FMObj{
@ -24,6 +25,7 @@ public class Artist extends FMObj{
public static Artist getArtist(String name, String username) {
String url = Network.getArtistInfoUrl(name, username);
TestCall.test(url);
Document response = Network.getResponse(url);
Artist artist = Parser.parseArtist(response);
return artist;

View File

@ -1,7 +1,44 @@
package sarsoo.fmframework.music;
import sarsoo.fmframework.gui.WikiView;
public class Wiki {
protected String date;
protected String summary;
protected String content;
private String date;
private String summary;
private String content;
public Wiki(String date, String summary, String content) {
this.setDate(date);
this.setSummary(summary);
this.setContent(content);
}
public void view(String name) {
WikiView view = new WikiView(this, name);
view.setVisible(true);
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}

View File

@ -29,13 +29,8 @@ public class Parser {
if (doc.getDocumentElement().getAttribute("status").equals("ok")) {
String name = doc.getElementsByTagName("name").item(0).getTextContent();
String artist = doc.getElementsByTagName("artist").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 mbid = null;
NodeList mbidNodeList = doc.getElementsByTagName("mbid");
if (mbidNodeList.item(0) != null) {
mbid = doc.getElementsByTagName("mbid").item(0).getTextContent();
@ -46,110 +41,47 @@ public class Parser {
int playCount = Integer.parseInt(doc.getElementsByTagName("playcount").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 for "
// + name);
// }
NodeList userPlayCountNodeList = doc.getElementsByTagName("userplaycount");
if (userPlayCountNodeList.item(0) != null) {
userPlayCount = Integer.parseInt(userPlayCountNodeList.item(0).getTextContent());
}
Artist artistObj = Artist.getArtist(artist, Reference.getUserName());
Wiki wiki = new Wiki();
Wiki wiki = null;
NodeList wikiNodeList = doc.getElementsByTagName("wiki");
if (wikiNodeList.item(0) != null) {
String date = wikiNodeList.item(0).getFirstChild().getTextContent();
Node summaryNode = wikiNodeList.item(0).getFirstChild().getNextSibling();
while (!(summaryNode instanceof Element) && summaryNode != null) {
summaryNode = summaryNode.getNextSibling();
}
String summary = summaryNode.getTextContent();
// System.out.println(summary);
Node contentNode = summaryNode.getNextSibling();
while (!(contentNode instanceof Element) && contentNode != null) {
contentNode = contentNode.getNextSibling();
}
String content = contentNode.getTextContent();
// System.out.println(content);
wiki = new Wiki(date, summary, content);
}
Album album = new Album(name, url, mbid, artistObj, listeners, playCount, userPlayCount, wiki);
// NodeList trackListNode = doc.getElementsByTagName("tracks");
//
// NodeList child1 = doc.getElementsByTagName("tracks");
// Node sibling = trackListNode.item(0).getFirstChild();
// int counter;
// // for(counter = 0; counter < 2; counter++) {
// while (sibling != trackListNode.item(0).getLastChild()) {
// while (!(sibling instanceof Element) && sibling != null) {
// sibling = sibling.getNextSibling();
// }
// // NamedNodeMap attributes = sibling.getAttributes();
//
// // int trackNumber =
// // Integer.parseInt(attributes.getNamedItem("rank").getNodeValue());
// NodeList trackNodeList = sibling.getChildNodes();
// String trackName = trackNodeList.item(0).getTextContent();
// String artistName = trackNodeList.item(8).getFirstChild().getTextContent();
//
// Track track = Track.getTrack(trackName, artistName, Reference.getUserName());
// track.setAlbum(album);
// System.out.println(track.getName());
//
// album.addTrack(track);
//
// sibling = sibling.getNextSibling();
//
// }
// while (!(sibling instanceof Element) && sibling != null) {
// sibling = sibling.getNextSibling();
// }
// NodeList list = sibling.getChildNodes();
// list.item(0);
// System.out.println(list.item(0));
// System.out.println(trackListNode.item(0).getTextContent());
// 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();
// System.out.println(published);
// System.out.println(summary);
// System.out.println(content);
return album;
// } catch (NullPointerException e) {
// System.err.println("Could Not Parse Album");
// return null;
// }
}
return null;
}
public static Artist parseArtist(Document doc) {
// try {
if (doc.getDocumentElement().getAttribute("status").equals("ok")) {
String name = doc.getElementsByTagName("name").item(0).getTextContent();
String mbid = null;
// try {
// mbid = doc.getElementsByTagName("mbid").item(0).getTextContent();
// } catch (NullPointerException e) {
// System.err.println("Null Mbid for " + name);
// }
NodeList mbidNodeList = doc.getElementsByTagName("mbid");
if (mbidNodeList.item(0) != null) {
@ -157,32 +89,69 @@ public class Parser {
}
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 = 0;
// try {
// userPlayCount =
// Integer.parseInt(doc.getElementsByTagName("userplaycount").item(0).getTextContent());
// } catch (Exception e) {
// System.err.println("Couldn't parse userPlayCount, possibly unscrobbled for "
// + name);
// }
// System.out.println(listeners);
NodeList userPlayCountNodeList = doc.getElementsByTagName("userplaycount");
if (userPlayCountNodeList.item(0) != null) {
userPlayCount = Integer.parseInt(userPlayCountNodeList.item(0).getTextContent());
}
Wiki wiki = null;
NodeList wikiNodeList = doc.getElementsByTagName("bio");
if (wikiNodeList.getLength() != 0) {
Node wikiNode = wikiNodeList.item(0);
NodeList wikiContents = wikiNode.getChildNodes();
String date = null;
String summary = null;
String content = null;
int counter;
Node node;
for (counter = 0; counter < wikiContents.getLength(); counter++) {
node = wikiContents.item(counter);
System.out.println(node.getNodeName() + node.getTextContent());
if (node.getNodeName().equals("published")) {
date = node.getTextContent();
}
if (node.getNodeName().equals("summary")) {
summary = node.getTextContent();
}
if (node.getNodeName().equals("content")) {
content = node.getTextContent();
}
}
if(date != null && content != null)
wiki = new Wiki(date, summary, content);
// if (wikiNodeList.item(0) != null) {
// String date = wikiNodeList.item(0).getFirstChild().getTextContent();
//
// Node summaryNode = wikiNodeList.item(0).getFirstChild().getNextSibling();
// while (!(summaryNode instanceof Element) && summaryNode != null) {
// summaryNode = summaryNode.getNextSibling();
// }
// String summary = summaryNode.getTextContent();
// // System.out.println(summary);
//
// Node contentNode = summaryNode.getNextSibling();
// while (!(contentNode instanceof Element) && contentNode != null) {
// contentNode = contentNode.getNextSibling();
// }
// String content = contentNode.getTextContent();
// // System.out.println(content);
//
// wiki = new Wiki(date, summary, content);
// }
}
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;
// }
}
return null;
}
public static Track parseTrack(Document doc) {
@ -202,47 +171,49 @@ public class Parser {
return null;
}
String mbid = null;
// try {
// mbid = doc.getElementsByTagName("mbid").item(0).getTextContent();
// } catch (NullPointerException e) {
// System.err.println("Null Mbid for " + name + " - " + artistName);
// }
NodeList mbidNodeList = doc.getElementsByTagName("mbid");
if (mbidNodeList.item(0) != null) {
mbid = doc.getElementsByTagName("mbid").item(0).getTextContent();
}
String url = null;
// try {
url = doc.getElementsByTagName("url").item(0).getTextContent();
// } catch (NullPointerException e) {
// System.err.println("Could Not Parse Track Url");
// return null;
// }
int listeners = Integer.parseInt(doc.getElementsByTagName("listeners").item(0).getTextContent());
int playCount = Integer.parseInt(doc.getElementsByTagName("playcount").item(0).getTextContent());
int userPlayCount = 0;
// try {
// userPlayCount =
// Integer.parseInt(doc.getElementsByTagName("userplaycount").item(0).getTextContent());
// } catch (Exception e) {
// System.out.println();
// System.err.println("Couldn't parse userPlayCount, possibly unscrobbled for "
// + name);
// }
NodeList userPlayCountNodeList = doc.getElementsByTagName("userplaycount");
if (userPlayCountNodeList.item(0) != null) {
userPlayCount = Integer.parseInt(userPlayCountNodeList.item(0).getTextContent());
}
// String albumName =
// doc.getElementsByTagName("album").item(0).getTextContent();
// System.out.println(albumName);
Artist artistObj = Artist.getArtist(artistName, Reference.getUserName());
// System.out.println(userPlayCount);
Wiki wiki = null;
NodeList wikiNodeList = doc.getElementsByTagName("wiki");
if (wikiNodeList.item(0) != null) {
String date = wikiNodeList.item(0).getFirstChild().getTextContent();
Node summaryNode = wikiNodeList.item(0).getFirstChild().getNextSibling();
while (!(summaryNode instanceof Element) && summaryNode != null) {
summaryNode = summaryNode.getNextSibling();
}
String summary = summaryNode.getTextContent();
// System.out.println(summary);
Node contentNode = summaryNode.getNextSibling();
while (!(contentNode instanceof Element) && contentNode != null) {
contentNode = contentNode.getNextSibling();
}
String content = contentNode.getTextContent();
// System.out.println(content);
wiki = new Wiki(date, summary, content);
}
Track track = new Track(name, url, mbid, artistObj, listeners, playCount, userPlayCount, null);
return track;
@ -253,27 +224,20 @@ public class Parser {
}
public static Track parseLastTrack(Document doc) {
// try {
// System.out.println(doc.getDocumentElement().getAttribute("status"));
if (doc.getDocumentElement().getAttribute("status").equals("ok")) {
String name = doc.getElementsByTagName("name").item(0).getTextContent();
// System.out.println(name);
String artistName = doc.getElementsByTagName("artist").item(0).getTextContent();
String albumName = doc.getElementsByTagName("album").item(0).getTextContent();
System.out.println(albumName);
// System.out.println(albumName);
Track track = Track.getTrack(name, artistName, Reference.getUserName());
Album album = Album.getAlbum(albumName, artistName, Reference.getUserName());
// if (album.getName() != null)
track.setAlbum(album);
return track;
// } catch (NullPointerException e) {
// System.err.println("Could Not Parse Last Track");
// return null;
// }
}
return null;

View File

@ -8,6 +8,7 @@ import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.music.Track;
import sarsoo.fmframework.net.Network;
import sarsoo.fmframework.net.TestCall;
import sarsoo.fmframework.parser.Parser;
public class GetObject {
@ -33,12 +34,18 @@ public class GetObject {
public static Track getLastTrack() {
String url = Network.getLastTrackUrl(Reference.getUserName());
// TestCall.test(url);
Document doc = Network.getResponse(url);
// System.out.println(doc.getDocumentElement().getAttribute("status"));
if (doc != null) {
// System.out.println(doc.getDocumentElement().getAttribute("status"));
Parser.stripSpace(doc.getDocumentElement());
Track track = Parser.parseLastTrack(doc);
// return null;
return track;
}
return null;
}