Skip to content

Commit

Permalink
Merge pull request #14 from gusper/master
Browse files Browse the repository at this point in the history
Added support for the beta version of Spotify Desktop to the SpotifyLocalAPIClass
  • Loading branch information
Jonas Dellinger committed Feb 22, 2015
2 parents b019e3d + bd5d775 commit d0211fd
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions SpotifyAPI/SpoitfyLocalAPI/SpotifyAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ public class SpotifyLocalAPIClass
SpotifyMusicHandler mh;
RemoteHandler rh;
SpotifyEventHandler eh;
public SpotifyLocalAPIClass()
static bool betaMode;

public SpotifyLocalAPIClass(bool betaMode = false)
{
rh = RemoteHandler.GetInstance();
mh = new SpotifyMusicHandler();
eh = new SpotifyEventHandler(this, mh);
SpotifyLocalAPIClass.betaMode = betaMode;
}

/// <summary>
Expand All @@ -26,6 +29,7 @@ public Boolean Connect()
{
return rh.Init();
}

/// <summary>
/// Returns the MusicHandler
/// </summary>
Expand All @@ -34,6 +38,7 @@ public SpotifyMusicHandler GetMusicHandler()
{
return mh;
}

/// <summary>
/// Returns the EventHanlder
/// </summary>
Expand All @@ -42,62 +47,81 @@ public SpotifyEventHandler GetEventHandler()
{
return eh;
}

/// <summary>
/// Checks if Spotify is running
/// </summary>
/// <returns>True, if it's running, false if not</returns>
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;
}

/// <summary>
/// Checks if Spotify's WebHelper is running (Needed for API Calls)
/// </summary>
/// <returns>True, if it's running, false if not</returns>
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;
}

/// <summary>
/// Runs Spotify
/// </summary>
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);
}

/// <summary>
/// Runs Spotify's WebHelper
/// </summary>
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);
}

/// <summary>
/// Checks for a valid SpotifyURL (Still not finished)
/// </summary>
/// <param name="url">The Spotify URI starting with "spotify:"</param>
/// <returns>True if the URI is valid, false if not</returns>
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;
}

/// <summary>
/// Updates and Fetches all current information about the current track etc.
/// </summary>
public void Update()
{
if (!SpotifyLocalAPIClass.IsSpotifyWebHelperRunning() || !SpotifyLocalAPIClass.IsSpotifyRunning())
return;

mh.Update(rh.Update());
}
}
Expand Down

0 comments on commit d0211fd

Please sign in to comment.