album parsing working

This commit is contained in:
aj 2018-03-24 17:51:23 -07:00
parent 2834448c59
commit c7254f291a
11 changed files with 203 additions and 37 deletions

View File

@ -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<Tag> tagList;
protected ArrayList<Track> 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() {

View File

@ -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<Album> albums;
protected ArrayList<Track> tracks;
protected ArrayList<Artist> similarArtists;
protected ArrayList<Tag> tagList;
protected Wiki wiki;
public Artist(String name) {
this.name = name;
super(name, null, null, 0, 0, 0, null);
}
public String toString() {
return name;
}
}

View File

@ -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;
}
}

View File

@ -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<Tag> tagList;
protected Wiki wiki;
public Track(String name, String artist) {
super(name, null, null, 0, 0, 0, null);
this.artist = new Artist(artist);
}
}

View File

@ -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;
}
}

View File

@ -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");

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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();
}
}