diff --git a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/LastFMToSpotify.java b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/LastFMToSpotify.java index c32d43e..9149a5d 100644 --- a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/LastFMToSpotify.java +++ b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/LastFMToSpotify.java @@ -4,6 +4,7 @@ package de.b00tload.tools.lastfmtospotifyplaylist; import com.neovisionaries.i18n.CountryCode; import de.b00tload.tools.lastfmtospotifyplaylist.arguments.ArgumentHandler; import de.b00tload.tools.lastfmtospotifyplaylist.arguments.Arguments; +import de.b00tload.tools.lastfmtospotifyplaylist.util.BrowserHelper; import de.b00tload.tools.lastfmtospotifyplaylist.util.PeriodHelper; import de.b00tload.tools.lastfmtospotifyplaylist.util.TokenHelper; @@ -100,7 +101,12 @@ public class LastFMToSpotify { } }); logLn("Waiting for Spotify authorization.", 1); - //TODO: Open auth page in Browser + + String authPage = "https://accounts.spotify.com/authorize?client_id=" + + configuration.get("spotify.clientid") + + "&response_type=code&state=73775e18-b0bc-4031-a379-bce99a0e3e6c&redirect_uri=http%3A%2F%2Flocalhost%3A9876%2Fcallback%2Fspotify%2F&scope=user-read-private%20playlist-modify-private%20playlist-modify-public%20ugc-image-upload%20playlist-read-private%20playlist-read-collaborative"; + BrowserHelper.openInBrowser(authPage); + while (waiting.get()); } } diff --git a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/BrowserHelper.java b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/BrowserHelper.java new file mode 100644 index 0000000..1eedfd5 --- /dev/null +++ b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/BrowserHelper.java @@ -0,0 +1,42 @@ +package de.b00tload.tools.lastfmtospotifyplaylist.util; + +public class BrowserHelper { + + /** + * Opens a url in the systems default browser + * @param url + */ + public static void openInBrowser(String url) { + + String os = System.getProperty("os.name").toLowerCase(); + ProcessBuilder builder; + + if (os.indexOf("win") >= 0) { + // Windows + builder = new ProcessBuilder("rundll32.exe","url.dll,FileProtocolHandler", url); + } else if (os.indexOf("mac") >= 0) { + // Mac + builder = new ProcessBuilder("open", url); + } else if (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0) { + // Linux + os = "linux"; + builder = new ProcessBuilder("xdg-open", url); + } else { + Logger.logLn("Please open the following link:\n"+url, 1); + builder = null; + } + + try { + if (builder != null) { + Process exec = builder.start(); + if (os.equals("linux") && exec.exitValue() == 3) { + // on Linux in case of missing browser + Logger.logLn("Please open the following link:\n"+url, 1); + } + } + } catch (Exception e) { + Logger.logLn(e.getMessage(), 3); + } + + } +}