album parsing working
This commit is contained in:
parent
2834448c59
commit
c7254f291a
@ -4,20 +4,17 @@ import java.util.ArrayList;
|
|||||||
|
|
||||||
public class Album extends FMObj{
|
public class Album extends FMObj{
|
||||||
protected Artist artist;
|
protected Artist artist;
|
||||||
protected int listeners;
|
|
||||||
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, Artist artist, int listeners, int playCount, int userPlayCount) {
|
public Album(String name) {
|
||||||
|
super(name, null, null, 0, 0, 0, null);
|
||||||
this.name = name;
|
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.artist = artist;
|
||||||
this.listeners = listeners;
|
|
||||||
this.playCount = playCount;
|
|
||||||
this.userPlayCount = userPlayCount;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
@ -3,16 +3,18 @@ package sarsoo.fmframework.music;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class Artist extends FMObj{
|
public class Artist extends FMObj{
|
||||||
protected int listeners;
|
|
||||||
protected int plays;
|
|
||||||
protected boolean streamable;
|
protected boolean streamable;
|
||||||
|
protected boolean onTour;
|
||||||
protected ArrayList<Album> albums;
|
protected ArrayList<Album> albums;
|
||||||
protected ArrayList<Track> tracks;
|
protected ArrayList<Track> tracks;
|
||||||
protected ArrayList<Artist> similarArtists;
|
protected ArrayList<Artist> similarArtists;
|
||||||
protected ArrayList<Tag> tagList;
|
protected ArrayList<Tag> tagList;
|
||||||
protected Wiki wiki;
|
|
||||||
|
|
||||||
public Artist(String name) {
|
public Artist(String name) {
|
||||||
this.name = name;
|
super(name, null, null, 0, 0, 0, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,22 @@ public class FMObj {
|
|||||||
protected String url;
|
protected String url;
|
||||||
protected String mbid;
|
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() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
@ -17,5 +33,21 @@ public class FMObj {
|
|||||||
public String getMbid() {
|
public String getMbid() {
|
||||||
return mbid;
|
return mbid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getListeners() {
|
||||||
|
return listeners;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int playCount() {
|
||||||
|
return playCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int userPlayCount() {
|
||||||
|
return userPlayCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Wiki getWiki() {
|
||||||
|
return wiki;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,12 +6,13 @@ public class Track extends FMObj{
|
|||||||
protected Album album;
|
protected Album album;
|
||||||
protected Artist artist;
|
protected Artist artist;
|
||||||
protected int trackNumber;
|
protected int trackNumber;
|
||||||
protected int id;
|
|
||||||
protected int duration;
|
protected int duration;
|
||||||
protected int playcount;
|
|
||||||
protected boolean streamable;
|
protected boolean streamable;
|
||||||
|
protected boolean isLoved;
|
||||||
protected ArrayList<Tag> tagList;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,11 +19,9 @@ import org.w3c.dom.NodeList;
|
|||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
public class Network {
|
public class Network {
|
||||||
public static Document apiAlbumInfoCall(String artist, String album, String username) {
|
|
||||||
|
public static Document getResponse(String urlString) {
|
||||||
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",
|
|
||||||
artist, album, username, Key.getKey());
|
|
||||||
URL url = new URL(urlString);
|
URL url = new URL(urlString);
|
||||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
conn.setRequestMethod("GET");
|
conn.setRequestMethod("GET");
|
||||||
@ -32,9 +30,9 @@ public class Network {
|
|||||||
if (conn.getResponseCode() != 200) {
|
if (conn.getResponseCode() != 200) {
|
||||||
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
|
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
InputStream input = conn.getInputStream();
|
InputStream input = conn.getInputStream();
|
||||||
|
|
||||||
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
|
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
|
||||||
DocumentBuilder dBuilder;
|
DocumentBuilder dBuilder;
|
||||||
try {
|
try {
|
||||||
@ -43,9 +41,9 @@ public class Network {
|
|||||||
Document doc = dBuilder.parse(input);
|
Document doc = dBuilder.parse(input);
|
||||||
conn.disconnect();
|
conn.disconnect();
|
||||||
doc.getDocumentElement().normalize();
|
doc.getDocumentElement().normalize();
|
||||||
|
|
||||||
return doc;
|
return doc;
|
||||||
|
|
||||||
} catch (SAXException e) {
|
} catch (SAXException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -53,9 +51,11 @@ public class Network {
|
|||||||
}
|
}
|
||||||
} catch (ParserConfigurationException e) {
|
} catch (ParserConfigurationException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (MalformedURLException e) {
|
} catch (
|
||||||
|
|
||||||
|
MalformedURLException e) {
|
||||||
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
||||||
@ -65,5 +65,29 @@ public class Network {
|
|||||||
|
|
||||||
}
|
}
|
||||||
return null;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,10 +9,8 @@ import java.net.URL;
|
|||||||
|
|
||||||
public class TestCall {
|
public class TestCall {
|
||||||
|
|
||||||
public static void test(String artist, String album, String username) {
|
public static void test(String urlString) {
|
||||||
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",
|
|
||||||
artist, album, username, Key.getKey());
|
|
||||||
URL url = new URL(urlString);
|
URL url = new URL(urlString);
|
||||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
conn.setRequestMethod("GET");
|
conn.setRequestMethod("GET");
|
||||||
|
@ -29,7 +29,7 @@ public class AlbumParser {
|
|||||||
|
|
||||||
Artist artistObj = new Artist(artist);
|
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;
|
return album;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
74
fmframework/src/sarsoo/fmframework/parser/Parser.java
Normal file
74
fmframework/src/sarsoo/fmframework/parser/Parser.java
Normal 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;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -13,8 +13,8 @@ class AlbumParserTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test() {
|
void test() {
|
||||||
Document doc = Network.apiAlbumInfoCall("Pink Floyd", "The Wall", "sarsoo");
|
//Document doc = Network.apiAlbumInfoCall("Pink Floyd", "The Wall", "sarsoo");
|
||||||
Album album = AlbumParser.parseAlbum(doc);
|
//Album album = AlbumParser.parseAlbum(doc);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -7,21 +7,30 @@ import java.io.InputStream;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
|
|
||||||
|
import sarsoo.fmframework.music.Album;
|
||||||
import sarsoo.fmframework.net.Network;
|
import sarsoo.fmframework.net.Network;
|
||||||
import sarsoo.fmframework.net.TestCall;
|
import sarsoo.fmframework.net.TestCall;
|
||||||
import sarsoo.fmframework.parser.AlbumParser;
|
import sarsoo.fmframework.parser.AlbumParser;
|
||||||
|
import sarsoo.fmframework.parser.Parser;
|
||||||
|
|
||||||
class NetworkTest {
|
class NetworkTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testCall() {
|
void testCall() {
|
||||||
TestCall.test("Pink Floyd", "The Wall", "sarsoo");
|
//TestCall.test("Pink Floyd", "The Wall", "sarsoo");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test() {
|
void test() {
|
||||||
//Document doc = Network.apiAlbumInfoCall("Mastodon", "Leviathan", "sarsoo");
|
String url = Network.getAlbumInfoUrl("The Wall", "Pink Floyd", "Sarsoo");
|
||||||
//AlbumParser.parseAlbum(doc);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
29
fmframework/src/sarsoo/fmframework/test/ParserTest.java
Normal file
29
fmframework/src/sarsoo/fmframework/test/ParserTest.java
Normal 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user