parsing more resilient, better track handling on null album

This commit is contained in:
aj 2018-03-29 02:12:36 -07:00
parent e289ec60f5
commit d29c1bf8da
6 changed files with 178 additions and 125 deletions

View File

@ -1,26 +1,17 @@
package sarsoo.fmframework.gui; package sarsoo.fmframework.gui;
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.text.NumberFormat;
import java.util.ArrayList;
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.JOptionPane; import javax.swing.JOptionPane;
import javax.swing.JPanel;
import sarsoo.fmframework.music.Album; import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Artist; import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.music.Track; import sarsoo.fmframework.music.Track;
import sarsoo.fmframework.net.Network;
import sarsoo.fmframework.util.FMObjList;
import sarsoo.fmframework.util.GetObject; import sarsoo.fmframework.util.GetObject;
import sarsoo.fmframework.util.Reference;
public class MainMenu extends JFrame { public class MainMenu extends JFrame {

View File

@ -54,6 +54,7 @@ public class TrackView extends JFrame {
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US); NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
name.setText(track.getName()); name.setText(track.getName());
if(track.getAlbum() != null)
album.setText(track.getAlbum().getName()); album.setText(track.getAlbum().getName());
artist.setText(track.getArtist().getName()); artist.setText(track.getArtist().getName());
listeners.setText(numberFormat.format(track.getListeners()) + " Listeners"); listeners.setText(numberFormat.format(track.getListeners()) + " Listeners");
@ -87,6 +88,7 @@ public class TrackView extends JFrame {
}); });
add(name); add(name);
if(track.getAlbum() != null)
add(album); add(album);
add(artist); add(artist);
add(listeners); add(listeners);

View File

@ -1,5 +0,0 @@
package sarsoo.fmframework.music;
public class Bio {
public String date;
}

View File

