From 379e3a014c44d5ba2535b4e0173fac69a0cba70a Mon Sep 17 00:00:00 2001 From: aj Date: Thu, 5 Apr 2018 22:05:55 -0700 Subject: [PATCH] Added tag list pulling --- .../sarsoo/fmframework/gui/RefListsView.java | 19 +++++ .../sarsoo/fmframework/gui/TagMenuView.java | 5 ++ .../src/sarsoo/fmframework/music/Artist.java | 3 +- .../sarsoo/fmframework/net/URLBuilder.java | 6 +- .../src/sarsoo/fmframework/parser/Parser.java | 79 ++++++++++++------ .../sarsoo/fmframework/test/NetworkTest.java | 80 ++++++++++++------- .../src/sarsoo/fmframework/util/Getter.java | 19 +++-- 7 files changed, 149 insertions(+), 62 deletions(-) create mode 100644 fmframework/src/sarsoo/fmframework/gui/TagMenuView.java diff --git a/fmframework/src/sarsoo/fmframework/gui/RefListsView.java b/fmframework/src/sarsoo/fmframework/gui/RefListsView.java index 7c76721..b8a3854 100644 --- a/fmframework/src/sarsoo/fmframework/gui/RefListsView.java +++ b/fmframework/src/sarsoo/fmframework/gui/RefListsView.java @@ -8,6 +8,7 @@ import java.util.ArrayList; import javax.swing.JButton; import javax.swing.JFrame; import sarsoo.fmframework.util.FMObjList; +import sarsoo.fmframework.util.Getter; import sarsoo.fmframework.util.Reference; public class RefListsView extends JFrame { @@ -86,5 +87,23 @@ public class RefListsView extends JFrame { } }); add(viewEmoTrio); + + JButton viewRockTag = new JButton("Rock Tag"); + viewRockTag.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + FMObjList list = Getter.getTag(Reference.getUserName(), "rock"); + list.view(); + } + }); + add(viewRockTag); + + JButton viewRapTag = new JButton("Rap Tag"); + viewRapTag.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + FMObjList list = Getter.getTag(Reference.getUserName(), "rap"); + list.view(); + } + }); + add(viewRapTag); } } diff --git a/fmframework/src/sarsoo/fmframework/gui/TagMenuView.java b/fmframework/src/sarsoo/fmframework/gui/TagMenuView.java new file mode 100644 index 0000000..375d50d --- /dev/null +++ b/fmframework/src/sarsoo/fmframework/gui/TagMenuView.java @@ -0,0 +1,5 @@ +package sarsoo.fmframework.gui; + +public class TagMenuView { + +} diff --git a/fmframework/src/sarsoo/fmframework/music/Artist.java b/fmframework/src/sarsoo/fmframework/music/Artist.java index 1d7424c..1cab967 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.TestCall; import sarsoo.fmframework.net.URLBuilder; //import sarsoo.fmframework.net.TestCall; import sarsoo.fmframework.parser.Parser; @@ -26,7 +27,7 @@ public class Artist extends FMObj { public static Artist getArtist(String name, String username) { String url = URLBuilder.getArtistInfoUrl(name, username); - // TestCall.test(url); +// TestCall.test(url); Document response = Network.getResponse(url); if (response != null) { Artist artist = Parser.parseArtist(response); diff --git a/fmframework/src/sarsoo/fmframework/net/URLBuilder.java b/fmframework/src/sarsoo/fmframework/net/URLBuilder.java index 72862b3..3a730f3 100644 --- a/fmframework/src/sarsoo/fmframework/net/URLBuilder.java +++ b/fmframework/src/sarsoo/fmframework/net/URLBuilder.java @@ -111,13 +111,13 @@ public class URLBuilder { return urlString; } - public static String getUserPersonalTags(String username, String tag, FMObjType taggingType) { + public static String getUserPersonalTags(String username, String tag) { String urlString; try { - urlString = String.format("http://ws.audioscrobbler.com/2.0/?method=user.getpersonaltags&user=%s&tag=%s&taggingtype=%s&api_key=%s", + urlString = String.format("http://ws.audioscrobbler.com/2.0/?method=user.getpersonaltags&user=%s&tag=%s&taggingtype=artist&api_key=%s", URLEncoder.encode(username, "UTF-8"), URLEncoder.encode(tag, "UTF-8"), - URLEncoder.encode(taggingType.toString(), "UTF-8"), +// URLEncoder.encode(taggingType.toString().toLowerCase(), "UTF-8"), URLEncoder.encode(Key.getKey(), "UTF-8")); return urlString; } catch (UnsupportedEncodingException e) { diff --git a/fmframework/src/sarsoo/fmframework/parser/Parser.java b/fmframework/src/sarsoo/fmframework/parser/Parser.java index 863a8ff..bd4a5ef 100644 --- a/fmframework/src/sarsoo/fmframework/parser/Parser.java +++ b/fmframework/src/sarsoo/fmframework/parser/Parser.java @@ -20,6 +20,8 @@ import sarsoo.fmframework.music.Album; import sarsoo.fmframework.music.Artist; import sarsoo.fmframework.music.Track; import sarsoo.fmframework.music.Wiki; +import sarsoo.fmframework.net.URLBuilder; +import sarsoo.fmframework.util.FMObjList; import sarsoo.fmframework.util.Reference; public class Parser { @@ -54,30 +56,30 @@ public class Parser { NodeList wikiContents = wikiNode.getChildNodes(); String date = null; -// String summary = null; + // String summary = null; String content = null; int counter; Node node; for (counter = 0; counter < wikiContents.getLength(); counter++) { node = wikiContents.item(counter); - + if (node.getNodeName().equals("published")) { -// System.out.println(node.getNodeName() + node.getTextContent()); + // System.out.println(node.getNodeName() + node.getTextContent()); date = node.getTextContent(); } -// if (node.getNodeName().equals("summary")) { -// System.out.println(node.getNodeName() + node.getTextContent()); -// summary = node.getTextContent(); -// } + // if (node.getNodeName().equals("summary")) { + // System.out.println(node.getNodeName() + node.getTextContent()); + // summary = node.getTextContent(); + // } if (node.getNodeName().equals("content")) { -// System.out.println(node.getNodeName() + node.getTextContent()); + // System.out.println(node.getNodeName() + node.getTextContent()); content = node.getTextContent(); } } if (date != null && content != null) -// System.out.println("wiki init"); + // System.out.println("wiki init"); wiki = new Wiki(date, null, content); } @@ -123,23 +125,23 @@ public class Parser { Node node; for (counter = 0; counter < wikiContents.getLength(); counter++) { node = wikiContents.item(counter); - + if (node.getNodeName().equals("published")) { -// System.out.println(node.getNodeName() + node.getTextContent()); + // System.out.println(node.getNodeName() + node.getTextContent()); date = node.getTextContent(); } if (node.getNodeName().equals("summary")) { -// System.out.println(node.getNodeName() + node.getTextContent()); + // System.out.println(node.getNodeName() + node.getTextContent()); summary = node.getTextContent(); } if (node.getNodeName().equals("content")) { -// System.out.println(node.getNodeName() + node.getTextContent()); + // System.out.println(node.getNodeName() + node.getTextContent()); content = node.getTextContent(); } } if (date != null && content != null) -// System.out.println("wiki init"); + // System.out.println("wiki init"); wiki = new Wiki(date, summary, content); // if (wikiNodeList.item(0) != null) { // String date = wikiNodeList.item(0).getFirstChild().getTextContent(); @@ -214,30 +216,30 @@ public class Parser { NodeList wikiContents = wikiNode.getChildNodes(); String date = null; -// String summary = null; + // String summary = null; String content = null; int counter; Node node; for (counter = 0; counter < wikiContents.getLength(); counter++) { node = wikiContents.item(counter); - + if (node.getNodeName().equals("published")) { -// System.out.println(node.getNodeName() + node.getTextContent()); + // System.out.println(node.getNodeName() + node.getTextContent()); date = node.getTextContent(); } -// if (node.getNodeName().equals("summary")) { -// System.out.println(node.getNodeName() + node.getTextContent()); -// summary = node.getTextContent(); -// } + // if (node.getNodeName().equals("summary")) { + // System.out.println(node.getNodeName() + node.getTextContent()); + // summary = node.getTextContent(); + // } if (node.getNodeName().equals("content")) { -// System.out.println(node.getNodeName() + node.getTextContent()); + // System.out.println(node.getNodeName() + node.getTextContent()); content = node.getTextContent(); } } if (date != null && content != null) -// System.out.println("wiki init"); + // System.out.println("wiki init"); wiki = new Wiki(date, null, content); } @@ -271,6 +273,37 @@ public class Parser { } + public static FMObjList parseTagList(Document doc) { + if (doc.getDocumentElement().getAttribute("status").equals("ok")) { + NodeList objList = doc.getElementsByTagName("artist"); + + // Node obj = objList.item(0); + FMObjList list = new FMObjList(); + // String name = obj.getFirstChild().getTextContent(); + // System.out.println(name); + // + // Artist artist = Artist.getArtist(name, Reference.getUserName()); + // + // System.out.println(artist); + + int counter; + for (counter = 0; counter < objList.getLength(); counter++) { + Node obj = objList.item(counter); + + String name = obj.getFirstChild().getTextContent(); + // System.out.println(name + "."); + // System.out.println(obj.getTextContent()); + Artist artist = Artist.getArtist(name, Reference.getUserName()); + + System.out.println(artist); + list.add(artist); + } + return list; + } + return null; + + } + public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); diff --git a/fmframework/src/sarsoo/fmframework/test/NetworkTest.java b/fmframework/src/sarsoo/fmframework/test/NetworkTest.java index ef0622b..3d8fccf 100644 --- a/fmframework/src/sarsoo/fmframework/test/NetworkTest.java +++ b/fmframework/src/sarsoo/fmframework/test/NetworkTest.java @@ -15,41 +15,61 @@ import sarsoo.fmframework.net.TestCall; import sarsoo.fmframework.net.URLBuilder; import sarsoo.fmframework.parser.AlbumParser; import sarsoo.fmframework.parser.Parser; +import sarsoo.fmframework.util.FMObjList; +import sarsoo.fmframework.util.Reference; class NetworkTest { - - @Test - void testCall() { - //TestCall.test("Pink Floyd", "The Wall", "sarsoo"); - } +// +// @Test +// void testCall() { +// //TestCall.test("Pink Floyd", "The Wall", "sarsoo"); +// } +// +// @Test +// void test() { +// String url = URLBuilder.getAlbumInfoUrl("The Wall", "Pink Floyd", "Sarsoo"); +// Document response = Network.getResponse(url); +// Album album = Parser.parseAlbum(response); +// //System.out.println(album); +// } +// +// @Test +// void testArtist() { +// String url = URLBuilder.getArtistInfoUrl("Pink Floyd", "sarsoo"); +// Document response = Network.getResponse(url); +// Artist artist = Parser.parseArtist(response); +// //System.out.println(album); +// } +// +// @Test +// void testTrack() { +// String url = URLBuilder.getTrackInfoUrl("Business", "Eminem", "sarsoo"); +// Document response = Network.getResponse(url); +// Track track = Parser.parseTrack(response); +// //System.out.println(album); +// } @Test - void test() { - String url = URLBuilder.getAlbumInfoUrl("The Wall", "Pink Floyd", "Sarsoo"); + void testTag() { + Reference.setUserName("sarsoo"); + String url = URLBuilder.getUserPersonalTags("sarsoo", "rock"); Document response = Network.getResponse(url); - Album album = Parser.parseAlbum(response); - //System.out.println(album); + FMObjList list = Parser.parseTagList(response); + + list.view(); +// FMObjList list = Parser.parseTagList(response); + +// int counter; +// for(counter = 0; counter < list.size(); counter++) { +// System.out.println(list.get(counter)); +// } + +// System.out.println(url); } - @Test - void testArtist() { - String url = URLBuilder.getArtistInfoUrl("Pink Floyd", "sarsoo"); - Document response = Network.getResponse(url); - Artist artist = Parser.parseArtist(response); - //System.out.println(album); - } - - @Test - void testTrack() { - String url = URLBuilder.getTrackInfoUrl("Business", "Eminem", "sarsoo"); - Document response = Network.getResponse(url); - Track track = Parser.parseTrack(response); - //System.out.println(album); - } - - @Test - void getArtistXml() { - //String url = Network.getAlbumInfoUrl("The Wall", "Pink Floyd", "Sarsoo"); - //TestCall.test(url); - } +// @Test +// void getArtistXml() { +// //String url = Network.getAlbumInfoUrl("The Wall", "Pink Floyd", "Sarsoo"); +// //TestCall.test(url); +// } } diff --git a/fmframework/src/sarsoo/fmframework/util/Getter.java b/fmframework/src/sarsoo/fmframework/util/Getter.java index 6689761..8a8f9c3 100644 --- a/fmframework/src/sarsoo/fmframework/util/Getter.java +++ b/fmframework/src/sarsoo/fmframework/util/Getter.java @@ -35,10 +35,10 @@ public class Getter { public static Track getLastTrack() { String url = URLBuilder.getLastTrackUrl(Reference.getUserName()); -// TestCall.test(url); + // TestCall.test(url); Document doc = Network.getResponse(url); - -// System.out.println(doc.getDocumentElement().getAttribute("status")); + + // System.out.println(doc.getDocumentElement().getAttribute("status")); if (doc != null) { // System.out.println(doc.getDocumentElement().getAttribute("status")); Parser.stripSpace(doc.getDocumentElement()); @@ -49,16 +49,25 @@ public class Getter { return null; } - + public static int getScrobbles(String 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(); - if(scrobbles != null) + if (scrobbles != null) return Integer.parseInt(scrobbles); } return 0; } + public static FMObjList getTag(String username, String tag) { + String url = URLBuilder.getUserPersonalTags(username, tag); + Document doc = Network.getResponse(url); + if (doc != null) { + return Parser.parseTagList(doc); + } + return null; + } + }