parsing working, static get methods for objects

This commit is contained in:
aj 2018-03-24 18:35:29 -07:00
parent c7254f291a
commit 9828dd1b0c
5 changed files with 102 additions and 20 deletions

View File

@ -2,6 +2,11 @@ package sarsoo.fmframework.music;
import java.util.ArrayList; import java.util.ArrayList;
import org.w3c.dom.Document;
import sarsoo.fmframework.net.Network;
import sarsoo.fmframework.parser.Parser;
public class Album extends FMObj{ public class Album extends FMObj{
protected Artist artist; protected Artist artist;
protected ArrayList<Tag> tagList; protected ArrayList<Tag> tagList;
@ -22,4 +27,11 @@ public class Album extends FMObj{
} }
public static Album getAlbum(String name, String artist, String username) {
String url = Network.getAlbumInfoUrl(name, artist, username);
Document response = Network.getResponse(url);
Album album = Parser.parseAlbum(response);
return album;
}
} }

View File

@ -2,6 +2,11 @@ package sarsoo.fmframework.music;
import java.util.ArrayList; import java.util.ArrayList;
import org.w3c.dom.Document;
import sarsoo.fmframework.net.Network;
import sarsoo.fmframework.parser.Parser;
public class Artist extends FMObj{ public class Artist extends FMObj{
protected boolean streamable; protected boolean streamable;
protected boolean onTour; protected boolean onTour;
@ -14,7 +19,18 @@ public class Artist extends FMObj{
super(name, null, null, 0, 0, 0, null); super(name, null, null, 0, 0, 0, null);
} }
public Artist(String name, String url, String mbid, int listeners, int playCount, int userPlayCount, boolean streamable, boolean onTour, Wiki wiki) {
super(name, url, mbid, listeners, playCount, userPlayCount, wiki);
}
public String toString() { public String toString() {
return name; return name;
} }
public static Artist getArtist(String name, String username) {
String url = Network.getArtistInfoUrl(name, username);
Document response = Network.getResponse(url);
Artist artist = Parser.parseArtist(response);
return artist;
}
} }

View File

@ -2,6 +2,11 @@ package sarsoo.fmframework.music;
import java.util.ArrayList; import java.util.ArrayList;
import org.w3c.dom.Document;
import sarsoo.fmframework.net.Network;
import sarsoo.fmframework.parser.Parser;
public class Track extends FMObj{ public class Track extends FMObj{
protected Album album; protected Album album;
protected Artist artist; protected Artist artist;
@ -15,4 +20,15 @@ public class Track extends FMObj{
super(name, null, null, 0, 0, 0, null); super(name, null, null, 0, 0, 0, null);
this.artist = new Artist(artist); this.artist = new Artist(artist);
} }
public Track(String name, String url, String mbid, int listeners, int playCount, int userPlayCount, Wiki wiki) {
super(name, url, mbid, listeners, playCount, userPlayCount, wiki);
}
public static Track getTrack(String name, String artist, String username) {
String url = Network.getTrackInfoUrl(name, artist, username);
Document response = Network.getResponse(url);
Track track = Parser.parseTrack(response);
return track;
}
} }

View File