@ -24,7 +24,8 @@ public class Network {
URL url = new URL(urlString); URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET"); conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/xml"); conn.setRequestProperty("Accept", "text/xml");
conn.setRequestProperty("User-Agent", "fmframework/1.0");
if (conn.getResponseCode() != 200) { if (conn.getResponseCode() != 200) {
throw new ApiCallException(conn.getResponseCode()); throw new ApiCallException(conn.getResponseCode());
@ -94,7 +95,7 @@ public class Network {
public static String getLastTrackUrl(String username) { public static String getLastTrackUrl(String username) {
String urlString = String.format( String urlString = String.format(
"http://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&limit=1&user=%s&api_key=%s", username, "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=%s&limit=1&api_key=%s", username,
Key.getKey()); Key.getKey());
return urlString; return urlString;
} }

View File

@ -14,6 +14,7 @@ import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap; import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node; 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;
@ -24,24 +25,37 @@ import sarsoo.fmframework.util.Reference;
public class Parser { public class Parser {
public static Album parseAlbum(Document doc) { public static Album parseAlbum(Document doc) {
try { // try {
String name = doc.getElementsByTagName("name").item(0).getTextContent(); String name = doc.getElementsByTagName("name").item(0).getTextContent();
String artist = doc.getElementsByTagName("artist").item(0).getTextContent(); String artist = doc.getElementsByTagName("artist").item(0).getTextContent();
String mbid = null; String mbid = null;
try { // 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) {
mbid = doc.getElementsByTagName("mbid").item(0).getTextContent(); 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 url = doc.getElementsByTagName("url").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 = 0; int userPlayCount = 0;
try { // try {
userPlayCount = Integer.parseInt(doc.getElementsByTagName("userplaycount").item(0).getTextContent()); // userPlayCount =
} catch (Exception e) { // Integer.parseInt(doc.getElementsByTagName("userplaycount").item(0).getTextContent());
System.err.println("Couldn't parse userPlayCount, possibly unscrobbled for " + name); // } 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());
} }
// Node trackListNode = doc.getElementsByTagName("tracks").item(0); // Node trackListNode = doc.getElementsByTagName("tracks").item(0);
@ -81,22 +95,28 @@ public class Parser {
Album album = new Album(name, url, mbid, artistObj, listeners, playCount, userPlayCount, wiki); Album album = new Album(name, url, mbid, artistObj, listeners, playCount, userPlayCount, wiki);
return album; return album;
} catch (NullPointerException e) { // } catch (NullPointerException e) {
System.err.println("Could Not Parse Album"); // System.err.println("Could Not Parse Album");
return null; // return null;
} // }
} }
public static Artist parseArtist(Document doc) { public static Artist parseArtist(Document doc) {
try { // try {
String name = doc.getElementsByTagName("name").item(0).getTextContent(); String name = doc.getElementsByTagName("name").item(0).getTextContent();
String mbid = null; String mbid = null;
try { // 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) {
mbid = doc.getElementsByTagName("mbid").item(0).getTextContent(); 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 url = doc.getElementsByTagName("url").item(0).getTextContent();
// String streamable = // String streamable =
// doc.getElementsByTagName("streamable").item(0).getTextContent(); // doc.getElementsByTagName("streamable").item(0).getTextContent();
@ -104,41 +124,78 @@ public class Parser {
int playCount = Integer.parseInt(doc.getElementsByTagName("playcount").item(0).getTextContent()); int playCount = Integer.parseInt(doc.getElementsByTagName("playcount").item(0).getTextContent());
int userPlayCount = 0; int userPlayCount = 0;
try { // try {
userPlayCount = Integer.parseInt(doc.getElementsByTagName("userplaycount").item(0).getTextContent()); // userPlayCount =
} catch (Exception e) { // Integer.parseInt(doc.getElementsByTagName("userplaycount").item(0).getTextContent());
System.err.println("Couldn't parse userPlayCount, possibly unscrobbled for " + name); // } catch (Exception e) {
} // System.err.println("Couldn't parse userPlayCount, possibly unscrobbled for "
// + name);
// }
// System.out.println(listeners); // System.out.println(listeners);
NodeList userPlayCountNodeList = doc.getElementsByTagName("userplaycount");
if (userPlayCountNodeList.item(0) != null) {
userPlayCount = Integer.parseInt(userPlayCountNodeList.item(0).getTextContent());
}
Artist artist = new Artist(name, url, mbid, listeners, playCount, userPlayCount, false, false, null); Artist artist = new Artist(name, url, mbid, listeners, playCount, userPlayCount, false, false, null);
return artist; return artist;
} catch (NullPointerException e) { // } catch (NullPointerException e) {
System.err.println("Could Not Parse Artist"); // System.err.println("Could Not Parse Artist");
return null; // return null;
} // }
} }
public static Track parseTrack(Document doc) { public static Track parseTrack(Document doc) {
if (doc.getDocumentElement().getAttribute("status").equals("ok")) {
String name = " ";
try { try {
String name = doc.getElementsByTagName("name").item(0).getTextContent(); name = doc.getElementsByTagName("name").item(0).getTextContent();
String artistName = doc.getElementsByTagName("artist").item(0).getFirstChild().getTextContent();
String mbid = null;
try {
mbid = doc.getElementsByTagName("mbid").item(0).getTextContent();
} catch (NullPointerException e) { } catch (NullPointerException e) {
System.err.println("Null Mbid for " + name + " - " + artistName); System.err.println("Could Not Parse Track Name");
return null;
} }
String url = doc.getElementsByTagName("url").item(0).getTextContent(); String artistName = " ";
try {
artistName = doc.getElementsByTagName("artist").item(0).getFirstChild().getTextContent();
} catch (NullPointerException e) {
System.err.println("Could Not Parse Artist Name for " + name);
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 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 = 0; int userPlayCount = 0;
try { // try {
userPlayCount = Integer.parseInt(doc.getElementsByTagName("userplaycount").item(0).getTextContent()); // userPlayCount =
} catch (Exception e) { // Integer.parseInt(doc.getElementsByTagName("userplaycount").item(0).getTextContent());
System.err.println("Couldn't parse userPlayCount, possibly unscrobbled for " + name); // } 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 = // String albumName =
@ -150,32 +207,38 @@ public class Parser {
Track track = new Track(name, url, mbid, artistObj, listeners, playCount, userPlayCount, null); Track track = new Track(name, url, mbid, artistObj, listeners, playCount, userPlayCount, null);
return track; return track;
} catch (NullPointerException e) { } else {
System.err.println("Could Not Parse Track");
return null; return null;
} }
} }
public static Track parseLastTrack(Document doc) { public static Track parseLastTrack(Document doc) {
try { // try {
// System.out.println(doc.getDocumentElement().getAttribute("status"));
if (doc.getDocumentElement().getAttribute("status").equals("ok")) {
String name = doc.getElementsByTagName("name").item(0).getTextContent(); String name = doc.getElementsByTagName("name").item(0).getTextContent();
// System.out.println(name);
String artistName = doc.getElementsByTagName("artist").item(0).getTextContent(); String artistName = doc.getElementsByTagName("artist").item(0).getTextContent();
String albumName = doc.getElementsByTagName("album").item(0).getTextContent(); String albumName = doc.getElementsByTagName("album").item(0).getTextContent();
Track track = Track.getTrack(name, artistName, Reference.getUserName()); Track track = Track.getTrack(name, artistName, Reference.getUserName());
Album album = Album.getAlbum(albumName, artistName, Reference.getUserName()); // Album album = Album.getAlbum(albumName, artistName, Reference.getUserName());
track.setAlbum(album); // if (album.getName() != null)
// track.setAlbum(album);
return track; return track;
} catch (NullPointerException e) { // } catch (NullPointerException e) {
System.err.println("Could Not Parse Track"); // System.err.println("Could Not Parse Last Track");
return null; // return null;
// }
} }
return null;
} }
public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException { public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {

View File

@ -34,9 +34,10 @@ public class GetObject {
String url = Network.getLastTrackUrl(Reference.getUserName()); String url = Network.getLastTrackUrl(Reference.getUserName());
Document doc = Network.getResponse(url); Document doc = Network.getResponse(url);
// System.out.println(doc.getDocumentElement().getAttribute("status"));
Parser.stripSpace(doc.getDocumentElement()); Parser.stripSpace(doc.getDocumentElement());
Track track = Parser.parseLastTrack(doc); Track track = Parser.parseLastTrack(doc);
// return null;
return track; return track;
} }