changed list view layout, fixed bad requests changed request property

and url encoding
This commit is contained in:
aj 2018-04-03 15:24:24 -07:00
parent d6c68d014d
commit 942ddb3c59
6 changed files with 160 additions and 57 deletions

View File

@ -9,17 +9,17 @@ import sarsoo.fmframework.util.Reference;
public class Driver { public class Driver {
public static void main(String[] args) { public static void main(String[] args) {
// String username = JOptionPane.showInputDialog(null, "Enter User-Name"); String username = JOptionPane.showInputDialog(null, "Enter User-Name");
// System.out.println(username); // System.out.println(username);
// if (username != null) { if (username != null) {
Reference.setUserName("sarsoo"); // Reference.setUserName("sarsoo");
// Reference.setUserName(username); Reference.setUserName(username);
// System.out.println(Getter.getScrobbles(Reference.getUserName())); // System.out.println(Getter.getScrobbles(Reference.getUserName()));
MainMenu main = new MainMenu(); MainMenu main = new MainMenu();
main.setVisible(true); main.setVisible(true);
// } }
} }
} }

View File

@ -4,6 +4,8 @@ import java.awt.Font;
import java.awt.GridLayout; import java.awt.GridLayout;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.util.Locale; import java.util.Locale;
@ -35,21 +37,44 @@ public class FMObjListView extends JFrame {
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US); NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
Font font = new Font("Arial", Font.PLAIN, 20); Font font = new Font("Arial", Font.PLAIN, 20);
Font header = new Font("Arial", Font.BOLD, 16);
JPanel headerLabels = new JPanel(); JPanel headerLabels = new JPanel();
headerLabels.setLayout(new GridLayout(1, 5)); // headerLabels.setFont(header);
headerLabels.add(new JLabel("Name")); headerLabels.setLayout(new GridLayout(1, 4));
headerLabels.add(new JLabel("User Scrobbles"));
headerLabels.add(new JLabel("Total Scrobbles"));
headerLabels.add(new JLabel("")); JLabel headerName = new JLabel("name");
headerName.setHorizontalAlignment(SwingConstants.CENTER);
headerName.setFont(header);
JLabel headerUser = new JLabel("user");
headerUser.setHorizontalAlignment(SwingConstants.CENTER);
headerUser.setFont(header);
JLabel headerTotal = new JLabel("total");
headerTotal.setHorizontalAlignment(SwingConstants.CENTER);
headerTotal.setFont(header);
headerLabels.add(headerName);
headerLabels.add(headerUser);
headerLabels.add(headerTotal);
headerLabels.add(new JLabel("")); headerLabels.add(new JLabel(""));
// headerLabels.add(new JLabel(""));
add(headerLabels); add(headerLabels);
int counter; int counter;
for (counter = 0; counter < objects.size(); counter++) { for (counter = 0; counter < objects.size(); counter++) {
FMObj fmObj = objects.get(counter); FMObj fmObj = objects.get(counter);
JLabel name = new JLabel(fmObj.getName()); JLabel artistName = new JLabel(fmObj.getName());
artistName.setHorizontalAlignment(SwingConstants.CENTER);
artistName.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
fmObj.view();
}
});
int playCountString = fmObj.getUserPlayCount(); int playCountString = fmObj.getUserPlayCount();
@ -58,26 +83,29 @@ public class FMObjListView extends JFrame {
userPlays = new JLabel("0"); userPlays = new JLabel("0");
else else
userPlays = new JLabel(Integer.toString(fmObj.getUserPlayCount())); userPlays = new JLabel(Integer.toString(fmObj.getUserPlayCount()));
userPlays.setHorizontalAlignment(SwingConstants.CENTER);
JLabel plays = new JLabel(numberFormat.format(fmObj.getPlayCount())); JLabel plays = new JLabel(numberFormat.format(fmObj.getPlayCount()));
plays.setHorizontalAlignment(SwingConstants.CENTER);
JButton openExternal = new JButton("Open Online"); JButton openExternal = new JButton("Open Online");
openExternal.addActionListener(new ActionListener() { openExternal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
Network.openURL(fmObj.getUrl()); Network.openURL(fmObj.getUrl());
} }
}); });
JButton openInternal = new JButton("Open " + fmObj.getClass().getSimpleName()); // JButton openInternal = new JButton("Open " + fmObj.getClass().getSimpleName());
openInternal.addActionListener(new ActionListener() { // openInternal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { // public void actionPerformed(ActionEvent arg0) {
fmObj.view(); // fmObj.view();
} // }
}); // });
JPanel panel = new JPanel(); JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 5)); panel.setLayout(new GridLayout(1, 4));
panel.add(name); panel.add(artistName);
panel.add(userPlays); panel.add(userPlays);
panel.add(plays); panel.add(plays);
panel.add(openInternal); // panel.add(openInternal);
panel.add(openExternal); panel.add(openExternal);
add(panel); add(panel);

