diff --git a/fmframework/src/sarsoo/fmframework/music/Album.java b/fmframework/src/sarsoo/fmframework/music/Album.java index 6c4a0ce..255410a 100644 --- a/fmframework/src/sarsoo/fmframework/music/Album.java +++ b/fmframework/src/sarsoo/fmframework/music/Album.java @@ -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 tagList; protected ArrayList 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(); + } } diff --git a/fmframework/src/sarsoo/fmframework/net/Network.java b/fmframework/src/sarsoo/fmframework/net/Network.java index 96516ea..0eb5f77 100644 --- a/fmframework/src/sarsoo/fmframework/net/Network.java +++ b/fmframework/src/sarsoo/fmframework/net/Network.java @@ -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; } } diff --git a/fmframework/src/sarsoo/fmframework/net/TestCall.java b/fmframework/src/sarsoo/fmframework/net/TestCall.java index 3ef3ed2..1fe0d4f 100644 --- a/fmframework/src/sarsoo/fmframework/net/TestCall.java +++ b/fmframework/src/sarsoo/fmframework/net/TestCall.java @@ -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); } diff --git a/fmframework/src/sarsoo/fmframework/parser/AlbumParser.java b/fmframework/src/sarsoo/fmframework/parser/AlbumParser.java index 0167305..9ca8b41 100644 --- a/fmframework/src/sarsoo/fmframework/parser/AlbumParser.java +++ b/fmframework/src/sarsoo/fmframework/parser/AlbumParser.java @@ -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(); - - 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(); - } + public static Album parseAlbum(Document doc){ - return null; + 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()); + + Artist artistObj = new Artist(artist); + + Album album = new Album(name, url, mbid, artistObj, listeners, playCount, userPlayCount); + return album; } diff --git a/fmframework/src/sarsoo/fmframework/test/AlbumParserTest.java b/fmframework/src/sarsoo/fmframework/test/AlbumParserTest.java new file mode 100644 index 0000000..a231f12 --- /dev/null +++ b/fmframework/src/sarsoo/fmframework/test/AlbumParserTest.java @@ -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); + } + +} diff --git a/fmframework/src/sarsoo/fmframework/test/NetworkTest.java b/fmframework/src/sarsoo/fmframework/test/NetworkTest.java index a8945dc..6373cc0 100644 --- a/fmframework/src/sarsoo/fmframework/test/NetworkTest.java +++ b/fmframework/src/sarsoo/fmframework/test/NetworkTest.java @@ -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); } }