From 57d2ec312c015798a071aa2b327cd223956f8b38 Mon Sep 17 00:00:00 2001 From: aj Date: Fri, 6 Apr 2018 00:32:21 -0700 Subject: [PATCH] added todays scrobbles as button on main --- .../src/sarsoo/fmframework/gui/MainMenu.java | 9 +++++ .../sarsoo/fmframework/gui/TagMenuView.java | 7 ++-- .../sarsoo/fmframework/net/URLBuilder.java | 39 ++++++++++++++++++- .../src/sarsoo/fmframework/parser/Parser.java | 1 + .../sarsoo/fmframework/test/NetworkTest.java | 15 ++----- .../src/sarsoo/fmframework/util/Getter.java | 17 +++++++- 6 files changed, 72 insertions(+), 16 deletions(-) diff --git a/fmframework/src/sarsoo/fmframework/gui/MainMenu.java b/fmframework/src/sarsoo/fmframework/gui/MainMenu.java index 1bb1c24..51c20c6 100644 --- a/fmframework/src/sarsoo/fmframework/gui/MainMenu.java +++ b/fmframework/src/sarsoo/fmframework/gui/MainMenu.java @@ -11,6 +11,7 @@ import javax.swing.JOptionPane; import sarsoo.fmframework.music.Album; import sarsoo.fmframework.music.Artist; import sarsoo.fmframework.music.Track; +import sarsoo.fmframework.net.Network; import sarsoo.fmframework.util.Getter; import sarsoo.fmframework.util.Reference; @@ -21,6 +22,7 @@ public class MainMenu extends JFrame { JButton viewLastTrack = new JButton("View Last Track"); JButton viewList = new JButton("View List"); JButton viewTag = new JButton("View Tags"); + JButton today = new JButton(); public MainMenu() { super("fmframework - " + Reference.getUserName()); @@ -71,11 +73,18 @@ public class MainMenu extends JFrame { view.setVisible(true); } }); + today.setText("Today: " + Integer.toString(Getter.getScrobblesToday(Reference.getUserName()))); + today.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + Network.openURL(String.format("https://www.last.fm/user/%s/library?date_preset=LAST_30_DAYS", Reference.getUserName())); + } + }); add(viewLastTrack); add(viewList); add(viewTag); add(getAlbum); add(getArtist); + add(today); } diff --git a/fmframework/src/sarsoo/fmframework/gui/TagMenuView.java b/fmframework/src/sarsoo/fmframework/gui/TagMenuView.java index c8f7c3c..cf67f3e 100644 --- a/fmframework/src/sarsoo/fmframework/gui/TagMenuView.java +++ b/fmframework/src/sarsoo/fmframework/gui/TagMenuView.java @@ -18,11 +18,12 @@ public class TagMenuView extends JFrame { public TagMenuView() { super("View Tags"); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); - setLayout(new GridLayout(3, 2)); + + ArrayList tags = Getter.getUserTags(Reference.getUserName()); + + setLayout(new GridLayout(4, 4)); setSize(300, 300); setResizable(false); - - ArrayList tags = Getter.getUserTags(Reference.getUserName()); int counter; for(counter = 0; counter < tags.size(); counter++) { diff --git a/fmframework/src/sarsoo/fmframework/net/URLBuilder.java b/fmframework/src/sarsoo/fmframework/net/URLBuilder.java index bb18c1f..f20d1ce 100644 --- a/fmframework/src/sarsoo/fmframework/net/URLBuilder.java +++ b/fmframework/src/sarsoo/fmframework/net/URLBuilder.java @@ -2,6 +2,8 @@ package sarsoo.fmframework.net; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; +import java.time.Instant; +import java.time.LocalDateTime; public class URLBuilder { @@ -143,7 +145,42 @@ public class URLBuilder { return null; } - + public static String getTodayScrobbles(String username) { + LocalDateTime now = LocalDateTime.now(); + + String month; + if(now.getMonthValue() < 10) { + month = "0" + now.getMonthValue(); + }else { + month = Integer.toString(now.getMonthValue()); + } + + String day; + if(now.getMonthValue() < 10) { + day = "0" + now.getDayOfMonth(); + }else { + day = Integer.toString(now.getDayOfMonth()); + } + + String date = String.format("%d-%s-%sT07:00:00.00Z", now.getYear(), month, day); + System.out.println(date); +// long midnight = Instant.parse("2018-04-05T07:00:00.00Z").getEpochSecond(); + long midnight = Instant.parse(date).getEpochSecond(); + + String urlString; + try { + urlString = String.format("http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=%s&limit=200&from=%s&api_key=%s", + URLEncoder.encode(username, "UTF-8"), + URLEncoder.encode(Long.toString(midnight), "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 5d45477..7af2fd3 100644 --- a/fmframework/src/sarsoo/fmframework/parser/Parser.java +++ b/fmframework/src/sarsoo/fmframework/parser/Parser.java @@ -91,6 +91,7 @@ public class Parser { } return null; } + public static Artist parseArtist(Document doc) { if (doc.getDocumentElement().getAttribute("status").equals("ok")) { diff --git a/fmframework/src/sarsoo/fmframework/test/NetworkTest.java b/fmframework/src/sarsoo/fmframework/test/NetworkTest.java index 50225c8..d2704b2 100644 --- a/fmframework/src/sarsoo/fmframework/test/NetworkTest.java +++ b/fmframework/src/sarsoo/fmframework/test/NetworkTest.java @@ -3,6 +3,7 @@ package sarsoo.fmframework.test; import static org.junit.jupiter.api.Assertions.*; import java.io.InputStream; +import java.time.Instant; import java.util.ArrayList; import org.junit.jupiter.api.Test; @@ -18,6 +19,7 @@ import sarsoo.fmframework.net.URLBuilder; import sarsoo.fmframework.parser.AlbumParser; import sarsoo.fmframework.parser.Parser; import sarsoo.fmframework.util.FMObjList; +import sarsoo.fmframework.util.Getter; import sarsoo.fmframework.util.Reference; class NetworkTest { @@ -53,18 +55,9 @@ class NetworkTest { @Test void testTag() { - Reference.setUserName("sarsoo"); - String url = URLBuilder.getUserTopTags("sarsoo"); - Document response = Network.getResponse(url); - ArrayList list = Parser.parseUserTags(response); - -// FMObjList list = Parser.parseTagList(response); - - int counter; - for(counter = 0; counter < list.size(); counter++) { - System.out.println(list.get(counter)); - } +// System.out.println(Instant.parse("2018-04-05T07:00:00.00Z").getEpochSecond()); + System.out.println(Getter.getScrobblesToday("sarsoo")); // System.out.println(url); } diff --git a/fmframework/src/sarsoo/fmframework/util/Getter.java b/fmframework/src/sarsoo/fmframework/util/Getter.java index c0ba11c..1196543 100644 --- a/fmframework/src/sarsoo/fmframework/util/Getter.java +++ b/fmframework/src/sarsoo/fmframework/util/Getter.java @@ -5,6 +5,8 @@ import java.util.ArrayList; import javax.swing.JOptionPane; import org.w3c.dom.Document; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; import sarsoo.fmframework.music.Album; import sarsoo.fmframework.music.Artist; @@ -64,6 +66,19 @@ public class Getter { return 0; } + public static int getScrobblesToday(String username) { + String url = URLBuilder.getTodayScrobbles(username); + Document doc = Network.getResponse(url); + if (doc.getDocumentElement().getAttribute("status").equals("ok")) { + Node node = doc.getChildNodes().item(0).getFirstChild(); + NamedNodeMap var = node.getAttributes(); +// System.out.println(var.getNamedItem("total").getNodeValue()); + // if (scrobbles != null) + return Integer.parseInt(var.getNamedItem("total").getNodeValue()); + } + return 0; + } + public static FMObjList getUserTag(String username, String tag) { String url = URLBuilder.getUserPersonalTags(username, tag); Document doc = Network.getResponse(url); @@ -72,7 +87,7 @@ public class Getter { } return null; } - + public static ArrayList getUserTags(String username) { String url = URLBuilder.getUserTopTags(username); Document doc = Network.getResponse(url);