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{
|
||||
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() {
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
@ -18,4 +34,20 @@ public class FMObj {
|
||||
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 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);
|
||||
}
|
||||
}
|
||||
|
@ -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) {
|
||||
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());
|
||||
public static Document getResponse(String urlString) {
|
||||
try {
|
||||
URL url = new URL(urlString);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
@ -55,7 +53,9 @@ public class Network {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -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");
|
||||
|
@ -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;
|
||||
|
||||
}
|
||||
|
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
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
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