using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace SpotifyAPI.SpotifyLocalAPI
{
public class SpotifyLocalAPIClass
{
SpotifyMusicHandler mh;
RemoteHandler rh;
SpotifyEventHandler eh;
static bool betaMode;
public SpotifyLocalAPIClass(bool betaMode = false)
{
rh = RemoteHandler.GetInstance();
mh = new SpotifyMusicHandler();
eh = new SpotifyEventHandler(this, mh);
SpotifyLocalAPIClass.betaMode = betaMode;
}
///
/// Connects with Spotify. Needs to be called before all other SpotifyAPI functions
///
/// Returns true, if it was successful, false if not
public Boolean Connect()
{
return rh.Init();
}
///
/// Returns the MusicHandler
///
/// Returns the MusicHandler
public SpotifyMusicHandler GetMusicHandler()
{
return mh;
}
///
/// Returns the EventHanlder
///
/// Returns the EventHanlder
public SpotifyEventHandler GetEventHandler()
{
return eh;
}
///
/// Checks if Spotify is running
///
/// True, if it's running, false if not
public static Boolean IsSpotifyRunning()
{
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()
{
var procName = (betaMode) ? "spotifybetawebhelper" : "spotifywebhelper";
if (Process.GetProcessesByName(procName).Length < 1)
return false;
return true;
}
///
/// Runs Spotify
///
public void RunSpotify()
{
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) + pathToExe);
}
///
/// Checks for a valid SpotifyURL (Still not finished)
///
/// The Spotify URI starting with "spotify:"
/// True if the URI is valid, false if not
public static Boolean IsValidSpotifyURI(String uri)
{
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.
///
public void Update()
{
if (!SpotifyLocalAPIClass.IsSpotifyWebHelperRunning() || !SpotifyLocalAPIClass.IsSpotifyRunning())
return;
mh.Update(rh.Update());
}
}
}