View File

@ -34,7 +34,7 @@ public class RefListsView extends JFrame {
// add(view); // add(view);
// } // }
JButton viewTDE = new JButton("View TDE"); JButton viewTDE = new JButton("TDE");
viewTDE.addActionListener(new ActionListener() { viewTDE.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
Reference.getTDE().view(); Reference.getTDE().view();
@ -42,7 +42,7 @@ public class RefListsView extends JFrame {
}); });
add(viewTDE); add(viewTDE);
JButton viewBPHQ = new JButton("View BPHQ"); JButton viewBPHQ = new JButton("BPHQ");
viewBPHQ.addActionListener(new ActionListener() { viewBPHQ.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
Reference.getBPHQ().view(); Reference.getBPHQ().view();
@ -50,33 +50,41 @@ public class RefListsView extends JFrame {
}); });
add(viewBPHQ); add(viewBPHQ);
JButton viewDre = new JButton("View Dre"); JButton viewDre = new JButton("Dre");
viewDre.addActionListener(new ActionListener() { viewDre.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
Reference.getDre().view(); Reference.getDre().view();
} }
}); });
add(viewDre); add(viewDre);
JButton viewWu = new JButton("View Wu"); JButton viewWu = new JButton("Wu");
viewWu.addActionListener(new ActionListener() { viewWu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
Reference.getWu().view(); Reference.getWu().view();
} }
}); });
add(viewWu); add(viewWu);
JButton viewHopeless = new JButton("View Hopeless"); JButton viewHopeless = new JButton("Hopeless");
viewHopeless.addActionListener(new ActionListener() { viewHopeless.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
Reference.getHopeless().view(); Reference.getHopeless().view();
} }
}); });
add(viewHopeless); add(viewHopeless);
JButton viewSaturation = new JButton("View Saturation"); JButton viewSaturation = new JButton("Saturation");
viewSaturation.addActionListener(new ActionListener() { viewSaturation.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
Reference.getSaturation().view(); Reference.getSaturation().view();
} }
}); });
add(viewSaturation); add(viewSaturation);
JButton viewEmoTrio = new JButton("Emo Trio");
viewEmoTrio.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Reference.getEmoTrio().view();
}
});
add(viewEmoTrio);
} }
} }

View File

