using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace SpotifyAPIv1
{
public class SpotifyAPI
{
SpotifyMusicHandler mh;
RemoteHandler rh;
SpotifyEventHandler eh;
public SpotifyAPI()
{
rh = RemoteHandler.GetInstance();
mh = new SpotifyMusicHandler();
eh = new SpotifyEventHandler(this, mh);
}
///
/// Connects with Spotify. Needs to be called before all other SpotifyAPI functions
///
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()
{
if (Process.GetProcessesByName("spotify").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)
return false;
return true;
}
///
/// Runs Spotify
///
public void RunSpotify()
{
if(!IsSpotifyRunning())
Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Spotify\\spotify.exe");
}
///
/// Runs Spotify's WebHelper
///
public void RunSpotifyWebHelper()
{
if (!IsSpotifyWebHelperRunning())
Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Spotify\\Data\\SpotifyWebHelper.exe");
}
///
/// 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 (!SpotifyAPI.IsSpotifyWebHelperRunning() || !SpotifyAPI.IsSpotifyRunning())
return;
mh.Update(rh.Update());
}
}
}