diff --git a/SpotifyAPI.dll b/SpotifyAPI.dll index 31d7df71..19fdcf2f 100644 Binary files a/SpotifyAPI.dll and b/SpotifyAPI.dll differ diff --git a/SpotifyAPI/CFID.cs b/SpotifyAPI/CFID.cs index a72df7c7..866de8be 100644 --- a/SpotifyAPI/CFID.cs +++ b/SpotifyAPI/CFID.cs @@ -5,6 +5,9 @@ using System.Text; namespace SpotifyAPIv1 { + /// + /// JSON Response, used internaly + /// class CFID { public Error error { get; set; } @@ -13,6 +16,9 @@ namespace SpotifyAPIv1 public String client_version { get; set; } public Boolean running { get; set; } } + /// + /// JSON Response, used internaly + /// class Error { public String type { get; set; } diff --git a/SpotifyAPI/Enum.cs b/SpotifyAPI/Enum.cs index 5a716644..3a20cd6d 100644 --- a/SpotifyAPI/Enum.cs +++ b/SpotifyAPI/Enum.cs @@ -3,12 +3,18 @@ using System.Text; namespace SpotifyAPIv1 { + /// + /// Enum for the AlbumArt + /// public enum AlbumArtSize { SIZE_160, SIZE_320, SIZE_640 } + /// + /// Not implemented yet + /// public enum CFIDResponse { SUCCESS, diff --git a/SpotifyAPI/Events.cs b/SpotifyAPI/Events.cs index e3463c09..02099887 100644 --- a/SpotifyAPI/Events.cs +++ b/SpotifyAPI/Events.cs @@ -3,20 +3,32 @@ using System.Text; namespace SpotifyAPIv1 { + /// + /// Event gets triggered, when the Track is changed + /// public class TrackChangeEventArgs { public Track old_track { get; set; } public Track new_track { get; set; } } + /// + /// Event gets triggered, when the Playin-state is changed (e.g Play --> Pause) + /// public class PlayStateEventArgs { public Boolean playing { get; set; } } + /// + /// Event gets triggered, when the volume changes + /// public class VolumeChangeEventArgs { public double old_volume { get; set; } public double new_volume { get; set; } } + /// + /// Event gets triggered, when the tracktime changes + /// public class TrackTimeChangeEventArgs { public double track_time { get; set; } diff --git a/SpotifyAPI/RemoteHandler.cs b/SpotifyAPI/RemoteHandler.cs index f3d804d9..6d7c3149 100644 --- a/SpotifyAPI/RemoteHandler.cs +++ b/SpotifyAPI/RemoteHandler.cs @@ -53,7 +53,7 @@ namespace SpotifyAPIv1 String response = query("remote/status.json", true, true, -1); if(response == "") { - return Update(); + return null; } response = response.Replace("\\n", ""); byte[] bytes = Encoding.Default.GetBytes(response); @@ -61,7 +61,7 @@ namespace SpotifyAPIv1 List raw = (List)JsonConvert.DeserializeObject(response,typeof(List)); return raw[0]; } - private String GetOAuthKey() + internal String GetOAuthKey() { String raw = ""; using(WebClient wc = new WebClient()) @@ -73,7 +73,7 @@ namespace SpotifyAPIv1 return (String)lol["t"]; } - private String GetCFID() + internal String GetCFID() { string a = query("simplecsrf/token.json", false, false, -1); a = a.Replace(@"\", ""); @@ -84,7 +84,7 @@ namespace SpotifyAPIv1 throw new Exception("SpotifyWebHelper Error: " + d[0].error.message); return d[0].token; } - private string query(string request, bool oauth, bool cfid, int wait) + internal string query(string request, bool oauth, bool cfid, int wait) { string parameters = "?&ref=&cors=&_=" + GetTimestamp(); if (request.Contains("?")) @@ -108,19 +108,18 @@ namespace SpotifyAPIv1 } string a = "http://" + host + ":4380/" + request + parameters; - string derp = ""; + string response = ""; try { - derp = wc.DownloadString(a); - derp = "[ " + derp + " ]"; + response = "[ " + wc.DownloadString(a) + " ]"; } catch (Exception z) { - + throw; } - return derp; + return response; } - private int GetTimestamp() + internal int GetTimestamp() { return Convert.ToInt32((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds); } diff --git a/SpotifyAPI/SpotifyAPI.cs b/SpotifyAPI/SpotifyAPI.cs index 18ed5ab1..522f7886 100644 --- a/SpotifyAPI/SpotifyAPI.cs +++ b/SpotifyAPI/SpotifyAPI.cs @@ -18,48 +18,81 @@ namespace SpotifyAPIv1 eh = new SpotifyEventHandler(this, mh); } + /// + /// Connects with Spotify. Needs to be called before all other SpotifyAPI functions + /// public void Connect() { 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"); } - public static Boolean IsValidSpotifyURL(String url) + /// + /// 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 = url.Split(':'); + 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()) diff --git a/SpotifyAPI/SpotifyEventHandler.cs b/SpotifyAPI/SpotifyEventHandler.cs index 43782cb4..7e14654a 100644 --- a/SpotifyAPI/SpotifyEventHandler.cs +++ b/SpotifyAPI/SpotifyEventHandler.cs @@ -30,28 +30,31 @@ namespace SpotifyAPIv1 timer.Interval = 50; timer.Elapsed += tick; timer.AutoReset = false; - timer.Enabled = true; - timer.Start(); + timer.Enabled = false; this.api = api; this.mh = mh; } - + /// + /// If Events should be triggered + /// + /// True if you want to listen for events, false if not public void ListenForEvents(Boolean listen) { - this.listen = listen; + timer.Enabled = listen; + if (listen) + timer.Start(); } + /// + /// Sets a synchronizing object, so you don't need to Invoke + /// + /// The SynchronizingObject e.g a Form public void SetSynchronizingObject(System.ComponentModel.ISynchronizeInvoke obj) { timer.SynchronizingObject = obj; } - private void tick(object sender, EventArgs e) + internal void tick(object sender, EventArgs e) { - if (!listen) - { - timer.Start(); - return; - } api.Update(); if (response == null) { diff --git a/SpotifyAPI/SpotifyMusicHandler.cs b/SpotifyAPI/SpotifyMusicHandler.cs index 6d8da130..a9a32380 100644 --- a/SpotifyAPI/SpotifyMusicHandler.cs +++ b/SpotifyAPI/SpotifyMusicHandler.cs @@ -11,13 +11,14 @@ namespace SpotifyAPIv1 public class SpotifyMusicHandler { [DllImport("user32.dll")] - static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo); + private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo); [DllImport("nircmd.dll")] - public static extern bool DoNirCmd(String NirCmdStr); + private static extern bool DoNirCmd(String NirCmdStr); RemoteHandler rh; StatusResponse sr; + //Constants for the Keyboard Event (NextTrack + PreviousTrack) const byte VK_MEDIA_NEXT_TRACK = 0xb0; const byte VK_MEDIA_PREV_TRACK = 0xb1; const int KEYEVENTF_EXTENDEDKEY = 0x1; @@ -27,66 +28,116 @@ namespace SpotifyAPIv1 { rh = RemoteHandler.GetInstance(); } + /// + /// Simulates a KeyPress + /// + /// The keycode for the represented Key void PressKey(byte keyCode) { keybd_event(keyCode, 0x45, KEYEVENTF_EXTENDEDKEY, 0); keybd_event(keyCode, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); } + /// + /// Checks if a song is playing + /// + /// True if a song is playing, false if not public Boolean IsPlaying() { return sr.playing; } + /// + /// Returns the current Volume + /// + /// A value between 0 and 1 public double GetVolume() { return sr.volume; } - public void PlayURL(String url) + /// + /// Plays a Spotify URI + /// + /// The Spotify URI. Can be checked with + public void PlayURL(String uri) { - rh.SendPlayRequest(url); + rh.SendPlayRequest(uri); } + /// + /// Checks if the current "Track" is an Advert + /// + /// True if it's an Advert, false if not public Boolean IsAdRunning() { return !sr.next_enabled && !sr.prev_enabled; } + /// + /// Returns the current Track object + /// + /// Returns the current track object public Track GetCurrentTrack() { return sr.track; } + /// + /// Skips the current song (Using keypress simulation) + /// public void Skip() { PressKey(VK_MEDIA_NEXT_TRACK); } + /// + /// Emulates the "Previous" Key (Using keypress simulation) + /// + public void Previous() + { + PressKey(VK_MEDIA_PREV_TRACK); + } + /// + /// Returns the current track postion + /// + /// A double between 0 and ∞ public double GetTrackPosition() { return sr.playing_position; } + /// + /// Mutes Spotify (Requires nircmd.dll) + /// public void Mute() { if(File.Exists("nircmd.dll")) DoNirCmd("muteappvolume spotify.exe 1"); } + /// + /// Unmutes Spotify (Requires nircmd.dll) + /// public void UnMute() { if (File.Exists("nircmd.dll")) DoNirCmd("muteappvolume spotify.exe 0"); } - public void Previous() - { - PressKey(VK_MEDIA_PREV_TRACK); - } + /// + /// Pause function + /// public void Pause() { rh.SendPauseRequest(); - } + /// + /// Play function + /// public void Play() { rh.SendPlayRequest(); } + /// + /// Returns all Informations gathered by the JSON Request + /// + /// A StatusResponse object public StatusResponse GetStatusResponse() { return sr; } + //Used internaly internal void Update(StatusResponse sr) { this.sr = sr; diff --git a/SpotifyAPI/StatusResponse.cs b/SpotifyAPI/StatusResponse.cs index 7fb310a2..fc415624 100644 --- a/SpotifyAPI/StatusResponse.cs +++ b/SpotifyAPI/StatusResponse.cs @@ -7,6 +7,7 @@ namespace SpotifyAPIv1 { public class StatusResponse { + //All information got from the JSON Response public int version { get; set; } public string client_version { get; set; } public bool playing { get; set; } diff --git a/SpotifyAPI/Track.cs b/SpotifyAPI/Track.cs index b5e6982e..3d1cd61f 100644 --- a/SpotifyAPI/Track.cs +++ b/SpotifyAPI/Track.cs @@ -16,34 +16,67 @@ namespace SpotifyAPIv1 public int length { get; set; } public string track_type { get; set; } + /// + /// Returns the track name + /// + /// A String. which is the track name public String GetTrackName() { return track_resource.name; } + /// + /// Returns the track lenght + /// + /// A integer, which is the track length public int GetLength() { return length; } + /// + /// Returns the URI for the album + /// + /// A String, which is the album URI public String GetAlbumURI() { return album_resource.uri; } + /// + /// Returns the URI for the track + /// + /// A String, which is the track URI public String GetTrackURI() { return track_resource.uri; } + /// + /// Returns the URI for the artist + /// + /// A String, which is the artist URI public String GetArtistURI() { return artist_resource.uri; } + /// + /// Returns the albume name + /// + /// A String, which is the album name public String GetAlbumName() { return album_resource.name; } + /// + /// Returns the artist name + /// + /// A String, which is the artist name public String GetArtistName() { return artist_resource.name; } + /// + /// Returns a URL to the album cover in the provided size + /// + /// AlbumArtSize (160,320,640) + /// A String, which is the URL to the Albumart public String GetAlbumArtURL(AlbumArtSize size) { if (album_resource.uri.Contains("local")) @@ -79,6 +112,11 @@ namespace SpotifyAPIv1 } return ""; } + /// + /// Returns a Bitmap of the album cover in the provided size asynchronous + /// + /// AlbumArtSize (160,320,640) + /// A Bitmap, which is the albumart public async Task GetAlbumArtAsync(AlbumArtSize size) { using (WebClient wc = new WebClient()) @@ -102,6 +140,11 @@ namespace SpotifyAPIv1 } } } + /// + /// Returns a Bitmap of the album cover in the provided size + /// + /// AlbumArtSize (160,320,640) + /// A Bitmap, which is the albumart public Bitmap GetAlbumArt(AlbumArtSize size) { using(WebClient wc = new WebClient()) diff --git a/SpotifyAPI_Example/Form1.cs b/SpotifyAPI_Example/Form1.cs index bef64070..cb69410e 100644 --- a/SpotifyAPI_Example/Form1.cs +++ b/SpotifyAPI_Example/Form1.cs @@ -106,7 +106,7 @@ namespace SpotifyAPI_Example private void button5_Click(object sender, EventArgs e) { - if (SpotifyAPI.IsValidSpotifyURL(textBox1.Text)) + if (SpotifyAPI.IsValidSpotifyURI(textBox1.Text)) mh.PlayURL(textBox1.Text); }