Added alot

This commit is contained in:
Johnny @PC 2014-01-08 23:22:54 +01:00
parent 0d55f4d36b
commit cae9f34acf
12 changed files with 351 additions and 19 deletions

25
DebugExample/API.cs Normal file
View File

@ -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);
}
}

View File

@ -45,6 +45,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
@ -52,6 +53,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="API.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>

View File

@ -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);
}
}
}

15
SpotifyAPI/Enum.cs Normal file
View File

@ -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
}
}

View File

@ -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();
}
}
}

27
SpotifyAPI/Events.cs Normal file
View File

@ -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; }
}
}

View File

@ -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;
}
}
}

View File

@ -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<StatusResponse> raw = (List<StatusResponse>)JsonConvert.DeserializeObject(response,typeof(List<StatusResponse>));
return raw[0];
}
private String GetOAuthKey()
{
String raw = "";

View File

@ -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());
}
}
}

View File

@ -36,6 +36,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
@ -45,10 +46,14 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CFID.cs" />
<Compile Include="Enum.cs" />
<Compile Include="EventHandler.cs" />
<Compile Include="Events.cs" />
<Compile Include="MusicHandler.cs" />
<Compile Include="RemoteHandler.cs" />
<Compile Include="SpotifyAPI.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StatusResponse.cs" />
<Compile Include="Track.cs" />
</ItemGroup>
<ItemGroup>

View File

@ -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; }
}
}

View File

@ -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("<meta property=\"og:image\""))
{
string[] l = line.Split(new string[] { "/" }, StringSplitOptions.None);
return "http://o.scdn.co/" + albumsize + @"/" + l[4].Replace("\"", "").Replace(">", "");
}
}
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("<meta property=\"og:image\""))
{
string[] l = line.Split(new string[] { "/" }, StringSplitOptions.None);
String url = "http://o.scdn.co/" + albumsize + @"/" + l[4].Replace("\"", "").Replace(">", "");
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; }
}
}