parsing working, beginning model work

This commit is contained in:
aj 2018-03-24 16:58:21 -07:00
parent b6037c9846
commit 2834448c59
6 changed files with 64 additions and 36 deletions

View File

@ -3,20 +3,26 @@ package sarsoo.fmframework.music;
import java.util.ArrayList; import java.util.ArrayList;
public class Album extends FMObj{ public class Album extends FMObj{
protected int id;
protected String date;
protected Artist artist; protected Artist artist;
protected int listeners; protected int listeners;
protected int playCount; protected int playCount;
protected int userPlayCount;
protected ArrayList<Tag> tagList; protected ArrayList<Tag> tagList;
protected ArrayList<Track> trackList; protected ArrayList<Track> trackList;
public Album(String name, String url, String mbid, int id, String artist) { public Album(String name, String url, String mbid, Artist artist, int listeners, int playCount, int userPlayCount) {
this.name = name; this.name = name;
this.url = url; this.url = url;
this.mbid = mbid; this.mbid = mbid;
this.id = id; this.artist = artist;
this.artist = new Artist(artist); this.listeners = listeners;
this.playCount = playCount;
this.userPlayCount = userPlayCount;
}
public String toString() {
return name + " - " + artist.getName();
} }
} }

View File

@ -13,10 +13,13 @@ import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
public class Network { public class Network {
public static void apiAlbumInfoCall(String artist, String album, String username) { public static Document apiAlbumInfoCall(String artist, String album, String username) {
try { try {
String urlString = String.format("http://ws.audioscrobbler.com/2.0/?method=album.getInfo&artist=%s&album=%s&autocorrect=1&username=%s&api_key=%s", String urlString = String.format("http://ws.audioscrobbler.com/2.0/?method=album.getInfo&artist=%s&album=%s&autocorrect=1&username=%s&api_key=%s",
@ -41,8 +44,7 @@ public class Network {
conn.disconnect(); conn.disconnect();
doc.getDocumentElement().normalize(); doc.getDocumentElement().normalize();
System.out.println("Root Element: " + doc.getDocumentElement().getNodeName()); return doc;
} catch (SAXException e) { } catch (SAXException e) {
e.printStackTrace(); e.printStackTrace();
@ -62,5 +64,6 @@ public class Network {
e.printStackTrace(); e.printStackTrace();
} }
return null;
} }
} }

View File

@ -9,9 +9,11 @@ import java.net.URL;
public class TestCall { public class TestCall {
public static void test() { public static void test(String artist, String album, String username) {
try { try {
URL url = new URL("http://ws.audioscrobbler.com/2.0/?method=artist.getInfo&artist=Mastodon&format=json&api_key=" + Key.getKey()); String urlString = String.format("http://ws.audioscrobbler.com/2.0/?method=album.getInfo&artist=%s&album=%s&autocorrect=1&username=%s&api_key=%s",
artist, album, username, Key.getKey());
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/json"); conn.setRequestProperty("Accept", "application/json");
@ -23,7 +25,6 @@ public class TestCall {
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String output; String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) { while ((output = br.readLine()) != null) {
System.out.println(output); System.out.println(output);
} }

View File

@ -6,6 +6,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import sarsoo.fmframework.music.Album; import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Artist;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap; import org.w3c.dom.NamedNodeMap;
@ -16,29 +17,20 @@ import org.xml.sax.SAXException;
public class AlbumParser { public class AlbumParser {
public static Album parseAlbum(InputStream input){ public static Album parseAlbum(Document doc){
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
try {
dBuilder = dbFactory.newDocumentBuilder();
try {
Document doc = dBuilder.parse(input);
doc.getDocumentElement().normalize();
System.out.println("Root Element: " + doc.getDocumentElement().getNodeName()); String name = doc.getElementsByTagName("name").item(0).getTextContent();
} catch (SAXException e) { String artist = doc.getElementsByTagName("artist").item(0).getTextContent();
// TODO Auto-generated catch block String mbid = doc.getElementsByTagName("mbid").item(0).getTextContent();
e.printStackTrace(); String url = doc.getElementsByTagName("url").item(0).getTextContent();
} catch (IOException e) { int listeners = Integer.parseInt(doc.getElementsByTagName("listeners").item(0).getTextContent());
// TODO Auto-generated catch block int playCount = Integer.parseInt(doc.getElementsByTagName("playcount").item(0).getTextContent());
e.printStackTrace(); int userPlayCount = Integer.parseInt(doc.getElementsByTagName("userplaycount").item(0).getTextContent());
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null; Artist artistObj = new Artist(artist);
Album album = new Album(name, url, mbid, artistObj, listeners, playCount, userPlayCount);
return album;
} }

View File

@ -0,0 +1,20 @@
package sarsoo.fmframework.test;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.net.Network;
import sarsoo.fmframework.parser.AlbumParser;
class AlbumParserTest {
@Test
void test() {
Document doc = Network.apiAlbumInfoCall("Pink Floyd", "The Wall", "sarsoo");
Album album = AlbumParser.parseAlbum(doc);
}
}

View File

@ -5,17 +5,23 @@ import static org.junit.jupiter.api.Assertions.*;
import java.io.InputStream; import java.io.InputStream;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
import sarsoo.fmframework.net.Network; import sarsoo.fmframework.net.Network;
import sarsoo.fmframework.net.TestCall;
import sarsoo.fmframework.parser.AlbumParser; import sarsoo.fmframework.parser.AlbumParser;
class NetworkTest { class NetworkTest {
@Test
void testCall() {
TestCall.test("Pink Floyd", "The Wall", "sarsoo");
}
@Test @Test
void test() { void test() {
Network.apiAlbumInfoCall("Mastodon", "Leviathan", "sarsoo"); //Document doc = Network.apiAlbumInfoCall("Mastodon", "Leviathan", "sarsoo");
//System.out.println(xml); //AlbumParser.parseAlbum(doc);
//AlbumParser.parseAlbum(xml);
} }
} }