From 1f5fd213049c1c7f6eb2d512812342e45957b411 Mon Sep 17 00:00:00 2001 From: aj Date: Thu, 5 Apr 2018 22:40:15 -0700 Subject: [PATCH] added tags to main menu, gets and displays all tags automatically --- .../src/sarsoo/fmframework/gui/MainMenu.java | 10 +++- .../sarsoo/fmframework/gui/RefListsView.java | 4 +- .../sarsoo/fmframework/gui/TagMenuView.java | 39 ++++++++++++++- .../src/sarsoo/fmframework/music/Tag.java | 48 ++++++++++++++++--- .../sarsoo/fmframework/net/URLBuilder.java | 17 +++++++ .../src/sarsoo/fmframework/parser/Parser.java | 38 ++++++++++++++- .../sarsoo/fmframework/test/NetworkTest.java | 16 ++++--- .../src/sarsoo/fmframework/util/Getter.java | 16 ++++++- 8 files changed, 166 insertions(+), 22 deletions(-) diff --git a/fmframework/src/sarsoo/fmframework/gui/MainMenu.java b/fmframework/src/sarsoo/fmframework/gui/MainMenu.java index 8357de6..1bb1c24 100644 --- a/fmframework/src/sarsoo/fmframework/gui/MainMenu.java +++ b/fmframework/src/sarsoo/fmframework/gui/MainMenu.java @@ -20,11 +20,12 @@ public class MainMenu extends JFrame { JButton getArtist = new JButton("Get Artist"); JButton viewLastTrack = new JButton("View Last Track"); JButton viewList = new JButton("View List"); + JButton viewTag = new JButton("View Tags"); public MainMenu() { super("fmframework - " + Reference.getUserName()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - setLayout(new GridLayout(2, 2)); + setLayout(new GridLayout(3, 2)); setSize(300, 300); setResizable(false); @@ -64,8 +65,15 @@ public class MainMenu extends JFrame { view.setVisible(true); } }); + viewTag.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + TagMenuView view = new TagMenuView(); + view.setVisible(true); + } + }); add(viewLastTrack); add(viewList); + add(viewTag); add(getAlbum); add(getArtist); diff --git a/fmframework/src/sarsoo/fmframework/gui/RefListsView.java b/fmframework/src/sarsoo/fmframework/gui/RefListsView.java index b8a3854..08402ff 100644 --- a/fmframework/src/sarsoo/fmframework/gui/RefListsView.java +++ b/fmframework/src/sarsoo/fmframework/gui/RefListsView.java @@ -91,7 +91,7 @@ public class RefListsView extends JFrame { JButton viewRockTag = new JButton("Rock Tag"); viewRockTag.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { - FMObjList list = Getter.getTag(Reference.getUserName(), "rock"); + FMObjList list = Getter.getUserTag(Reference.getUserName(), "rock"); list.view(); } }); @@ -100,7 +100,7 @@ public class RefListsView extends JFrame { JButton viewRapTag = new JButton("Rap Tag"); viewRapTag.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { - FMObjList list = Getter.getTag(Reference.getUserName(), "rap"); + FMObjList list = Getter.getUserTag(Reference.getUserName(), "rap"); list.view(); } }); diff --git a/fmframework/src/sarsoo/fmframework/gui/TagMenuView.java b/fmframework/src/sarsoo/fmframework/gui/TagMenuView.java index 375d50d..c8f7c3c 100644 --- a/fmframework/src/sarsoo/fmframework/gui/TagMenuView.java +++ b/fmframework/src/sarsoo/fmframework/gui/TagMenuView.java @@ -1,5 +1,42 @@ package sarsoo.fmframework.gui; -public class TagMenuView { +import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.ArrayList; +import javax.swing.JButton; +import javax.swing.JFrame; + +import sarsoo.fmframework.music.Tag; +import sarsoo.fmframework.util.FMObjList; +import sarsoo.fmframework.util.Getter; +import sarsoo.fmframework.util.Reference; + +public class TagMenuView extends JFrame { + + public TagMenuView() { + super("View Tags"); + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + setLayout(new GridLayout(3, 2)); + setSize(300, 300); + setResizable(false); + + ArrayList tags = Getter.getUserTags(Reference.getUserName()); + + int counter; + for(counter = 0; counter < tags.size(); counter++) { + Tag tag = tags.get(counter); + + JButton view = new JButton(tag.getName()); + view.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + + FMObjList list = Getter.getUserTag(Reference.getUserName(), tag.getName()); + list.view(); + } + }); + add(view); + } + } } diff --git a/fmframework/src/sarsoo/fmframework/music/Tag.java b/fmframework/src/sarsoo/fmframework/music/Tag.java index 0225bd8..bebf5bf 100644 --- a/fmframework/src/sarsoo/fmframework/music/Tag.java +++ b/fmframework/src/sarsoo/fmframework/music/Tag.java @@ -1,11 +1,45 @@ package sarsoo.fmframework.music; public class Tag { - protected String name; - protected String url; - protected int reach; - protected int taggings; - protected Boolean streamable; - protected String summary; - protected String content; + private String name; + private String url; + private int count; + private int reach; + private int taggings; + private Boolean streamable; + private String summary; + private String content; + + public Tag(String name, String url) { + this.setName(name); + this.setUrl(url); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public String toString() { + return name; + } } diff --git a/fmframework/src/sarsoo/fmframework/net/URLBuilder.java b/fmframework/src/sarsoo/fmframework/net/URLBuilder.java index 3a730f3..bb18c1f 100644 --- a/fmframework/src/sarsoo/fmframework/net/URLBuilder.java +++ b/fmframework/src/sarsoo/fmframework/net/URLBuilder.java @@ -127,6 +127,23 @@ public class URLBuilder { // System.out.println(urlString); return null; } + + public static String getUserTopTags(String username) { + String urlString; + try { + urlString = String.format("http://ws.audioscrobbler.com/2.0/?method=user.gettoptags&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 getUserTopTags() { // diff --git a/fmframework/src/sarsoo/fmframework/parser/Parser.java b/fmframework/src/sarsoo/fmframework/parser/Parser.java index bd4a5ef..5d45477 100644 --- a/fmframework/src/sarsoo/fmframework/parser/Parser.java +++ b/fmframework/src/sarsoo/fmframework/parser/Parser.java @@ -3,6 +3,7 @@ package sarsoo.fmframework.parser; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; +import java.util.ArrayList; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; @@ -18,6 +19,7 @@ import org.w3c.dom.NodeList; import sarsoo.fmframework.music.Album; import sarsoo.fmframework.music.Artist; +import sarsoo.fmframework.music.Tag; import sarsoo.fmframework.music.Track; import sarsoo.fmframework.music.Wiki; import sarsoo.fmframework.net.URLBuilder; @@ -272,8 +274,40 @@ public class Parser { return null; } + + public static ArrayList parseUserTags(Document doc) { + if (doc.getDocumentElement().getAttribute("status").equals("ok")) { + NodeList objList = doc.getElementsByTagName("tag"); - public static FMObjList parseTagList(Document doc) { + // Node obj = objList.item(0); + ArrayList list = new ArrayList(); + // 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++) { + NodeList obj = objList.item(counter).getChildNodes(); + + String name = obj.item(0).getTextContent(); + String url = obj.item(4).getTextContent(); + // System.out.println(obj.getTextContent()); + + Tag tag = new Tag(name, url); + +// System.out.println(name + " " + url); + list.add(tag); + } + return list; + } + return null; + + } + + public static FMObjList parseUserTagList(Document doc) { if (doc.getDocumentElement().getAttribute("status").equals("ok")) { NodeList objList = doc.getElementsByTagName("artist"); @@ -295,7 +329,7 @@ public class Parser { // System.out.println(obj.getTextContent()); Artist artist = Artist.getArtist(name, Reference.getUserName()); - System.out.println(artist); +// System.out.println(artist); list.add(artist); } return list; diff --git a/fmframework/src/sarsoo/fmframework/test/NetworkTest.java b/fmframework/src/sarsoo/fmframework/test/NetworkTest.java index 3d8fccf..50225c8 100644 --- a/fmframework/src/sarsoo/fmframework/test/NetworkTest.java +++ b/fmframework/src/sarsoo/fmframework/test/NetworkTest.java @@ -3,12 +3,14 @@ package sarsoo.fmframework.test; import static org.junit.jupiter.api.Assertions.*; import java.io.InputStream; +import java.util.ArrayList; import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import sarsoo.fmframework.music.Album; import sarsoo.fmframework.music.Artist; +import sarsoo.fmframework.music.Tag; import sarsoo.fmframework.music.Track; import sarsoo.fmframework.net.Network; import sarsoo.fmframework.net.TestCall; @@ -52,17 +54,17 @@ class NetworkTest { @Test void testTag() { Reference.setUserName("sarsoo"); - String url = URLBuilder.getUserPersonalTags("sarsoo", "rock"); + String url = URLBuilder.getUserTopTags("sarsoo"); Document response = Network.getResponse(url); - FMObjList list = Parser.parseTagList(response); + ArrayList list = Parser.parseUserTags(response); - list.view(); + // FMObjList list = Parser.parseTagList(response); -// int counter; -// for(counter = 0; counter < list.size(); counter++) { -// System.out.println(list.get(counter)); -// } + int counter; + for(counter = 0; counter < list.size(); counter++) { + System.out.println(list.get(counter)); + } // System.out.println(url); } diff --git a/fmframework/src/sarsoo/fmframework/util/Getter.java b/fmframework/src/sarsoo/fmframework/util/Getter.java index 8a8f9c3..c0ba11c 100644 --- a/fmframework/src/sarsoo/fmframework/util/Getter.java +++ b/fmframework/src/sarsoo/fmframework/util/Getter.java @@ -1,11 +1,14 @@ package sarsoo.fmframework.util; +import java.util.ArrayList; + import javax.swing.JOptionPane; import org.w3c.dom.Document; import sarsoo.fmframework.music.Album; import sarsoo.fmframework.music.Artist; +import sarsoo.fmframework.music.Tag; import sarsoo.fmframework.music.Track; import sarsoo.fmframework.net.Network; import sarsoo.fmframework.net.TestCall; @@ -61,11 +64,20 @@ public class Getter { return 0; } - public static FMObjList getTag(String username, String tag) { + public static FMObjList getUserTag(String username, String tag) { String url = URLBuilder.getUserPersonalTags(username, tag); Document doc = Network.getResponse(url); if (doc != null) { - return Parser.parseTagList(doc); + return Parser.parseUserTagList(doc); + } + return null; + } + + public static ArrayList getUserTags(String username) { + String url = URLBuilder.getUserTopTags(username); + Document doc = Network.getResponse(url); + if (doc != null) { + return Parser.parseUserTags(doc); } return null; }