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;
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.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.FMObjList;
import sarsoo.fmframework.util.GetObject;
import sarsoo.fmframework.util.Reference;
public class MainMenu extends JFrame {

View File

@ -54,6 +54,7 @@ public class TrackView extends JFrame {
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
name.setText(track.getName());
if(track.getAlbum() != null)
album.setText(track.getAlbum().getName());
artist.setText(track.getArtist().getName());
listeners.setText(numberFormat.format(track.getListeners()) + " Listeners");
@ -87,6 +88,7 @@ public class TrackView extends JFrame {
});
add(name);
if(track.getAlbum() != null)
add(album);
add(artist);
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);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/xml");
conn.setRequestProperty("Accept", "text/xml");
conn.setRequestProperty("User-Agent", "fmframework/1.0");
if (conn.getResponseCode() != 200) {
throw new ApiCallException(conn.getResponseCode());
@ -94,7 +95,7 @@ public class Network {
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,
"http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=%s&limit=1&api_key=%s", username,
Key.getKey());
return urlString;
}

View File

@ -14,6 +14,7 @@ import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Artist;
@ -24,24 +25,37 @@ import sarsoo.fmframework.util.Reference;
public class Parser {
public static Album parseAlbum(Document doc) {
try {
// try {
String name = doc.getElementsByTagName("name").item(0).getTextContent();
String artist = doc.getElementsByTagName("artist").item(0).getTextContent();
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();
} 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 = 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);
// 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());
}
// 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);
return album;
} catch (NullPointerException e) {
System.err.println("Could Not Parse Album");
return null;
}
// } catch (NullPointerException e) {
// System.err.println("Could Not Parse Album");
// return null;
// }
}
public static Artist parseArtist(Document doc) {
try {
// try {
String name = doc.getElementsByTagName("name").item(0).getTextContent();
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();
} 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();
@ -104,41 +124,78 @@ 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);
}
// 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());
}
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;
}
// } catch (NullPointerException e) {
// System.err.println("Could Not Parse Artist");
// return null;
// }
}
public static Track parseTrack(Document doc) {
if (doc.getDocumentElement().getAttribute("status").equals("ok")) {
String name = " ";
try {
String 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();
name = doc.getElementsByTagName("name").item(0).getTextContent();
} 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 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);
// 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 =
@ -150,32 +207,38 @@ public class Parser {
Track track = new Track(name, url, mbid, artistObj, listeners, playCount, userPlayCount, null);
return track;
} catch (NullPointerException e) {
System.err.println("Could Not Parse Track");
} else {
return null;
}
}
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();
// System.out.println(name);
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());
// Album album = Album.getAlbum(albumName, artistName, Reference.getUserName());
track.setAlbum(album);
// if (album.getName() != null)
// track.setAlbum(album);
return track;
} catch (NullPointerException e) {
System.err.println("Could Not Parse Track");
return null;
// } catch (NullPointerException e) {
// System.err.println("Could Not Parse Last Track");
// return null;
// }
}
return null;
}
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());
Document doc = Network.getResponse(url);
// System.out.println(doc.getDocumentElement().getAttribute("status"));
Parser.stripSpace(doc.getDocumentElement());
Track track = Parser.parseLastTrack(doc);
// return null;
return track;
}