From 69e69f9cf5042e8646fb80f43ad2eb17ae0bd4c3 Mon Sep 17 00:00:00 2001 From: "Johnny @PC" Date: Thu, 13 Feb 2014 14:23:52 +0100 Subject: [PATCH] Added Docs/Fixxed Spelling URI --> URL --- SpotifyAPI.dll | Bin 24576 -> 24576 bytes SpotifyAPI/CFID.cs | 6 +++ SpotifyAPI/Enum.cs | 6 +++ SpotifyAPI/Events.cs | 12 ++++++ SpotifyAPI/RemoteHandler.cs | 19 ++++---- SpotifyAPI/SpotifyAPI.cs | 37 +++++++++++++++- SpotifyAPI/SpotifyEventHandler.cs | 23 +++++----- SpotifyAPI/SpotifyMusicHandler.cs | 69 ++++++++++++++++++++++++++---- SpotifyAPI/StatusResponse.cs | 1 + SpotifyAPI/Track.cs | 43 +++++++++++++++++++ SpotifyAPI_Example/Form1.cs | 2 +- 11 files changed, 186 insertions(+), 32 deletions(-) diff --git a/SpotifyAPI.dll b/SpotifyAPI.dll index 31d7df71230121c8a0e427c3168d7f62b3c88b0b..19fdcf2f754a263dadb828489185a54b72129479 100644 GIT binary patch delta 2274 zcmYk;e@v8h90&0C_qjWs=Z?FR6OJH}@cePtfr^5l1d4)!CWr?r$OWQgxLSZeFb8%R zN;a*@9!D)`*>Y-0E%*H6mTN5C(xz)=wN#ea*!s29GPg8leV*_0ne(>ec-{N+`Fy|6 zclR804~y<$acIBI_r-YMfyv^Hz7N)kTwREuV|)jYrcY6_^g0m~hxI)o=4;yt`6#EK z3LF?Rd`hBCwCmFyO`DTjCjl-fw%ro}yJFv+NG}LeX=1j5sq|_Cd{8=l^uEn1wk1Ch zuDF?Q>&@0Qb3l0E!iN3r7<3#Bt)F<^V}%d`oM-Zr87voP+Moy3R~@Vw1pPXZ*Kf( z_u#?f&=T#L@Y!keB7s)G6=d9l-N^R#PMmtgycG1}6MFy?@fGV=`ydiAD%GQ3$@n>T ze!8sF${cqw7uQ*99QR4Tur^Vaga26Vl;uF1A^TDalpu1EU`e~+NGAnZ-#G%L>_;l+ zVLoR{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Ggx@5Z`jKlW*$+>4}x_=dr1jFfgj-sHWN{Ic& z-sFHr^ZgAJ86PLtSg0<3KRI0pyP-O7mnjE)G{)AskhZ3G|6{>!t=nqFZvP933%IE| zft$3YG;2QG)&O-2+81yg-p5emxvt^VywiD!E5rEA>P0Hztlz93APrwi4H}noe~n(9 zC+oCIbp}gthqXz4M4D!`QdS7de6zBPvO?&lW-n8GC_*}CmsJlbpY?<4B^5nYS&UN7 zlq9<-Mr|F_o#Z5LW93pJ>hSbkG zVDphiO+_(4%dA9{)FRdyTM4aMB{hUe+j3H!nQ7nK zGO!BmQiEFb7uxqK^lZ<)2T`YGpd1~Ek17VST}sb4)uvTbWl_-7>smxqqC;wI zb}*yfQi(UDhUL~OF<}Z9X?!Klv94%asqY%=h8ArmPD_sCcWoyrrywz69FL{KQp3ow zCzk1ByXDfNLh3pg3NIW zYf#QQMPUuXtS=~Ak5<-Y%GRTUb;CY`4H#nGwLc<_vi`K&#YP-wSsYGMj5XU45s@e+ znSMu`h~O$~slzU6afek-jkTC&)i^w;gJqHV4jUX^lFsrf9&AD$Yk}e=1x?A%#by+7 zHkI5ZHY2*0c`vz_SjSTIF0lpetW3R^)WZr;R*w zEpeR`+p(Or(ltS?@ZCJ21kUa(PLI zSobMw#RN;8_ezfn5snv^-AYUOTYu2{d3&hx{VS>QV*`bk;&UTYY60DG zL=vkj^pit(0)D#R@Ye&8+KSpd|DN%mFEoUYHl~N_9XH=}#|!Fh!r8Ge(O^E_F;+KZ eD+Bw+=A;|l4Wn__)<+iQSSappx+lgK+5QC<0+U4m 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); }