moved url getter methods to separate class URLBuilder

This commit is contained in:
aj 2018-04-05 11:34:58 -07:00
parent b620b6e5d6
commit f6d8f5f401
8 changed files with 173 additions and 131 deletions

View File

@ -6,6 +6,7 @@ import org.w3c.dom.Document;
import sarsoo.fmframework.gui.AlbumView;
import sarsoo.fmframework.net.Network;
import sarsoo.fmframework.net.URLBuilder;
import sarsoo.fmframework.parser.Parser;
import sarsoo.fmframework.util.Reference;
@ -30,7 +31,7 @@ public class Album extends FMObj {
}
public static Album getAlbum(String name, String artist, String username) {
String url = Network.getAlbumInfoUrl(name, artist, username);
String url = URLBuilder.getAlbumInfoUrl(name, artist, username);
Document response = Network.getResponse(url);
if (response != null) {
Album album = Parser.parseAlbum(response);

View File

@ -5,6 +5,7 @@ import java.util.ArrayList;
import org.w3c.dom.Document;
import sarsoo.fmframework.net.Network;
import sarsoo.fmframework.net.URLBuilder;
//import sarsoo.fmframework.net.TestCall;
import sarsoo.fmframework.parser.Parser;
@ -24,7 +25,7 @@ public class Artist extends FMObj {
}
public static Artist getArtist(String name, String username) {
String url = Network.getArtistInfoUrl(name, username);
String url = URLBuilder.getArtistInfoUrl(name, username);
// TestCall.test(url);
Document response = Network.getResponse(url);
if (response != null) {
@ -35,7 +36,7 @@ public class Artist extends FMObj {
}
public static Artist getArtistByMbid(String mbid, String username) {
String url = Network.getArtistInfoMbidUrl(mbid, username);
String url = URLBuilder.getArtistInfoMbidUrl(mbid, username);
Document response = Network.getResponse(url);
Artist artist = Parser.parseArtist(response);
return artist;

View File

@ -6,6 +6,7 @@ import org.w3c.dom.Document;
import sarsoo.fmframework.gui.TrackView;
import sarsoo.fmframework.net.Network;
import sarsoo.fmframework.net.URLBuilder;
//import sarsoo.fmframework.net.TestCall;
import sarsoo.fmframework.parser.Parser;
@ -30,7 +31,7 @@ public class Track extends FMObj {
}
public static Track getTrack(String name, String artist, String username) {
String url = Network.getTrackInfoUrl(name, artist, username);
String url = URLBuilder.getTrackInfoUrl(name, artist, username);
// TestCall.test(url);
Document response = Network.getResponse(url);
if (response != null) {
@ -58,7 +59,7 @@ public class Track extends FMObj {
}
public String getLyricsURL() {
return Network.getLyricsUrl(name, artist.getName());
return URLBuilder.getLyricsUrl(name, artist.getName());
}
public Album getAlbum() {

View File

@ -3,12 +3,10 @@ package sarsoo.fmframework.net;
import java.awt.Desktop;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@ -65,103 +63,6 @@ public class Network {
}
public static String getArtistInfoUrl(String artist, String username) {
String urlString;
try {
urlString = String.format(
"http://ws.audioscrobbler.com/2.0/?method=artist.getInfo&artist=%s&autocorrect=0&username=%s&api_key=%s",
URLEncoder.encode(artist, "UTF-8"), URLEncoder.encode(username, "UTF-8"), URLEncoder.encode(Key.getKey(), "UTF-8"));
return urlString;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static String getArtistInfoMbidUrl(String mbid, String username) {
String urlString;
try {
urlString = String.format(
"http://ws.audioscrobbler.com/2.0/?method=artist.getInfo&mbid=%s&autocorrect=0&username=%s&api_key=%s",
URLEncoder.encode(mbid, "UTF-8"), URLEncoder.encode(username, "UTF-8"), URLEncoder.encode(Key.getKey(), "UTF-8"));
return urlString;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static String getAlbumInfoUrl(String album, String artist, String username) {
String urlString;
try {
urlString = String.format(
"http://ws.audioscrobbler.com/2.0/?method=album.getInfo&album=%s&artist=%s&autocorrect=0&username=%s&api_key=%s",
URLEncoder.encode(album, "UTF-8"), URLEncoder.encode(artist, "UTF-8"), URLEncoder.encode(username, "UTF-8"), URLEncoder.encode(Key.getKey(), "UTF-8"));
return urlString;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static String getTrackInfoUrl(String name, String artist, String username) {
String urlString;
try {
urlString = String.format(
"http://ws.audioscrobbler.com/2.0/?method=track.getInfo&track=%s&artist=%s&autocorrect=0&username=%s&api_key=%s",
URLEncoder.encode(name, "UTF-8"), URLEncoder.encode(artist, "UTF-8"), URLEncoder.encode(username, "UTF-8"), URLEncoder.encode(Key.getKey(), "UTF-8"));
return urlString;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static String getLastTrackUrl(String username) {
String urlString;
try {
urlString = String.format(
"http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=%s&limit=1&api_key=%s",
URLEncoder.encode(username, "UTF-8"), URLEncoder.encode(Key.getKey(), "UTF-8"));
return urlString;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println(urlString);
return null;
}
public static String getUserInfoUrl(String username) {
String urlString;
try {
urlString = String.format(
"http://ws.audioscrobbler.com/2.0/?method=user.getinfo&user=%s&api_key=%s",
URLEncoder.encode(username, "UTF-8"), URLEncoder.encode(Key.getKey(), "UTF-8"));
return urlString;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println(urlString);
return null;
}
public static String getLyricsUrl(String trackName, String artistName) {
String track = trackName.replaceAll(" ", "-");
// track = track.replaceAll("[^a-zA-Z ]", "").toLowerCase();
String artist = artistName.replaceAll(" ", "-");
// artist = artist.replaceAll("[^a-zA-Z ]", "").toLowerCase();
String urlString = String.format("http://genius.com/%s-%s-lyrics", artist, track);
return urlString;
}
public static void openURL(String url) {
try {
Desktop desktop = java.awt.Desktop.getDesktop();
@ -171,24 +72,4 @@ public class Network {
e.printStackTrace();
}
}
// public static String getRecentTracaksUrl(String username) {
//// Date date = new Date();
//// Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
//// Calendar cal2 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
////
//// cal.set(cal.YEAR, cal.MONTH, cal.DATE);
//// System.out.println(cal.getTime());
// //System.out.println(cal2.getTime());
//
// String urlString = String.format(
// "http://ws.audioscrobbler.com/2.0/"
// + "?method=user.getRecentTracks&"
// + "user=%s&"
// + "limit = 200&"
// + "from=%d&"
// + "api_key=%s",
// username, Key.getKey());
// return urlString;
// }
}

View File

@ -0,0 +1,155 @@
package sarsoo.fmframework.net;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class URLBuilder {
public enum FMObjType{
ARTIST,
ALBUM,
TRACK
}
public static String getArtistInfoUrl(String artist, String username) {
String urlString;
try {
urlString = String.format(
"http://ws.audioscrobbler.com/2.0/?method=artist.getInfo&artist=%s&autocorrect=0&username=%s&api_key=%s",
URLEncoder.encode(artist, "UTF-8"), URLEncoder.encode(username, "UTF-8"),
URLEncoder.encode(Key.getKey(), "UTF-8"));
return urlString;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static String getArtistInfoMbidUrl(String mbid, String username) {
String urlString;
try {
urlString = String.format(
"http://ws.audioscrobbler.com/2.0/?method=artist.getInfo&mbid=%s&autocorrect=0&username=%s&api_key=%s",
URLEncoder.encode(mbid, "UTF-8"), URLEncoder.encode(username, "UTF-8"),
URLEncoder.encode(Key.getKey(), "UTF-8"));
return urlString;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static String getAlbumInfoUrl(String album, String artist, String username) {
String urlString;
try {
urlString = String.format(
"http://ws.audioscrobbler.com/2.0/?method=album.getInfo&album=%s&artist=%s&autocorrect=0&username=%s&api_key=%s",
URLEncoder.encode(album, "UTF-8"), URLEncoder.encode(artist, "UTF-8"),
URLEncoder.encode(username, "UTF-8"), URLEncoder.encode(Key.getKey(), "UTF-8"));
return urlString;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static String getTrackInfoUrl(String name, String artist, String username) {
String urlString;
try {
urlString = String.format(
"http://ws.audioscrobbler.com/2.0/?method=track.getInfo&track=%s&artist=%s&autocorrect=0&username=%s&api_key=%s",
URLEncoder.encode(name, "UTF-8"), URLEncoder.encode(artist, "UTF-8"),
URLEncoder.encode(username, "UTF-8"), URLEncoder.encode(Key.getKey(), "UTF-8"));
return urlString;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static String getLastTrackUrl(String username) {
String urlString;
try {
urlString = String.format(
"http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=%s&limit=1&api_key=%s",
URLEncoder.encode(username, "UTF-8"), URLEncoder.encode(Key.getKey(), "UTF-8"));
return urlString;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println(urlString);
return null;
}
public static String getUserInfoUrl(String username) {
String urlString;
try {
urlString = String.format("http://ws.audioscrobbler.com/2.0/?method=user.getinfo&user=%s&api_key=%s",
URLEncoder.encode(username, "UTF-8"), URLEncoder.encode(Key.getKey(), "UTF-8"));
return urlString;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println(urlString);
return null;
}
public static String getLyricsUrl(String trackName, String artistName) {
String track = trackName.replaceAll(" ", "-");
// track = track.replaceAll("[^a-zA-Z ]", "").toLowerCase();
String artist = artistName.replaceAll(" ", "-");
// artist = artist.replaceAll("[^a-zA-Z ]", "").toLowerCase();
String urlString = String.format("http://genius.com/%s-%s-lyrics", artist, track);
return urlString;
}
public static String getUserPersonalTags(String username, String tag, FMObjType taggingType) {
String urlString;
try {
urlString = String.format("http://ws.audioscrobbler.com/2.0/?method=user.getpersonaltags&user=%s&tag=%s&taggingtype=%s&api_key=%s",
URLEncoder.encode(username, "UTF-8"),
URLEncoder.encode(tag, "UTF-8"),
URLEncoder.encode(taggingType.toString(), "UTF-8"),
URLEncoder.encode(Key.getKey(), "UTF-8"));
return urlString;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println(urlString);
return null;
}
// public static String getUserTopTags() {
//
// }
// public static String getRecentTracaksUrl(String username) {
//// Date date = new Date();
//// Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
//// Calendar cal2 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
////
//// cal.set(cal.YEAR, cal.MONTH, cal.DATE);
//// System.out.println(cal.getTime());
// //System.out.println(cal2.getTime());
//
// String urlString = String.format(
// "http://ws.audioscrobbler.com/2.0/"
// + "?method=user.getRecentTracks&"
// + "user=%s&"
// + "limit = 200&"
// + "from=%d&"
// + "api_key=%s",
// username, Key.getKey());
// return urlString;
// }
}

View File

@ -12,6 +12,7 @@ import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.music.Track;
import sarsoo.fmframework.net.Network;
import sarsoo.fmframework.net.TestCall;
import sarsoo.fmframework.net.URLBuilder;
import sarsoo.fmframework.parser.AlbumParser;
import sarsoo.fmframework.parser.Parser;
@ -24,7 +25,7 @@ class NetworkTest {
@Test
void test() {
String url = Network.getAlbumInfoUrl("The Wall", "Pink Floyd", "Sarsoo");
String url = URLBuilder.getAlbumInfoUrl("The Wall", "Pink Floyd", "Sarsoo");
Document response = Network.getResponse(url);
Album album = Parser.parseAlbum(response);
//System.out.println(album);
@ -32,7 +33,7 @@ class NetworkTest {
@Test
void testArtist() {
String url = Network.getArtistInfoUrl("Pink Floyd", "sarsoo");
String url = URLBuilder.getArtistInfoUrl("Pink Floyd", "sarsoo");
Document response = Network.getResponse(url);
Artist artist = Parser.parseArtist(response);
//System.out.println(album);
@ -40,7 +41,7 @@ class NetworkTest {
@Test
void testTrack() {
String url = Network.getTrackInfoUrl("Business", "Eminem", "sarsoo");
String url = URLBuilder.getTrackInfoUrl("Business", "Eminem", "sarsoo");
Document response = Network.getResponse(url);
Track track = Parser.parseTrack(response);
//System.out.println(album);

View File

@ -7,13 +7,14 @@ import org.w3c.dom.Document;
import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.net.Network;
import sarsoo.fmframework.net.URLBuilder;
import sarsoo.fmframework.parser.AlbumParser;
class ParserTest {
@Test
void testParseAlbum() {
String url = Network.getAlbumInfoUrl("Pink Floyd", "The Wall", "sarsoo");
String url = URLBuilder.getAlbumInfoUrl("Pink Floyd", "The Wall", "sarsoo");
Document doc = Network.getResponse(url);
Album album = AlbumParser.parseAlbum(doc);
assertNotNull(album);
@ -21,7 +22,7 @@ class ParserTest {
@Test
void testParseArtist() {
String url = Network.getArtistInfoUrl("Pink Floyd", "sarsoo");
String url = URLBuilder.getArtistInfoUrl("Pink Floyd", "sarsoo");
Document doc = Network.getResponse(url);
System.out.println();
}

View File

@ -9,6 +9,7 @@ import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.music.Track;
import sarsoo.fmframework.net.Network;
import sarsoo.fmframework.net.TestCall;
import sarsoo.fmframework.net.URLBuilder;
import sarsoo.fmframework.parser.Parser;
public class Getter {
@ -33,7 +34,7 @@ public class Getter {
public static Track getLastTrack() {
String url = Network.getLastTrackUrl(Reference.getUserName());
String url = URLBuilder.getLastTrackUrl(Reference.getUserName());
// TestCall.test(url);
Document doc = Network.getResponse(url);
@ -50,7 +51,7 @@ public class Getter {
}
public static int getScrobbles(String username) {
String url = Network.getUserInfoUrl(username);
String url = URLBuilder.getUserInfoUrl(username);
Document doc = Network.getResponse(url);
if (doc.getDocumentElement().getAttribute("status").equals("ok")) {
String scrobbles = doc.getElementsByTagName("playcount").item(0).getTextContent();