diff --git a/DebugExample/API.cs b/DebugExample/API.cs new file mode 100644 index 00000000..6454c70b --- /dev/null +++ b/DebugExample/API.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Runtime.InteropServices; + +namespace DebugExample +{ + public class API + { + [DllImport("API.dll",EntryPoint = "API_AddChatMessage",CallingConvention = CallingConvention.Cdecl)] + public static extern int API_AddChatMessage(UInt32 color,string text); + [DllImport("API.dll", EntryPoint = "API_GetChatLine", CallingConvention = CallingConvention.Cdecl)] + public static extern int API_GetChatLine(int line, ref String text); + [DllImport("API.dll", EntryPoint = "API_ImageCreate", CallingConvention = CallingConvention.Cdecl)] + public static extern int API_ImageCreate(String path); + [DllImport("API.dll", EntryPoint = "API_ImageShow", CallingConvention = CallingConvention.Cdecl)] + public static extern int API_ImageShow(int id); + [DllImport("API.dll", EntryPoint = "API_ImageDestroy", CallingConvention = CallingConvention.Cdecl)] + public static extern int API_ImageDestroy(int id); + [DllImport("API.dll", EntryPoint = "API_ImageSetPos", CallingConvention = CallingConvention.Cdecl)] + public static extern int API_ImageSetPos(int id,int width,int height); + } +} diff --git a/DebugExample/DebugExample.csproj b/DebugExample/DebugExample.csproj index d0f24799..2cb685bc 100644 --- a/DebugExample/DebugExample.csproj +++ b/DebugExample/DebugExample.csproj @@ -45,6 +45,7 @@ + @@ -52,6 +53,7 @@ + diff --git a/DebugExample/Program.cs b/DebugExample/Program.cs index 31401048..e42baeb9 100644 --- a/DebugExample/Program.cs +++ b/DebugExample/Program.cs @@ -5,16 +5,40 @@ using System.Text; using System.Threading.Tasks; using SpotifyAPIv1; using System.Threading; +using System.Diagnostics; +using System.Drawing; namespace DebugExample { class Program { + static SpotifyAPI test; static void Main(string[] args) { - SpotifyAPI test = new SpotifyAPI(); + test = new SpotifyAPI(); test.Connect(); - Thread.Sleep(-1); + Console.WriteLine("Connected..."); + test.Update(); + Console.WriteLine("Updating first time..."); + test.GetEventHandler().OnTrackNameChange += new SpotifyAPIv1.EventHandler.NameChangeEventHandler(namechange); + test.GetEventHandler().OnPlayStateChange += new SpotifyAPIv1.EventHandler.PlayStateEventHandler(playstate); + test.GetEventHandler().OnVolumeChange += new SpotifyAPIv1.EventHandler.VolumeChangeEventHandler(volumechange); + test.GetEventHandler().ListenForEvents(true); + Console.ReadLine(); + } + public static void namechange(NameChangeEventArgs e) + { + Console.WriteLine("Old Name: " + e.old_track.GetName()); + Console.WriteLine("New Name: " + e.new_track.GetName()); + //API.API_AddChatMessage(0xFFFFFF, "{2ecc71}" + e.new_track.GetName() + " {FFFFFF}[by]{2ecc71} " + e.new_track.GetArtist() + " {8e44ad}[" + e.new_track.GetAlbum() + "]"); + } + public static void playstate(PlayStateEventArgs e) + { + Console.WriteLine("PlayState: " + e.playing); + } + public static void volumechange(VolumeChangeEventArgs e) + { + Console.WriteLine("New Volume: " + e.new_volume); } } } diff --git a/SpotifyAPI/Enum.cs b/SpotifyAPI/Enum.cs new file mode 100644 index 00000000..7359de4f --- /dev/null +++ b/SpotifyAPI/Enum.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SpotifyAPIv1 +{ + public enum SizeEnum + { + SIZE_160 = 160, + SIZE_320 = 320, + SIZE_640 = 640 + } +} diff --git a/SpotifyAPI/EventHandler.cs b/SpotifyAPI/EventHandler.cs new file mode 100644 index 00000000..555a58fc --- /dev/null +++ b/SpotifyAPI/EventHandler.cs @@ -0,0 +1,86 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SpotifyAPIv1 +{ + public class EventHandler + { + private Boolean listen = false; + private Boolean blocked = false; + private System.Timers.Timer timer; + private SpotifyAPI api; + private MusicHandler mh; + + private StatusResponse response; + + public delegate void NameChangeEventHandler(NameChangeEventArgs e); + public delegate void PlayStateEventHandler(PlayStateEventArgs e); + public delegate void VolumeChangeEventHandler(VolumeChangeEventArgs e); + public event NameChangeEventHandler OnTrackNameChange; + public event PlayStateEventHandler OnPlayStateChange; + public event VolumeChangeEventHandler OnVolumeChange; + + public EventHandler(SpotifyAPI api, MusicHandler mh) + { + timer = new System.Timers.Timer(); + timer.Interval = 50; + timer.Elapsed += tick; + timer.AutoReset = false; + timer.Enabled = true; + timer.Start(); + + this.api = api; + this.mh = mh; + } + + public void ListenForEvents(Boolean listen) + { + this.listen = listen; + } + + public void tick(object sender, EventArgs e) + { + if (!listen || blocked) + { + timer.Start(); + return; + } + api.Update(); + if (response == null) + { + response = mh.GetStatusResponse(); + timer.Start(); + return; + } + StatusResponse new_response = mh.GetStatusResponse(); + if (new_response.track.GetName() != response.track.GetName() && OnTrackNameChange != null) + { + OnTrackNameChange(new NameChangeEventArgs() + { + old_track = response.track, + new_track = new_response.track + }); + } + if (new_response.playing != response.playing && OnPlayStateChange != null) + { + OnPlayStateChange(new PlayStateEventArgs() + { + playing = new_response.playing + }); + } + if (new_response.volume != response.volume && OnVolumeChange != null) + { + OnVolumeChange(new VolumeChangeEventArgs() + { + old_volume = response.volume, + new_volume = new_response.volume + }); + } + response = new_response; + timer.Start(); + } + } +} diff --git a/SpotifyAPI/Events.cs b/SpotifyAPI/Events.cs new file mode 100644 index 00000000..99f0ccc9 --- /dev/null +++ b/SpotifyAPI/Events.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SpotifyAPIv1 +{ + class Events + { + + } + public class NameChangeEventArgs + { + public Track old_track { get; set; } + public Track new_track { get; set; } + } + public class PlayStateEventArgs + { + public Boolean playing { get; set; } + } + public class VolumeChangeEventArgs + { + public double old_volume { get; set; } + public double new_volume { get; set; } + } +} diff --git a/SpotifyAPI/MusicHandler.cs b/SpotifyAPI/MusicHandler.cs index 20b731d3..f881884b 100644 --- a/SpotifyAPI/MusicHandler.cs +++ b/SpotifyAPI/MusicHandler.cs @@ -6,17 +6,32 @@ using System.Threading.Tasks; namespace SpotifyAPIv1 { - class MusicHandler + public class MusicHandler { - + RemoteHandler rh; + StatusResponse sr; public MusicHandler() { - + rh = RemoteHandler.GetInstance(); + } + public Boolean IsPlaying() + { + return sr.playing; } - public void Update() + public Track GetCurrentTrack() { + return sr.track; + } + public StatusResponse GetStatusResponse() + { + return sr; + } + + internal void Update(StatusResponse sr) + { + this.sr = sr; } } } diff --git a/SpotifyAPI/RemoteHandler.cs b/SpotifyAPI/RemoteHandler.cs index 32095838..f7039ff1 100644 --- a/SpotifyAPI/RemoteHandler.cs +++ b/SpotifyAPI/RemoteHandler.cs @@ -18,8 +18,8 @@ namespace SpotifyAPIv1 public String host = "127.0.0.1"; WebClient wc; - - public static RemoteHandler GetInstance() + MusicHandler mh; + internal static RemoteHandler GetInstance() { return instance; } @@ -30,11 +30,22 @@ namespace SpotifyAPIv1 wc.Headers.Add("Origin", "https://embed.spotify.com"); wc.Headers.Add("Referer", "https://embed.spotify.com/?uri=spotify:track:5Zp4SWOpbuOdnsxLqwgutt"); } - public void Init() + internal void Init() { oauthKey = GetOAuthKey(); cfidKey = GetCFID(); } + internal StatusResponse Update() + { + String response = recv("remote/status.json", true, true, -1); + if(response == "") + { + return Update(); + } + response = response.Replace("\\n", ""); + List raw = (List)JsonConvert.DeserializeObject(response,typeof(List)); + return raw[0]; + } private String GetOAuthKey() { String raw = ""; diff --git a/SpotifyAPI/SpotifyAPI.cs b/SpotifyAPI/SpotifyAPI.cs index b548ce99..cef6067a 100644 --- a/SpotifyAPI/SpotifyAPI.cs +++ b/SpotifyAPI/SpotifyAPI.cs @@ -9,6 +9,7 @@ using System.Text.RegularExpressions; using System.Windows; using System.Management; using System.Diagnostics; +using System.Drawing; namespace SpotifyAPIv1 { @@ -16,17 +17,26 @@ namespace SpotifyAPIv1 { MusicHandler mh; RemoteHandler rh; - + EventHandler eh; public SpotifyAPI() { rh = RemoteHandler.GetInstance(); + mh = new MusicHandler(); + eh = new EventHandler(this, mh); } public void Connect() { rh.Init(); } - + public MusicHandler GetMusicHandler() + { + return mh; + } + public EventHandler GetEventHandler() + { + return eh; + } public Boolean IsSpotifyRunning(Boolean runIt) { if (Process.GetProcessesByName("SpotifyWebHelper").Length < 1) @@ -41,11 +51,10 @@ namespace SpotifyAPIv1 } else return true; - } public void Update() { - + mh.Update(rh.Update()); } } } diff --git a/SpotifyAPI/SpotifyAPI.csproj b/SpotifyAPI/SpotifyAPI.csproj index 3a46d7f7..95810aa4 100644 --- a/SpotifyAPI/SpotifyAPI.csproj +++ b/SpotifyAPI/SpotifyAPI.csproj @@ -36,6 +36,7 @@ + @@ -45,10 +46,14 @@ + + + + diff --git a/SpotifyAPI/StatusResponse.cs b/SpotifyAPI/StatusResponse.cs new file mode 100644 index 00000000..db750f46 --- /dev/null +++ b/SpotifyAPI/StatusResponse.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SpotifyAPIv1 +{ + public class StatusResponse + { + public int version { get; set; } + public string client_version { get; set; } + public bool playing { get; set; } + public bool shuffle { get; set; } + public bool repeat { get; set; } + public bool play_enabled { get; set; } + public bool prev_enabled { get; set; } + public bool next_enabled { get; set; } + public Track track { get; set; } + public double playing_position { get; set; } + public int server_time { get; set; } + public double volume { get; set; } + public bool online { get; set; } + public bool running { get; set; } + } +} diff --git a/SpotifyAPI/Track.cs b/SpotifyAPI/Track.cs index a4740e89..998186fb 100644 --- a/SpotifyAPI/Track.cs +++ b/SpotifyAPI/Track.cs @@ -3,18 +3,105 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Drawing; +using System.Net; +using System.IO; namespace SpotifyAPIv1 { - class Track + public class Track { - public String titel { get; private set; } - public String artist { get; private set; } + public TrackResource track_resource { get; set; } + public TrackResource artist_resource { get; set; } + public TrackResource album_resource { get; set; } - public Track(String titel,String artist) + public String GetName() { - this.titel = titel; - this.artist = artist; + return track_resource.name; + } + public String GetAlbum() + { + return album_resource.name; + } + public String GetArtist() + { + return artist_resource.name; + } + public String GetAlbumArtURL(SizeEnum size) + { + int albumsize = 0; + switch (size) + { + case SizeEnum.SIZE_160: + albumsize = 160; + break; + case SizeEnum.SIZE_320: + albumsize = 320; + break; + case SizeEnum.SIZE_640: + albumsize = 640; + break; + } + String raw = new WebClient().DownloadString("http://open.spotify.com/album/" + album_resource.uri.Split(new string[] { ":" }, StringSplitOptions.None)[2]); + raw = raw.Replace("\t", ""); ; + string[] lines = raw.Split(new string[] { "\n" }, StringSplitOptions.None); + foreach (string line in lines) + { + if (line.StartsWith("", ""); + } + } + return ""; + } + public Bitmap GetAlbumArt(SizeEnum size) + { + WebClient wc = new WebClient(); + int albumsize = 0; + switch (size) + { + case SizeEnum.SIZE_160: + albumsize = 160; + break; + case SizeEnum.SIZE_320: + albumsize = 320; + break; + case SizeEnum.SIZE_640: + albumsize = 640; + break; + } + String raw = wc.DownloadString("http://open.spotify.com/album/" + album_resource.uri.Split(new string[] { ":" }, StringSplitOptions.None)[2]); + raw = raw.Replace("\t", ""); ; + string[] lines = raw.Split(new string[] { "\n" }, StringSplitOptions.None); + foreach (string line in lines) + { + if (line.StartsWith("", ""); + using (MemoryStream ms = new MemoryStream(wc.DownloadData(url))) + { + return (Bitmap)Image.FromStream(ms); + } + } + } + return null; } } + public class TrackResource + { + public String name { get; set; } + public String uri { get; set; } + public TrackResourceLocation location { get; set; } + } + public class TrackResourceLocation + { + public String og { get; set; } + } + internal class OpenGraphState + { + public Boolean private_session { get; set; } + public Boolean posting_disabled { get; set; } + } }