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;
public class Album extends FMObj{
protected int id;
protected String date;
protected Artist artist;
protected int listeners;
protected int playCount;
protected int userPlayCount;
protected ArrayList<Tag> tagList;
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.url = url;
this.mbid = mbid;
this.id = id;
this.artist = new Artist(artist);
this.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 org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Network {
public static void apiAlbumInfoCall(String artist, String album, String username) {
public static Document apiAlbumInfoCall(String artist, String album, String username) {
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",
@ -41,8 +44,7 @@ public class Network {
conn.disconnect();
doc.getDocumentElement().normalize();
System.out.println("Root Element: " + doc.getDocumentElement().getNodeName());
return doc;
} catch (SAXException e) {
e.printStackTrace();
@ -62,5 +64,6 @@ public class Network {
e.printStackTrace();
}
return null;
}
}

View File

@ -9,9 +9,11 @@ import java.net.URL;
public class TestCall {
public static void test() {
public static void test(String artist, String album, String username) {
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();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
@ -23,7 +25,6 @@ public class TestCall {
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}

View File

@ -6,6 +6,7 @@ import java.io.IOException;
import java.io.InputStream;
import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Artist;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
@ -16,29 +17,20 @@ import org.xml.sax.SAXException;
public class AlbumParser {
public static Album parseAlbum(InputStream input){
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
try {
dBuilder = dbFactory.newDocumentBuilder();
try {
Document doc = dBuilder.parse(input);
doc.getDocumentElement().normalize();
public static Album parseAlbum(Document doc){
System.out.println("Root Element: " + doc.getDocumentElement().getNodeName());
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String name = doc.getElementsByTagName("name").item(0).getTextContent();
String artist = doc.getElementsByTagName("artist").item(0).getTextContent();
String mbid = doc.getElementsByTagName("mbid").item(0).getTextContent();
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 = Integer.parseInt(doc.getElementsByTagName("userplaycount").item(0).getTextContent());
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 org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
import sarsoo.fmframework.net.Network;
import sarsoo.fmframework.net.TestCall;
import sarsoo.fmframework.parser.AlbumParser;
class NetworkTest {
@Test
void testCall() {
TestCall.test("Pink Floyd", "The Wall", "sarsoo");
}
@Test
void test() {
Network.apiAlbumInfoCall("Mastodon", "Leviathan", "sarsoo");
//System.out.println(xml);
//AlbumParser.parseAlbum(xml);
//Document doc = Network.apiAlbumInfoCall("Mastodon", "Leviathan", "sarsoo");
//AlbumParser.parseAlbum(doc);
}
}