diff --git a/fmframework/src/sarsoo/fmframework/music/Album.java b/fmframework/src/sarsoo/fmframework/music/Album.java index 255410a..a5741cc 100644 --- a/fmframework/src/sarsoo/fmframework/music/Album.java +++ b/fmframework/src/sarsoo/fmframework/music/Album.java @@ -4,20 +4,17 @@ import java.util.ArrayList; public class Album extends FMObj{ 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, Artist artist, int listeners, int playCount, int userPlayCount) { + public Album(String name) { + super(name, null, null, 0, 0, 0, null); this.name = name; - this.url = url; - this.mbid = mbid; + } + + public Album(String name, String url, String mbid, Artist artist, int listeners, int playCount, int userPlayCount, Wiki wiki) { + super(name, url, mbid, listeners, playCount, userPlayCount, wiki); this.artist = artist; - this.listeners = listeners; - this.playCount = playCount; - this.userPlayCount = userPlayCount; } public String toString() { diff --git a/fmframework/src/sarsoo/fmframework/music/Artist.java b/fmframework/src/sarsoo/fmframework/music/Artist.java index fd98c30..70671ea 100644 --- a/fmframework/src/sarsoo/fmframework/music/Artist.java +++ b/fmframework/src/sarsoo/fmframework/music/Artist.java @@ -3,16 +3,18 @@ package sarsoo.fmframework.music; import java.util.ArrayList; public class Artist extends FMObj{ - protected int listeners; - protected int plays; protected boolean streamable; + protected boolean onTour; protected ArrayList albums; protected ArrayList tracks; protected ArrayList similarArtists; protected ArrayList tagList; - protected Wiki wiki; public Artist(String name) { - this.name = name; + super(name, null, null, 0, 0, 0, null); + } + + public String toString() { + return name; } } diff --git a/fmframework/src/sarsoo/fmframework/music/FMObj.java b/fmframework/src/sarsoo/fmframework/music/FMObj.java index e1521e8..bf35091 100644 --- a/fmframework/src/sarsoo/fmframework/music/FMObj.java +++ b/fmframework/src/sarsoo/fmframework/music/FMObj.java @@ -6,6 +6,22 @@ public class FMObj { protected String url; protected String mbid; + protected int listeners; + protected int playCount; + protected int userPlayCount; + + protected Wiki wiki; + + public FMObj(String name, String url, String mbid, int listeners, int playCount, int userPlayCount, Wiki wiki) { + this.name = name; + this.url = url; + this.mbid = mbid; + this.listeners = listeners; + this.playCount = playCount; + this.userPlayCount = userPlayCount; + this.wiki = wiki; + } + public String getName() { return name; } @@ -17,5 +33,21 @@ public class FMObj { public String getMbid() { return mbid; } + + public int getListeners() { + return listeners; + } + + public int playCount() { + return playCount; + } + + public int userPlayCount() { + return userPlayCount; + } + + public Wiki getWiki() { + return wiki; + } } diff --git a/fmframework/src/sarsoo/fmframework/music/Track.java b/fmframework/src/sarsoo/fmframework/music/Track.java index 0997f3f..7138188 100644 --- a/fmframework/src/sarsoo/fmframework/music/Track.java +++ b/fmframework/src/sarsoo/fmframework/music/Track.java @@ -6,12 +6,13 @@ public class Track extends FMObj{ protected Album album; protected Artist artist; protected int trackNumber; - protected int id; protected int duration; - protected int playcount; protected boolean streamable; + protected boolean isLoved; protected ArrayList tagList; - protected Wiki wiki; - + public Track(String name, String artist) { + super(name, null, null, 0, 0, 0, null); + this.artist = new Artist(artist); + } } diff --git a/fmframework/src/sarsoo/fmframework/net/Network.java b/fmframework/src/sarsoo/fmframework/net/Network.java index 0eb5f77..5f23690 100644 --- a/fmframework/src/sarsoo/fmframework/net/Network.java +++ b/fmframework/src/sarsoo/fmframework/net/Network.java @@ -19,11 +19,9 @@ import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class Network { - public static Document apiAlbumInfoCall(String artist, String album, String username) { + + public static Document getResponse(String urlString) { 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", - artist, album, username, Key.getKey()); URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); @@ -32,9 +30,9 @@ public class Network { if (conn.getResponseCode() != 200) { throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode()); } - + InputStream input = conn.getInputStream(); - + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder; try { @@ -43,9 +41,9 @@ public class Network { Document doc = dBuilder.parse(input); conn.disconnect(); doc.getDocumentElement().normalize(); - + return doc; - + } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { @@ -53,9 +51,11 @@ public class Network { } } catch (ParserConfigurationException e) { e.printStackTrace(); - } + } - } catch (MalformedURLException e) { + } catch ( + + MalformedURLException e) { e.printStackTrace(); @@ -65,5 +65,29 @@ public class Network { } return null; + } + + public static String getArtistInfoUrl(String artist, String username) { + String urlString = String.format( + "http://ws.audioscrobbler.com/2.0/?method=artist.getInfo&artist=%s&autocorrect=1&username=%s&api_key=%s", + artist, username, Key.getKey()); + return urlString; + + + } + + public static String getAlbumInfoUrl(String album, String artist, String username) { + String urlString = String.format( + "http://ws.audioscrobbler.com/2.0/?method=album.getInfo&album=%s&artist=%s&autocorrect=1&username=%s&api_key=%s", + album, artist, username, Key.getKey()); + return urlString; + } + + public static String getTrackInfoUrl(String name, String artist, String username) { + String urlString = String.format( + "http://ws.audioscrobbler.com/2.0/?method=track.getInfo&track=%s&artist=%s&autocorrect=1&username=%s&api_key=%s", + name, artist, username, Key.getKey()); + return urlString; +} } diff --git a/fmframework/src/sarsoo/fmframework/net/TestCall.java b/fmframework/src/sarsoo/fmframework/net/TestCall.java index 1fe0d4f..b2d6d8f 100644 --- a/fmframework/src/sarsoo/fmframework/net/TestCall.java +++ b/fmframework/src/sarsoo/fmframework/net/TestCall.java @@ -9,10 +9,8 @@ import java.net.URL; public class TestCall { - public static void test(String artist, String album, String username) { + public static void test(String urlString) { 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", - artist, album, username, Key.getKey()); URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); diff --git a/fmframework/src/sarsoo/fmframework/parser/AlbumParser.java b/fmframework/src/sarsoo/fmframework/parser/AlbumParser.java index 9ca8b41..d6e0f98 100644 --- a/fmframework/src/sarsoo/fmframework/parser/AlbumParser.java +++ b/fmframework/src/sarsoo/fmframework/parser/AlbumParser.java @@ -29,7 +29,7 @@ public class AlbumParser { Artist artistObj = new Artist(artist); - Album album = new Album(name, url, mbid, artistObj, listeners, playCount, userPlayCount); + Album album = new Album(name, url, mbid, artistObj, listeners, playCount, userPlayCount, null); return album; } diff --git a/fmframework/src/sarsoo/fmframework/parser/Parser.java b/fmframework/src/sarsoo/fmframework/parser/Parser.java new file mode 100644 index 0000000..7f352e4 --- /dev/null +++ b/fmframework/src/sarsoo/fmframework/parser/Parser.java @@ -0,0 +1,74 @@ +package sarsoo.fmframework.parser; + +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import sarsoo.fmframework.music.Album; +import sarsoo.fmframework.music.Artist; +import sarsoo.fmframework.music.Wiki; + +public class Parser { + + public static Album parseAlbum(Document doc) { + + 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()); + + + Node wikiNode = doc.getElementsByTagName("wiki").item(0); + String published = wikiNode.getFirstChild().getTextContent(); + String summary = wikiNode.getFirstChild().getNextSibling().getTextContent(); + String content = wikiNode.getFirstChild().getNextSibling().getNextSibling().getTextContent(); + + //System.out.println(published); + //System.out.println(summary); + //System.out.println(content); + + Artist artistObj = new Artist(artist); + Wiki wiki = new Wiki(); + + Album album = new Album(name, url, mbid, artistObj, listeners, playCount, userPlayCount, wiki); + return album; + + } + + public static Album parseArtist(Document doc) { + + 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, null); + return album; + + } + + public static Album parseTrack(Document doc) { + + 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, null); + return album; + + } +} diff --git a/fmframework/src/sarsoo/fmframework/test/AlbumParserTest.java b/fmframework/src/sarsoo/fmframework/test/AlbumParserTest.java index a231f12..bd7e23a 100644 --- a/fmframework/src/sarsoo/fmframework/test/AlbumParserTest.java +++ b/fmframework/src/sarsoo/fmframework/test/AlbumParserTest.java @@ -13,8 +13,8 @@ class AlbumParserTest { @Test void test() { - Document doc = Network.apiAlbumInfoCall("Pink Floyd", "The Wall", "sarsoo"); - Album album = AlbumParser.parseAlbum(doc); + //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 6373cc0..e494920 100644 --- a/fmframework/src/sarsoo/fmframework/test/NetworkTest.java +++ b/fmframework/src/sarsoo/fmframework/test/NetworkTest.java @@ -7,21 +7,30 @@ import java.io.InputStream; import org.junit.jupiter.api.Test; import org.w3c.dom.Document; +import sarsoo.fmframework.music.Album; import sarsoo.fmframework.net.Network; import sarsoo.fmframework.net.TestCall; import sarsoo.fmframework.parser.AlbumParser; +import sarsoo.fmframework.parser.Parser; class NetworkTest { @Test void testCall() { - TestCall.test("Pink Floyd", "The Wall", "sarsoo"); + //TestCall.test("Pink Floyd", "The Wall", "sarsoo"); } @Test void test() { - //Document doc = Network.apiAlbumInfoCall("Mastodon", "Leviathan", "sarsoo"); - //AlbumParser.parseAlbum(doc); + String url = Network.getAlbumInfoUrl("The Wall", "Pink Floyd", "Sarsoo"); + Document response = Network.getResponse(url); + Album album = Parser.parseAlbum(response); + System.out.println(album); + } + + @Test + void getArtistXml() { + //String url = Network.getAlbumInfoUrl("The Wall", "Pink Floyd", "Sarsoo"); + //TestCall.test(url); } - } diff --git a/fmframework/src/sarsoo/fmframework/test/ParserTest.java b/fmframework/src/sarsoo/fmframework/test/ParserTest.java new file mode 100644 index 0000000..10cbe52 --- /dev/null +++ b/fmframework/src/sarsoo/fmframework/test/ParserTest.java @@ -0,0 +1,29 @@ +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 ParserTest { + + @Test + void testParseAlbum() { + String url = Network.getAlbumInfoUrl("Pink Floyd", "The Wall", "sarsoo"); + Document doc = Network.getResponse(url); + Album album = AlbumParser.parseAlbum(doc); + assertNotNull(album); + } + + @Test + void testParseArtist() { + String url = Network.getArtistInfoUrl("Pink Floyd", "sarsoo"); + Document doc = Network.getResponse(url); + System.out.println(); + } + +}