@ -6,6 +6,7 @@ import org.w3c.dom.NodeList;
import sarsoo.fmframework.music.Album; import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Artist; import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.music.Track;
import sarsoo.fmframework.music.Wiki; import sarsoo.fmframework.music.Wiki;
public class Parser { public class Parser {
@ -20,6 +21,24 @@ public class Parser {
int playCount = Integer.parseInt(doc.getElementsByTagName("playcount").item(0).getTextContent()); int playCount = Integer.parseInt(doc.getElementsByTagName("playcount").item(0).getTextContent());
int userPlayCount = Integer.parseInt(doc.getElementsByTagName("userplaycount").item(0).getTextContent()); int userPlayCount = Integer.parseInt(doc.getElementsByTagName("userplaycount").item(0).getTextContent());
// Node trackListNode = doc.getElementsByTagName("tracks").item(0);
// NodeList trackNodeList = trackListNode.getChildNodes();
// if (trackListNode != null) {
// int counter = 0;
// while (trackNodeList.item(counter) != null) {
// Node track = trackNodeList.item(counter);
// //System.out.println(track.getTextContent());
// String trackName = track.getFirstChild().getTextContent();
// String trackUrl = track.getFirstChild().getNextSibling().getNextSibling().getTextContent();
// //String trackDuration = track.getFirstChild().getNextSibling().getNextSibling().getNextSibling().getNextSibling().getTextContent();
// int trackNumber = Integer.parseInt(track.getAttributes().getNamedItem("rank").getNodeValue());
// // String trackName =
// // trackListNode.getFirstChild().getFirstChild().getTextContent();
// // String trackUrl = trackListNode.getNextSibling().getTextContent();
// System.out.println(trackUrl);
// counter++;
// }
// }
Node wikiNode = doc.getElementsByTagName("wiki").item(0); Node wikiNode = doc.getElementsByTagName("wiki").item(0);
String published = wikiNode.getFirstChild().getTextContent(); String published = wikiNode.getFirstChild().getTextContent();
@ -38,37 +57,38 @@ public class Parser {
} }
public static Album parseArtist(Document doc) { public static Artist parseArtist(Document doc) {
String name = doc.getElementsByTagName("name").item(0).getTextContent(); 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();
String streamable = doc.getElementsByTagName("streamable").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());
//System.out.println(listeners);
Artist artist = new Artist(name, url, mbid, listeners, playCount, userPlayCount, false, false, null);
return artist;
}
public static Track parseTrack(Document doc) {
String name = doc.getElementsByTagName("name").item(0).getTextContent();
String artistName = doc.getElementsByTagName("artist").item(0).getFirstChild().getTextContent();
String mbid = doc.getElementsByTagName("mbid").item(0).getTextContent(); String mbid = doc.getElementsByTagName("mbid").item(0).getTextContent();
String url = doc.getElementsByTagName("url").item(0).getTextContent(); String url = doc.getElementsByTagName("url").item(0).getTextContent();
int listeners = Integer.parseInt(doc.getElementsByTagName("listeners").item(0).getTextContent()); int listeners = Integer.parseInt(doc.getElementsByTagName("listeners").item(0).getTextContent());
int playCount = Integer.parseInt(doc.getElementsByTagName("playcount").item(0).getTextContent()); int playCount = Integer.parseInt(doc.getElementsByTagName("playcount").item(0).getTextContent());
int userPlayCount = Integer.parseInt(doc.getElementsByTagName("userplaycount").item(0).getTextContent()); int userPlayCount = Integer.parseInt(doc.getElementsByTagName("userplaycount").item(0).getTextContent());
Artist artistObj = new Artist(artist); Artist artistObj = new Artist(artistName);
Album album = new Album(name, url, mbid, artistObj, listeners, playCount, userPlayCount, null); // System.out.println(userPlayCount);
return album;
} Track track = new Track(name, url, mbid, listeners, playCount, userPlayCount, null);
return track;
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

@ -8,6 +8,8 @@ import org.junit.jupiter.api.Test;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import sarsoo.fmframework.music.Album; import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.music.Track;
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;
@ -25,7 +27,23 @@ class NetworkTest {
String url = Network.getAlbumInfoUrl("The Wall", "Pink Floyd", "Sarsoo"); String url = Network.getAlbumInfoUrl("The Wall", "Pink Floyd", "Sarsoo");
Document response = Network.getResponse(url); Document response = Network.getResponse(url);
Album album = Parser.parseAlbum(response); Album album = Parser.parseAlbum(response);
System.out.println(album); //System.out.println(album);
}
@Test
void testArtist() {
String url = Network.getArtistInfoUrl("Pink Floyd", "sarsoo");
Document response = Network.getResponse(url);
Artist artist = Parser.parseArtist(response);
//System.out.println(album);
}
@Test
void testTrack() {
String url = Network.getTrackInfoUrl("Business", "Eminem", "sarsoo");
Document response = Network.getResponse(url);
Track track = Parser.parseTrack(response);
//System.out.println(album);
} }
@Test @Test