From f6d8f5f401306ac5a0981cf91a559853b9706ed5 Mon Sep 17 00:00:00 2001 From: aj Date: Thu, 5 Apr 2018 11:34:58 -0700 Subject: [PATCH] moved url getter methods to separate class URLBuilder --- .../src/sarsoo/fmframework/music/Album.java | 3 +- .../src/sarsoo/fmframework/music/Artist.java | 5 +- .../src/sarsoo/fmframework/music/Track.java | 5 +- .../src/sarsoo/fmframework/net/Network.java | 119 -------------- .../sarsoo/fmframework/net/URLBuilder.java | 155 ++++++++++++++++++ .../sarsoo/fmframework/test/NetworkTest.java | 7 +- .../sarsoo/fmframework/test/ParserTest.java | 5 +- .../src/sarsoo/fmframework/util/Getter.java | 5 +- 8 files changed, 173 insertions(+), 131 deletions(-) create mode 100644 fmframework/src/sarsoo/fmframework/net/URLBuilder.java diff --git a/fmframework/src/sarsoo/fmframework/music/Album.java b/fmframework/src/sarsoo/fmframework/music/Album.java index 3757c3c..a2d2eb8 100644 --- a/fmframework/src/sarsoo/fmframework/music/Album.java +++ b/fmframework/src/sarsoo/fmframework/music/Album.java @@ -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); diff --git a/fmframework/src/sarsoo/fmframework/music/Artist.java b/fmframework/src/sarsoo/fmframework/music/Artist.java index 5f723f6..1d7424c 100644 --- a/fmframework/src/sarsoo/fmframework/music/Artist.java +++ b/fmframework/src/sarsoo/fmframework/music/Artist.java @@ -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; diff --git a/fmframework/src/sarsoo/fmframework/music/Track.java b/fmframework/src/sarsoo/fmframework/music/Track.java index 5689e46..48adf0c 100644 --- a/fmframework/src/sarsoo/fmframework/music/Track.java +++ b/fmframework/src/sarsoo/fmframework/music/Track.java @@ -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() { diff --git a/fmframework/src/sarsoo/fmframework/net/Network.java b/fmframework/src/sarsoo/fmframework/net/Network.java index 81a5300..ac3de70 100644 --- a/fmframework/src/sarsoo/fmframework/net/Network.java +++ b/fmframework/src/sarsoo/fmframework/net/Network.java @@ -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; - // } } diff --git a/fmframework/src/sarsoo/fmframework/net/URLBuilder.java b/fmframework/src/sarsoo/fmframework/net/URLBuilder.java new file mode 100644 index 0000000..72862b3 --- /dev/null +++ b/fmframework/src/sarsoo/fmframework/net/URLBuilder.java @@ -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; + // } + +} diff --git a/fmframework/src/sarsoo/fmframework/test/NetworkTest.java b/fmframework/src/sarsoo/fmframework/test/NetworkTest.java index 826c0d9..ef0622b 100644 --- a/fmframework/src/sarsoo/fmframework/test/NetworkTest.java +++ b/fmframework/src/sarsoo/fmframework/test/NetworkTest.java @@ -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); diff --git a/fmframework/src/sarsoo/fmframework/test/ParserTest.java b/fmframework/src/sarsoo/fmframework/test/ParserTest.java index 10cbe52..1790367 100644 --- a/fmframework/src/sarsoo/fmframework/test/ParserTest.java +++ b/fmframework/src/sarsoo/fmframework/test/ParserTest.java @@ -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(); } diff --git a/fmframework/src/sarsoo/fmframework/util/Getter.java b/fmframework/src/sarsoo/fmframework/util/Getter.java index bb121c3..6689761 100644 --- a/fmframework/src/sarsoo/fmframework/util/Getter.java +++ b/fmframework/src/sarsoo/fmframework/util/Getter.java @@ -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();