added authing from ui

This commit is contained in:
aj 2019-05-13 14:55:00 +01:00
parent 23d2b1cffa
commit d035193976
5 changed files with 122 additions and 38 deletions

View File

@ -20,6 +20,7 @@ import com.mashape.unirest.request.HttpRequestWithBody;
import sarsoo.fmframework.log.Logger; import sarsoo.fmframework.log.Logger;
import sarsoo.fmframework.log.entry.ErrorEntry; import sarsoo.fmframework.log.entry.ErrorEntry;
import sarsoo.fmframework.log.entry.LogEntry;
import sarsoo.fmframework.music.Scrobble; import sarsoo.fmframework.music.Scrobble;
public class FmAuthNetwork extends FmUserNetwork { public class FmAuthNetwork extends FmUserNetwork {
@ -33,6 +34,8 @@ public class FmAuthNetwork extends FmUserNetwork {
public void scrobble(Scrobble scrobble, String sk) { public void scrobble(Scrobble scrobble, String sk) {
Logger.getLog().log(new LogEntry("scrobble").addArg(scrobble.toString()));
HashMap<String, String> params = new HashMap<String, String>(); HashMap<String, String> params = new HashMap<String, String>();
params.put("artist", scrobble.getTrack().getArtist().getName()); params.put("artist", scrobble.getTrack().getArtist().getName());
@ -47,11 +50,12 @@ public class FmAuthNetwork extends FmUserNetwork {
JSONObject obj = makeAuthPostRequest("track.scrobble", params); JSONObject obj = makeAuthPostRequest("track.scrobble", params);
System.out.println(obj.toString());
} }
public String getToken() { public String getToken() {
Logger.getLog().log(new LogEntry("getToken"));
JSONObject obj = makeAuthGetRequest("auth.gettoken"); JSONObject obj = makeAuthGetRequest("auth.gettoken");
return obj.getString("token"); return obj.getString("token");
@ -59,6 +63,8 @@ public class FmAuthNetwork extends FmUserNetwork {
public String getSession(String token) { public String getSession(String token) {
Logger.getLog().log(new LogEntry("getSession"));
HashMap<String, String> params = new HashMap<String, String>(); HashMap<String, String> params = new HashMap<String, String>();
params.put("token", token); params.put("token", token);

View File

@ -10,13 +10,17 @@ import java.util.Locale;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler; import javafx.event.EventHandler;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.stage.FileChooser; import javafx.stage.FileChooser;
import sarsoo.fmframework.config.Config; import sarsoo.fmframework.config.Config;
import sarsoo.fmframework.config.ConfigVariable;
import sarsoo.fmframework.config.VariableEvent; import sarsoo.fmframework.config.VariableEvent;
import sarsoo.fmframework.config.VariableListener; import sarsoo.fmframework.config.VariableListener;
import sarsoo.fmframework.file.ListPersister; import sarsoo.fmframework.file.ListPersister;
import sarsoo.fmframework.fm.FmAuthNetwork;
import sarsoo.fmframework.fm.FmNetwork;
import sarsoo.fmframework.fx.TextAreaConsole; import sarsoo.fmframework.fx.TextAreaConsole;
import sarsoo.fmframework.fx.service.GetLastTrackService; import sarsoo.fmframework.fx.service.GetLastTrackService;
import sarsoo.fmframework.fx.service.GetScrobbleCountService; import sarsoo.fmframework.fx.service.GetScrobbleCountService;
@ -30,6 +34,7 @@ import sarsoo.fmframework.fx.tab.FMObjListEditTab;
import sarsoo.fmframework.fx.tab.GenrePieChartTab; import sarsoo.fmframework.fx.tab.GenrePieChartTab;
import sarsoo.fmframework.fx.tab.ScrobbleChartTab; import sarsoo.fmframework.fx.tab.ScrobbleChartTab;
import sarsoo.fmframework.fx.tab.TrackTab; import sarsoo.fmframework.fx.tab.TrackTab;
import sarsoo.fmframework.fx.tab.WebViewTab;
import sarsoo.fmframework.log.Log; import sarsoo.fmframework.log.Log;
import sarsoo.fmframework.log.Logger; import sarsoo.fmframework.log.Logger;
import sarsoo.fmframework.fx.FmFramework; import sarsoo.fmframework.fx.FmFramework;
@ -53,14 +58,14 @@ public class RootController {
Config config = FmFramework.getSessionConfig(); Config config = FmFramework.getSessionConfig();
if(config.getVariable("api_key") == null) { if (config.getVariable("api_key") == null) {
while(config.getVariable("api_key") == null) { while (config.getVariable("api_key") == null) {
setApiKey(); setApiKey();
} }
} }
if(config.getVariable("username") == null) { if (config.getVariable("username") == null) {
while(config.getVariable("username") == null) { while (config.getVariable("username") == null) {
changeUsername(); changeUsername();
} }
} }
@ -192,10 +197,54 @@ public class RootController {
} }
@FXML @FXML
protected void handleChangeUsername(ActionEvent event) throws IOException { protected void handleChangeUsername(ActionEvent event) {
changeUsername(); changeUsername();
} }
@FXML
protected void handleAuth(ActionEvent event) {
try {
Config config = FmFramework.getSessionConfig();
if (config.getVariable("api_secret") != null) {
FmAuthNetwork net = new FmAuthNetwork(config.getValue("api_key"), config.getValue("api_secret"),
config.getValue("username"));
String token = net.getToken();
String url = String.format("http://www.last.fm/api/auth/?api_key=%s&token=%s",
config.getValue("api_key"), token);
Tab tab = new WebViewTab(url);
tab.setOnClosed(new EventHandler<Event>() {
@Override
public void handle(Event arg0) {
completeAuth(net, token);
}
});
addTab(tab);
}
} catch (IOException e) {
e.printStackTrace();
}
}
protected void completeAuth(FmAuthNetwork net, String token) {
String sk = net.getSession(token);
if (sk != null) {
FmFramework.getSessionConfig().addVariable(new ConfigVariable("session_key", sk));
}
}
public void changeUsername() { public void changeUsername() {
Service<Void> service = new Service<Void>() { Service<Void> service = new Service<Void>() {
@Override @Override

View File

@ -0,0 +1,28 @@
package sarsoo.fmframework.fx.tab;
import java.io.IOException;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
public class WebViewTab extends Tab {
public WebViewTab(String path) throws IOException {
setText("web view");
StackPane pane = new StackPane();
WebView web = new WebView();
pane.getChildren().add(web);
WebEngine webEngine = web.getEngine();
webEngine.load(path);
setContent(pane);
}
}

View File

@ -34,6 +34,7 @@
<Menu fx:id="menuUtil" mnemonicParsing="false" text="util"> <Menu fx:id="menuUtil" mnemonicParsing="false" text="util">
<items> <items>
<MenuItem fx:id="menuItemScrobble" mnemonicParsing="false" onAction="#handleScrobble" text="scrobble" /> <MenuItem fx:id="menuItemScrobble" mnemonicParsing="false" onAction="#handleScrobble" text="scrobble" />
<MenuItem fx:id="menuItemAuth" mnemonicParsing="false" onAction="#handleAuth" text="authenticate" />
<MenuItem fx:id="menuItemOpenConsole" mnemonicParsing="false" onAction="#handleOpenConsole" text="open console" /> <MenuItem fx:id="menuItemOpenConsole" mnemonicParsing="false" onAction="#handleOpenConsole" text="open console" />
<MenuItem mnemonicParsing="false" onAction="#handleChangeUsername" text="set username" /> <MenuItem mnemonicParsing="false" onAction="#handleChangeUsername" text="set username" />
<MenuItem fx:id="menuItemPrintConfig" mnemonicParsing="false" onAction="#handlePrintConfig" text="print config" /> <MenuItem fx:id="menuItemPrintConfig" mnemonicParsing="false" onAction="#handlePrintConfig" text="print config" />

View File

@ -17,29 +17,29 @@ public class FmAuthNetworkTest {
@Test @Test
public void test() { public void test() {
FmAuthNetwork net = new FmAuthNetwork(Key.getKey(), Key.getSecret(), "sarsoo"); // FmAuthNetwork net = new FmAuthNetwork(Key.getKey(), Key.getSecret(), "sarsoo");
// HashMap<String, String> hash = new HashMap<>();
// //
// hash.put("zoo", "ash"); //// HashMap<String, String> hash = new HashMap<>();
// hash.put("boo", "ash"); ////
// hash.put("ash", "boo"); //// hash.put("zoo", "ash");
//// hash.put("boo", "ash");
//// hash.put("ash", "boo");
////
//// net.getApiSignature(hash);
// //
// net.getApiSignature(hash); // String token = net.getToken();
//
String token = net.getToken(); // System.out.println(token);
//
System.out.println(token); //// new Scanner(System.in).nextLine();
//
// new Scanner(System.in).nextLine(); // String key = net.getSession(token);
//
String key = net.getSession(token); // System.out.println(key);
//
System.out.println(key); // Scrobble scrobble = new Scrobble(1557750712, new TrackBuilder("i", new ArtistBuilder("kendrick lamar").build()).build());
//
Scrobble scrobble = new Scrobble(1557750712, new TrackBuilder("i", new ArtistBuilder("kendrick lamar").build()).build()); // net.scrobble(scrobble, key);
net.scrobble(scrobble, key);
assertTrue(true); assertTrue(true);
} }