Added beta support to SpotifyLocalAPIClass

Adds support for the spotifybeta.exe and spotifybetawebhelper.exe
version of the Spotify desktop client for Windows.
This commit is contained in:
Gus Perez (Home) 2015-02-22 11:31:27 -08:00
parent b019e3d0c2
commit 84b28a1aa9
2 changed files with 35 additions and 7 deletions

View File

@ -11,6 +11,8 @@ namespace SpotifyAPI.SpotifyLocalAPI
SpotifyMusicHandler mh; SpotifyMusicHandler mh;
RemoteHandler rh; RemoteHandler rh;
SpotifyEventHandler eh; SpotifyEventHandler eh;
static bool betaMode = false;
public SpotifyLocalAPIClass() public SpotifyLocalAPIClass()
{ {
rh = RemoteHandler.GetInstance(); rh = RemoteHandler.GetInstance();
@ -18,6 +20,11 @@ namespace SpotifyAPI.SpotifyLocalAPI
eh = new SpotifyEventHandler(this, mh); eh = new SpotifyEventHandler(this, mh);
} }
public SpotifyLocalAPIClass(bool betaMode) : this()
{
SpotifyLocalAPIClass.betaMode = betaMode;
}
/// <summary> /// <summary>
/// Connects with Spotify. Needs to be called before all other SpotifyAPI functions /// Connects with Spotify. Needs to be called before all other SpotifyAPI functions
/// </summary> /// </summary>
@ -26,6 +33,7 @@ namespace SpotifyAPI.SpotifyLocalAPI
{ {
return rh.Init(); return rh.Init();
} }
/// <summary> /// <summary>
/// Returns the MusicHandler /// Returns the MusicHandler
/// </summary> /// </summary>
@ -34,6 +42,7 @@ namespace SpotifyAPI.SpotifyLocalAPI
{ {
return mh; return mh;
} }
/// <summary> /// <summary>
/// Returns the EventHanlder /// Returns the EventHanlder
/// </summary> /// </summary>
@ -42,42 +51,57 @@ namespace SpotifyAPI.SpotifyLocalAPI
{ {
return eh; return eh;
} }
/// <summary> /// <summary>
/// Checks if Spotify is running /// Checks if Spotify is running
/// </summary> /// </summary>
/// <returns>True, if it's running, false if not</returns> /// <returns>True, if it's running, false if not</returns>
public static Boolean IsSpotifyRunning() public static Boolean IsSpotifyRunning()
{ {
if (Process.GetProcessesByName("spotify").Length < 1) var procName = (betaMode) ? "spotifybeta" : "spotify";
if (Process.GetProcessesByName(procName).Length < 1)
return false; return false;
return true; return true;
} }
/// <summary> /// <summary>
/// Checks if Spotify's WebHelper is running (Needed for API Calls) /// Checks if Spotify's WebHelper is running (Needed for API Calls)
/// </summary> /// </summary>
/// <returns>True, if it's running, false if not</returns> /// <returns>True, if it's running, false if not</returns>
public static Boolean IsSpotifyWebHelperRunning() public static Boolean IsSpotifyWebHelperRunning()
{ {
if (Process.GetProcessesByName("SpotifyWebHelper").Length < 1) var procName = (betaMode) ? "spotifybetawebhelper" : "spotifywebhelper";
if (Process.GetProcessesByName(procName).Length < 1)
return false; return false;
return true; return true;
} }
/// <summary> /// <summary>
/// Runs Spotify /// Runs Spotify
/// </summary> /// </summary>
public void RunSpotify() public void RunSpotify()
{ {
var pathToExe = (betaMode) ? @"\spotifybeta\spotifybeta.exe" : @"\spotify\spotify.exe";
if (!IsSpotifyRunning()) if (!IsSpotifyRunning())
Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Spotify\\spotify.exe"); Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + pathToExe);
} }
/// <summary> /// <summary>
/// Runs Spotify's WebHelper /// Runs Spotify's WebHelper
/// </summary> /// </summary>
public void RunSpotifyWebHelper() public void RunSpotifyWebHelper()
{ {
var pathToExe = (betaMode) ? @"\spotifybeta\spotifybetawebhelper.exe" : @"\spotify\data\spotifywebhelper.exe";
if (!IsSpotifyWebHelperRunning()) if (!IsSpotifyWebHelperRunning())
Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Spotify\\Data\\SpotifyWebHelper.exe"); Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + pathToExe);
} }
/// <summary> /// <summary>
/// Checks for a valid SpotifyURL (Still not finished) /// Checks for a valid SpotifyURL (Still not finished)
/// </summary> /// </summary>
@ -87,10 +111,13 @@ namespace SpotifyAPI.SpotifyLocalAPI
{ {
String[] types = new String[] { "track", "album", "local", "artist" }; String[] types = new String[] { "track", "album", "local", "artist" };
String[] split = uri.Split(':'); String[] split = uri.Split(':');
if (split.Length < 3) if (split.Length < 3)
return false; return false;
return split[0] == "spotify" && Array.IndexOf(types, split[1]) > -1 && split[2].Length == 22; return split[0] == "spotify" && Array.IndexOf(types, split[1]) > -1 && split[2].Length == 22;
} }
/// <summary> /// <summary>
/// Updates and Fetches all current information about the current track etc. /// Updates and Fetches all current information about the current track etc.
/// </summary> /// </summary>
@ -98,6 +125,7 @@ namespace SpotifyAPI.SpotifyLocalAPI
{ {
if (!SpotifyLocalAPIClass.IsSpotifyWebHelperRunning() || !SpotifyLocalAPIClass.IsSpotifyRunning()) if (!SpotifyLocalAPIClass.IsSpotifyWebHelperRunning() || !SpotifyLocalAPIClass.IsSpotifyRunning())
return; return;
mh.Update(rh.Update()); mh.Update(rh.Update());
} }
} }

View File

@ -23,7 +23,7 @@ namespace SpotifyAPI_Example
public Form1() public Form1()
{ {
InitializeComponent(); InitializeComponent();
spotify = new SpotifyLocalAPIClass(); spotify = new SpotifyLocalAPIClass(true);
if (!SpotifyLocalAPIClass.IsSpotifyRunning()) if (!SpotifyLocalAPIClass.IsSpotifyRunning())
{ {
spotify.RunSpotify(); spotify.RunSpotify();