Spotify.NET/SpotifyAPI/SpotifyAPI.cs

104 lines
3.4 KiB
C#
Raw Normal View History

2014-01-07 15:26:03 +00:00
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace SpotifyAPIv1
{
public class SpotifyAPI
{
2014-02-01 12:52:33 +00:00
SpotifyMusicHandler mh;
2014-01-07 15:26:03 +00:00
RemoteHandler rh;
2014-02-01 12:52:33 +00:00
SpotifyEventHandler eh;
2014-01-07 15:26:03 +00:00
public SpotifyAPI()
{
rh = RemoteHandler.GetInstance();
2014-02-01 12:52:33 +00:00
mh = new SpotifyMusicHandler();
eh = new SpotifyEventHandler(this, mh);
2014-01-07 15:26:03 +00:00
}
2014-02-13 13:23:52 +00:00
/// <summary>
/// Connects with Spotify. Needs to be called before all other SpotifyAPI functions
/// </summary>
2014-02-14 12:04:45 +00:00
public Boolean Connect()
2014-01-07 15:26:03 +00:00
{
2014-02-14 12:04:45 +00:00
return rh.Init();
2014-01-07 15:26:03 +00:00
}
2014-02-13 13:23:52 +00:00
/// <summary>
/// Returns the MusicHandler
/// </summary>
/// <returns>Returns the MusicHandler</returns>
2014-02-01 12:52:33 +00:00
public SpotifyMusicHandler GetMusicHandler()
2014-01-08 22:22:54 +00:00
{
return mh;
}
2014-02-13 13:23:52 +00:00
/// <summary>
/// Returns the EventHanlder
/// </summary>
/// <returns>Returns the EventHanlder</returns>
2014-02-01 12:52:33 +00:00
public SpotifyEventHandler GetEventHandler()
2014-01-08 22:22:54 +00:00
{
return eh;
}
2014-02-13 13:23:52 +00:00
/// <summary>
/// Checks if Spotify is running
/// </summary>
/// <returns>True, if it's running, false if not</returns>
2014-02-01 12:52:33 +00:00
public static Boolean IsSpotifyRunning()
{
if (Process.GetProcessesByName("spotify").Length < 1)
return false;
return true;
2014-02-01 12:52:33 +00:00
}
2014-02-13 13:23:52 +00:00
/// <summary>
/// Checks if Spotify's WebHelper is running (Needed for API Calls)
/// </summary>
/// <returns>True, if it's running, false if not</returns>
2014-02-01 12:52:33 +00:00
public static Boolean IsSpotifyWebHelperRunning()
2014-01-07 15:26:03 +00:00
{
if (Process.GetProcessesByName("SpotifyWebHelper").Length < 1)
2014-02-01 12:52:33 +00:00
return false;
return true;
2014-01-07 15:26:03 +00:00
}
2014-02-13 13:23:52 +00:00
/// <summary>
/// Runs Spotify
/// </summary>
2014-02-01 12:52:33 +00:00
public void RunSpotify()
{
if(!IsSpotifyRunning())
Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Spotify\\spotify.exe");
}
2014-02-13 13:23:52 +00:00
/// <summary>
/// Runs Spotify's WebHelper
/// </summary>
2014-02-01 12:52:33 +00:00
public void RunSpotifyWebHelper()
{
if (!IsSpotifyWebHelperRunning())
Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Spotify\\Data\\SpotifyWebHelper.exe");
}
2014-02-13 13:23:52 +00:00
/// <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)
2014-02-01 12:52:33 +00:00
{
String[] types = new String[] { "track","album","local","artist"};
2014-02-13 13:23:52 +00:00
String[] split = uri.Split(':');
2014-02-01 12:52:33 +00:00
if (split.Length < 3)
return false;
return split[0] == "spotify" && Array.IndexOf(types, split[1]) > -1 && split[2].Length == 22;
}
2014-02-13 13:23:52 +00:00
/// <summary>
/// Updates and Fetches all current information about the current track etc.
/// </summary>
2014-01-07 15:26:03 +00:00
public void Update()
{
2014-02-14 12:04:45 +00:00
if (!SpotifyAPI.IsSpotifyWebHelperRunning() || !SpotifyAPI.IsSpotifyRunning())
2014-02-01 12:52:33 +00:00
return;
2014-01-08 22:22:54 +00:00
mh.Update(rh.Update());
2014-01-07 15:26:03 +00:00
}
}
}