From 84b28a1aa98ae4524b2066f120d3a4771f4a31f2 Mon Sep 17 00:00:00 2001 From: "Gus Perez (Home)" Date: Sun, 22 Feb 2015 11:31:27 -0800 Subject: [PATCH 1/3] Added beta support to SpotifyLocalAPIClass Adds support for the spotifybeta.exe and spotifybetawebhelper.exe version of the Spotify desktop client for Windows. --- SpotifyAPI/SpoitfyLocalAPI/SpotifyAPI.cs | 40 ++++++++++++++++++++---- SpotifyLocalAPIExample/Form1.cs | 2 +- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/SpotifyAPI/SpoitfyLocalAPI/SpotifyAPI.cs b/SpotifyAPI/SpoitfyLocalAPI/SpotifyAPI.cs index fd297ca0..5f449290 100644 --- a/SpotifyAPI/SpoitfyLocalAPI/SpotifyAPI.cs +++ b/SpotifyAPI/SpoitfyLocalAPI/SpotifyAPI.cs @@ -11,6 +11,8 @@ namespace SpotifyAPI.SpotifyLocalAPI SpotifyMusicHandler mh; RemoteHandler rh; SpotifyEventHandler eh; + static bool betaMode = false; + public SpotifyLocalAPIClass() { rh = RemoteHandler.GetInstance(); @@ -18,6 +20,11 @@ namespace SpotifyAPI.SpotifyLocalAPI eh = new SpotifyEventHandler(this, mh); } + public SpotifyLocalAPIClass(bool betaMode) : this() + { + SpotifyLocalAPIClass.betaMode = betaMode; + } + /// /// Connects with Spotify. Needs to be called before all other SpotifyAPI functions /// @@ -26,6 +33,7 @@ namespace SpotifyAPI.SpotifyLocalAPI { return rh.Init(); } + /// /// Returns the MusicHandler /// @@ -34,6 +42,7 @@ namespace SpotifyAPI.SpotifyLocalAPI { return mh; } + /// /// Returns the EventHanlder /// @@ -42,42 +51,57 @@ namespace SpotifyAPI.SpotifyLocalAPI { return eh; } + /// /// Checks if Spotify is running /// /// True, if it's running, false if not public static Boolean IsSpotifyRunning() { - if (Process.GetProcessesByName("spotify").Length < 1) + var procName = (betaMode) ? "spotifybeta" : "spotify"; + + if (Process.GetProcessesByName(procName).Length < 1) return false; + return true; } + /// /// Checks if Spotify's WebHelper is running (Needed for API Calls) /// /// True, if it's running, false if not public static Boolean IsSpotifyWebHelperRunning() { - if (Process.GetProcessesByName("SpotifyWebHelper").Length < 1) + var procName = (betaMode) ? "spotifybetawebhelper" : "spotifywebhelper"; + + if (Process.GetProcessesByName(procName).Length < 1) return false; + return true; } + /// /// Runs Spotify /// public void RunSpotify() { - if(!IsSpotifyRunning()) - Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Spotify\\spotify.exe"); + var pathToExe = (betaMode) ? @"\spotifybeta\spotifybeta.exe" : @"\spotify\spotify.exe"; + + if (!IsSpotifyRunning()) + Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + pathToExe); } + /// /// Runs Spotify's WebHelper /// public void RunSpotifyWebHelper() { + var pathToExe = (betaMode) ? @"\spotifybeta\spotifybetawebhelper.exe" : @"\spotify\data\spotifywebhelper.exe"; + if (!IsSpotifyWebHelperRunning()) - Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Spotify\\Data\\SpotifyWebHelper.exe"); + Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + pathToExe); } + /// /// Checks for a valid SpotifyURL (Still not finished) /// @@ -85,12 +109,15 @@ namespace SpotifyAPI.SpotifyLocalAPI /// True if the URI is valid, false if not public static Boolean IsValidSpotifyURI(String uri) { - String[] types = new String[] { "track","album","local","artist"}; + String[] types = new String[] { "track", "album", "local", "artist" }; String[] split = uri.Split(':'); + if (split.Length < 3) return false; + return split[0] == "spotify" && Array.IndexOf(types, split[1]) > -1 && split[2].Length == 22; } + /// /// Updates and Fetches all current information about the current track etc. /// @@ -98,6 +125,7 @@ namespace SpotifyAPI.SpotifyLocalAPI { if (!SpotifyLocalAPIClass.IsSpotifyWebHelperRunning() || !SpotifyLocalAPIClass.IsSpotifyRunning()) return; + mh.Update(rh.Update()); } } diff --git a/SpotifyLocalAPIExample/Form1.cs b/SpotifyLocalAPIExample/Form1.cs index 86acb256..652c9c00 100644 --- a/SpotifyLocalAPIExample/Form1.cs +++ b/SpotifyLocalAPIExample/Form1.cs @@ -23,7 +23,7 @@ namespace SpotifyAPI_Example public Form1() { InitializeComponent(); - spotify = new SpotifyLocalAPIClass(); + spotify = new SpotifyLocalAPIClass(true); if (!SpotifyLocalAPIClass.IsSpotifyRunning()) { spotify.RunSpotify(); From 510c33489d945daabe14cc324bab7c3281c38400 Mon Sep 17 00:00:00 2001 From: "Gus Perez (Home)" Date: Sun, 22 Feb 2015 11:34:56 -0800 Subject: [PATCH 2/3] Undo my change to the local sample code so it doesn't default to using the beta version of Spotify. --- SpotifyLocalAPIExample/Form1.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpotifyLocalAPIExample/Form1.cs b/SpotifyLocalAPIExample/Form1.cs index 652c9c00..86acb256 100644 --- a/SpotifyLocalAPIExample/Form1.cs +++ b/SpotifyLocalAPIExample/Form1.cs @@ -23,7 +23,7 @@ namespace SpotifyAPI_Example public Form1() { InitializeComponent(); - spotify = new SpotifyLocalAPIClass(true); + spotify = new SpotifyLocalAPIClass(); if (!SpotifyLocalAPIClass.IsSpotifyRunning()) { spotify.RunSpotify(); From bd5d77528b1729a07da49b8812aa1a425c06c02a Mon Sep 17 00:00:00 2001 From: "Gus Perez (Home)" Date: Sun, 22 Feb 2015 11:54:08 -0800 Subject: [PATCH 3/3] Updated constructor to use a default parameter instead of going the overload route. --- SpotifyAPI/SpoitfyLocalAPI/SpotifyAPI.cs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/SpotifyAPI/SpoitfyLocalAPI/SpotifyAPI.cs b/SpotifyAPI/SpoitfyLocalAPI/SpotifyAPI.cs index 5f449290..36bfdabc 100644 --- a/SpotifyAPI/SpoitfyLocalAPI/SpotifyAPI.cs +++ b/SpotifyAPI/SpoitfyLocalAPI/SpotifyAPI.cs @@ -11,17 +11,13 @@ namespace SpotifyAPI.SpotifyLocalAPI SpotifyMusicHandler mh; RemoteHandler rh; SpotifyEventHandler eh; - static bool betaMode = false; + static bool betaMode; - public SpotifyLocalAPIClass() + public SpotifyLocalAPIClass(bool betaMode = false) { rh = RemoteHandler.GetInstance(); mh = new SpotifyMusicHandler(); eh = new SpotifyEventHandler(this, mh); - } - - public SpotifyLocalAPIClass(bool betaMode) : this() - { SpotifyLocalAPIClass.betaMode = betaMode; }