@ -51,7 +51,7 @@ public class TrackView extends JFrame {
setLayout(new GridLayout(6, 1)); setLayout(new GridLayout(6, 1));
} }
// setSize(300, 300); // setSize(300, 300);
setResizable(false); setResizable(false);
// info.setLayout(new GridLayout(6,1)); // info.setLayout(new GridLayout(6,1));
// nameInfo.setLayout(new GridLayout(3,1)); // nameInfo.setLayout(new GridLayout(3,1));
@ -100,7 +100,7 @@ public class TrackView extends JFrame {
playCount.setText(numberFormat.format(track.getPlayCount()) + " Total Scrobbles"); playCount.setText(numberFormat.format(track.getPlayCount()) + " Total Scrobbles");
playCount.setHorizontalAlignment(SwingConstants.CENTER); playCount.setHorizontalAlignment(SwingConstants.CENTER);
userPlayCount.setText(numberFormat.format(track.getUserPlayCount()) userPlayCount.setText(numberFormat.format(track.getUserPlayCount())
+ String.format(" Scrobbles (%.2f%%)", Maths.getPercentListening(track, Reference.getUserName()))); + String.format(" Scrobbles (%.3f%%)", Maths.getPercentListening(track, Reference.getUserName())));
userPlayCount.setHorizontalAlignment(SwingConstants.CENTER); userPlayCount.setHorizontalAlignment(SwingConstants.CENTER);
userPlayCount.setFont(sub); userPlayCount.setFont(sub);
@ -115,6 +115,19 @@ public class TrackView extends JFrame {
} }
}); });
if (track.getUserPlayCount() > 0) {
userPlayCount.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String namePlus = track.getName().replaceAll(" ", "+");
String artistPlus = track.getArtist().getName().replaceAll(" ", "+");
String url = String.format("https://www.last.fm/user/%s/library/music/%s/_/%s",
Reference.getUserName(), artistPlus, namePlus);
Network.openURL(url);
}
});
}
artist.addMouseListener(new MouseAdapter() { artist.addMouseListener(new MouseAdapter() {
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {

View File

@ -3,10 +3,12 @@ package sarsoo.fmframework.net;
import java.awt.Desktop; import java.awt.Desktop;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
@ -24,7 +26,7 @@ public class Network {
URL url = new URL(urlString); URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET"); conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "text/xml"); conn.setRequestProperty("Accept", "application/xml");
conn.setRequestProperty("User-Agent", "fmframework/1.0"); conn.setRequestProperty("User-Agent", "fmframework/1.0");
if (conn.getResponseCode() != 200) { if (conn.getResponseCode() != 200) {
@ -64,49 +66,91 @@ public class Network {
} }
public static String getArtistInfoUrl(String artist, String username) { public static String getArtistInfoUrl(String artist, String username) {
String urlString = String.format( String urlString;
"http://ws.audioscrobbler.com/2.0/?method=artist.getInfo&artist=%s&autocorrect=0&username=%s&api_key=%s", try {
artist, username, Key.getKey()); urlString = String.format(
return urlString; "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) { public static String getArtistInfoMbidUrl(String mbid, String username) {
String urlString = String.format( String urlString;
"http://ws.audioscrobbler.com/2.0/?method=artist.getInfo&mbid=%s&autocorrect=0&username=%s&api_key=%s", try {
mbid, username, Key.getKey()); urlString = String.format(
return urlString; "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) { public static String getAlbumInfoUrl(String album, String artist, String username) {
String urlString = String.format( String urlString;
"http://ws.audioscrobbler.com/2.0/?method=album.getInfo&album=%s&artist=%s&autocorrect=0&username=%s&api_key=%s", try {
album, artist, username, Key.getKey()); urlString = String.format(
return urlString; "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) { public static String getTrackInfoUrl(String name, String artist, String username) {
String urlString = String.format( String urlString;
"http://ws.audioscrobbler.com/2.0/?method=track.getInfo&track=%s&artist=%s&autocorrect=0&username=%s&api_key=%s", try {
name, artist, username, Key.getKey()); urlString = String.format(
return urlString; "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) { public static String getLastTrackUrl(String username) {
String urlString = String.format( String urlString;
"http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=%s&limit=1&api_key=%s", try {
username, Key.getKey()); urlString = String.format(
System.out.println(urlString); "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=%s&limit=1&api_key=%s",
return urlString; 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) { public static String getUserInfoUrl(String username) {
String urlString = String.format( String urlString;
"http://ws.audioscrobbler.com/2.0/?method=user.getinfo&user=%s&api_key=%s", try {
username, Key.getKey()); urlString = String.format(
System.out.println(urlString); "http://ws.audioscrobbler.com/2.0/?method=user.getinfo&user=%s&api_key=%s",
return urlString; 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) { public static String getLyricsUrl(String trackName, String artistName) {

View File

@ -123,4 +123,14 @@ public class Reference {
return wu; return wu;
} }
public static FMObjList getEmoTrio() {
FMObjList emoTrio = new FMObjList("Classic Emo Trio");
emoTrio.add(Artist.getArtist("My Chemical Romance", Reference.getUserName()));
emoTrio.add(Artist.getArtist("Fall Out Boy", Reference.getUserName()));
emoTrio.add(Artist.getArtist("Panic! at the Disco", Reference.getUserName()));
return emoTrio;
}
} }