This commit is contained in:
Johnny @PC 2014-01-10 08:09:14 +01:00
parent cae9f34acf
commit 247bddd685
8 changed files with 202 additions and 68 deletions

View File

@ -13,13 +13,25 @@ namespace DebugExample
public static extern int API_AddChatMessage(UInt32 color,string text); public static extern int API_AddChatMessage(UInt32 color,string text);
[DllImport("API.dll", EntryPoint = "API_GetChatLine", CallingConvention = CallingConvention.Cdecl)] [DllImport("API.dll", EntryPoint = "API_GetChatLine", CallingConvention = CallingConvention.Cdecl)]
public static extern int API_GetChatLine(int line, ref String text); public static extern int API_GetChatLine(int line, ref String text);
[DllImport("API.dll", EntryPoint = "API_ImageCreate", CallingConvention = CallingConvention.Cdecl)] [DllImport("API.dll", EntryPoint = "API_BoxCreate", CallingConvention = CallingConvention.Cdecl)]
public static extern int API_ImageCreate(String path); public static extern int API_BoxCreate(int x,int y,int width,int height,UInt32 color,Boolean show);
[DllImport("API.dll", EntryPoint = "API_ImageShow", CallingConvention = CallingConvention.Cdecl)] [DllImport("API.dll", EntryPoint = "API_BoxSetPos", CallingConvention = CallingConvention.Cdecl)]
public static extern int API_ImageShow(int id); public static extern int API_BoxSetPos(int id,int x,int y);
[DllImport("API.dll", EntryPoint = "API_ImageDestroy", CallingConvention = CallingConvention.Cdecl)] [DllImport("API.dll", EntryPoint = "API_BoxSetWidth", CallingConvention = CallingConvention.Cdecl)]
public static extern int API_ImageDestroy(int id); public static extern int API_BoxSetWidth(int id, int width);
[DllImport("API.dll", EntryPoint = "API_ImageSetPos", CallingConvention = CallingConvention.Cdecl)] [DllImport("API.dll", EntryPoint = "API_BoxSetHeight", CallingConvention = CallingConvention.Cdecl)]
public static extern int API_ImageSetPos(int id,int width,int height); public static extern int API_BoxSetHeight(int id, int height);
[DllImport("API.dll", EntryPoint = "API_BoxSetBorderColor", CallingConvention = CallingConvention.Cdecl)]
public static extern int API_BoxSetBorderColor(int id, UInt32 color);
[DllImport("API.dll", EntryPoint = "API_BoxShow", CallingConvention = CallingConvention.Cdecl)]
public static extern int API_BoxShow(int id);
[DllImport("API.dll", EntryPoint = "API_DestroyAllVisual", CallingConvention = CallingConvention.Cdecl)]
public static extern int API_DestroyAllVisual();
[DllImport("API.dll", EntryPoint = "API_BoxSetColor", CallingConvention = CallingConvention.Cdecl)]
public static extern int API_BoxSetColor(int id, UInt32 color);
[DllImport("API.dll", EntryPoint = "API_TextCreate", CallingConvention = CallingConvention.Cdecl)]
public static extern int API_TextCreate(String Font,int size,Boolean italic,Boolean bold,int x,int y,UInt32 color,String text,Boolean show);
[DllImport("API.dll", EntryPoint = "API_TextSetString", CallingConvention = CallingConvention.Cdecl)]
public static extern int API_TextSetString(int id, String text);
} }
} }

View File

@ -12,33 +12,79 @@ namespace DebugExample
{ {
class Program class Program
{ {
static SpotifyAPI test; static SpotifyAPI spotify;
static int title;
static int artist;
static int album;
static int time;
static void Main(string[] args) static void Main(string[] args)
{ {
test = new SpotifyAPI(); API.API_DestroyAllVisual();
test.Connect(); int boxid = API.API_BoxCreate(620, 150, 140, 60, 0xB6000000,true);
Console.WriteLine("Connected..."); API.API_BoxSetBorderColor(boxid, 0xFFFF7700);
test.Update();
Console.WriteLine("Updating first time...");
test.GetEventHandler().OnTrackNameChange += new SpotifyAPIv1.EventHandler.NameChangeEventHandler(namechange); spotify = new SpotifyAPI();
test.GetEventHandler().OnPlayStateChange += new SpotifyAPIv1.EventHandler.PlayStateEventHandler(playstate); spotify.Connect();
test.GetEventHandler().OnVolumeChange += new SpotifyAPIv1.EventHandler.VolumeChangeEventHandler(volumechange); log("Connected...");
test.GetEventHandler().ListenForEvents(true); spotify.Update();
Console.ReadLine(); log("Updating first time...");
spotify.GetEventHandler().OnTrackChange += new SpotifyAPIv1.EventHandler.TrackChangeEventHandler(namechange);
spotify.GetEventHandler().OnPlayStateChange += new SpotifyAPIv1.EventHandler.PlayStateEventHandler(playstate);
spotify.GetEventHandler().OnVolumeChange += new SpotifyAPIv1.EventHandler.VolumeChangeEventHandler(volumechange);
spotify.GetEventHandler().OnTrackTimeChange += new SpotifyAPIv1.EventHandler.TrackTimeChangeEventHandler(timechange);
spotify.GetEventHandler().ListenForEvents(true);
title = API.API_TextCreate("Arial", 19, true, false, 620, 150, 0xFFFFFFFF, spotify.GetMusicHandler().GetCurrentTrack().GetName(), true);
artist = API.API_TextCreate("Arial", 19, true, false, 620, 160, 0xFFFFFFFF, "von: " + spotify.GetMusicHandler().GetCurrentTrack().GetArtist(), true);
album = API.API_TextCreate("Arial", 19, true, false, 620, 170, 0xFFFFFFFF, "Album: " + spotify.GetMusicHandler().GetCurrentTrack().GetAlbum(), true);
Console.WriteLine(spotify.GetMusicHandler().GetTrackPosition());
time = API.API_TextCreate("Arial", 19, false, false, 620, 180, 0xFFFFFFFF, "Time: " + timede(spotify.GetMusicHandler().GetTrackPosition()), true);
String input = "";
while((input = Console.ReadLine()) != "q")
{
if (input == "pause")
spotify.GetMusicHandler().Pause();
if (input == "play")
spotify.GetMusicHandler().Play();
if (input == "skip")
spotify.GetMusicHandler().Skip();
if (input == "prev")
spotify.GetMusicHandler().Previous();
}
API.API_DestroyAllVisual();
} }
public static void namechange(NameChangeEventArgs e) private static String timede(double sec)
{ {
Console.WriteLine("Old Name: " + e.old_track.GetName()); String test = "{0}:{1}";
Console.WriteLine("New Name: " + e.new_track.GetName()); TimeSpan span = TimeSpan.FromSeconds(sec);
//API.API_AddChatMessage(0xFFFFFF, "{2ecc71}" + e.new_track.GetName() + " {FFFFFF}[by]{2ecc71} " + e.new_track.GetArtist() + " {8e44ad}[" + e.new_track.GetAlbum() + "]"); return String.Format(test, span.Minutes, span.Seconds);
}
private static void timechange(TrackTimeChangeEventArgs e)
{
log(e.track_time.ToString());
API.API_TextSetString(time, "Time: " + timede(spotify.GetMusicHandler().GetTrackPosition()));
}
public static void namechange(TrackChangeEventArgs e)
{
API.API_TextSetString(title, e.new_track.GetName());
API.API_TextSetString(artist,"Von :" + e.new_track.GetArtist());
API.API_TextSetString(album, "Album: " + e.new_track.GetAlbum());
log("Old Name: " + e.old_track.GetName());
log("New Name: " + e.new_track.GetName());
} }
public static void playstate(PlayStateEventArgs e) public static void playstate(PlayStateEventArgs e)
{ {
Console.WriteLine("PlayState: " + e.playing); log("PlayState: " + e.playing);
} }
public static void volumechange(VolumeChangeEventArgs e) public static void volumechange(VolumeChangeEventArgs e)
{ {
Console.WriteLine("New Volume: " + e.new_volume); log("New Volume: " + e.new_volume);
}
public static void log(String log)
{
Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss.fff") + "] " + log);
} }
} }
} }

View File

@ -9,19 +9,20 @@ namespace SpotifyAPIv1
public class EventHandler public class EventHandler
{ {
private Boolean listen = false; private Boolean listen = false;
private Boolean blocked = false;
private System.Timers.Timer timer; private System.Timers.Timer timer;
private SpotifyAPI api; private SpotifyAPI api;
private MusicHandler mh; private MusicHandler mh;
private StatusResponse response; private StatusResponse response;
public delegate void NameChangeEventHandler(NameChangeEventArgs e); public delegate void TrackChangeEventHandler(TrackChangeEventArgs e);
public delegate void PlayStateEventHandler(PlayStateEventArgs e); public delegate void PlayStateEventHandler(PlayStateEventArgs e);
public delegate void VolumeChangeEventHandler(VolumeChangeEventArgs e); public delegate void VolumeChangeEventHandler(VolumeChangeEventArgs e);
public event NameChangeEventHandler OnTrackNameChange; public delegate void TrackTimeChangeEventHandler(TrackTimeChangeEventArgs e);
public event TrackChangeEventHandler OnTrackChange;
public event PlayStateEventHandler OnPlayStateChange; public event PlayStateEventHandler OnPlayStateChange;
public event VolumeChangeEventHandler OnVolumeChange; public event VolumeChangeEventHandler OnVolumeChange;
public event TrackTimeChangeEventHandler OnTrackTimeChange;
public EventHandler(SpotifyAPI api, MusicHandler mh) public EventHandler(SpotifyAPI api, MusicHandler mh)
{ {
@ -41,9 +42,9 @@ namespace SpotifyAPIv1
this.listen = listen; this.listen = listen;
} }
public void tick(object sender, EventArgs e) private void tick(object sender, EventArgs e)
{ {
if (!listen || blocked) if (!listen)
{ {
timer.Start(); timer.Start();
return; return;
@ -56,9 +57,9 @@ namespace SpotifyAPIv1
return; return;
} }
StatusResponse new_response = mh.GetStatusResponse(); StatusResponse new_response = mh.GetStatusResponse();
if (new_response.track.GetName() != response.track.GetName() && OnTrackNameChange != null) if (new_response.track.GetName() != response.track.GetName() && OnTrackChange != null)
{ {
OnTrackNameChange(new NameChangeEventArgs() OnTrackChange(new TrackChangeEventArgs()
{ {
old_track = response.track, old_track = response.track,
new_track = new_response.track new_track = new_response.track
@ -79,6 +80,13 @@ namespace SpotifyAPIv1
new_volume = new_response.volume new_volume = new_response.volume
}); });
} }
if(new_response.playing_position != response.playing_position && OnTrackTimeChange != null)
{
OnTrackTimeChange(new TrackTimeChangeEventArgs()
{
track_time = new_response.playing_position
});
}
response = new_response; response = new_response;
timer.Start(); timer.Start();
} }

View File

@ -10,7 +10,7 @@ namespace SpotifyAPIv1
{ {
} }
public class NameChangeEventArgs public class TrackChangeEventArgs
{ {
public Track old_track { get; set; } public Track old_track { get; set; }
public Track new_track { get; set; } public Track new_track { get; set; }
@ -24,4 +24,8 @@ namespace SpotifyAPIv1
public double old_volume { get; set; } public double old_volume { get; set; }
public double new_volume { get; set; } public double new_volume { get; set; }
} }
public class TrackTimeChangeEventArgs
{
public double track_time { get; set; }
}
} }

View File

@ -3,17 +3,32 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace SpotifyAPIv1 namespace SpotifyAPIv1
{ {
public class MusicHandler public class MusicHandler
{ {
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
RemoteHandler rh; RemoteHandler rh;
StatusResponse sr; StatusResponse sr;
const byte VK_MEDIA_NEXT_TRACK = 0xb0;
const byte VK_MEDIA_PREV_TRACK = 0xb1;
public MusicHandler() public MusicHandler()
{ {
rh = RemoteHandler.GetInstance(); rh = RemoteHandler.GetInstance();
} }
void PressKey(byte keyCode)
{
const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
keybd_event(keyCode, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(keyCode, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
public Boolean IsPlaying() public Boolean IsPlaying()
{ {
return sr.playing; return sr.playing;
@ -23,7 +38,27 @@ namespace SpotifyAPIv1
{ {
return sr.track; return sr.track;
} }
public void Skip()
{
PressKey(VK_MEDIA_NEXT_TRACK);
}
public double GetTrackPosition()
{
return sr.playing_position;
}
public void Previous()
{
PressKey(VK_MEDIA_PREV_TRACK);
}
public void Pause()
{
rh.SendPauseRequest();
}
public void Play()
{
rh.SendPlayRequest();
}
public StatusResponse GetStatusResponse() public StatusResponse GetStatusResponse()
{ {
return sr; return sr;

View File

@ -23,10 +23,12 @@ namespace SpotifyAPIv1
{ {
return instance; return instance;
} }
public RemoteHandler() internal RemoteHandler()
{ {
wc = new WebClient(); wc = new WebClient();
wc.Proxy = null;
wc.Headers.Add("Origin", "https://embed.spotify.com"); wc.Headers.Add("Origin", "https://embed.spotify.com");
wc.Headers.Add("Referer", "https://embed.spotify.com/?uri=spotify:track:5Zp4SWOpbuOdnsxLqwgutt"); wc.Headers.Add("Referer", "https://embed.spotify.com/?uri=spotify:track:5Zp4SWOpbuOdnsxLqwgutt");
} }
@ -35,6 +37,18 @@ namespace SpotifyAPIv1
oauthKey = GetOAuthKey(); oauthKey = GetOAuthKey();
cfidKey = GetCFID(); cfidKey = GetCFID();
} }
internal void SendPauseRequest()
{
recv("remote/pause.json?pause=true", true, true, -1);
}
internal void SetVolumeRequest()
{
Console.WriteLine(recv("remote/info.json", true, true, -1));
}
internal void SendPlayRequest()
{
recv("remote/pause.json?pause=false", true, true, -1);
}
internal StatusResponse Update() internal StatusResponse Update()
{ {
String response = recv("remote/status.json", true, true, -1); String response = recv("remote/status.json", true, true, -1);
@ -49,25 +63,13 @@ namespace SpotifyAPIv1
private String GetOAuthKey() private String GetOAuthKey()
{ {
String raw = ""; String raw = "";
try using(WebClient wc = new WebClient())
{ {
raw = new WebClient().DownloadString("https://embed.spotify.com/openplay/?uri=spotify:track:5Zp4SWOpbuOdnsxLqwgutt"); wc.Proxy = null;
raw = wc.DownloadString("http://open.spotify.com/token");
} }
catch(WebException e) Dictionary<String, object> lol = (Dictionary<String, object>)JsonConvert.DeserializeObject<Dictionary<String, object>>(raw);
{ return (String)lol["t"];
}
raw = raw.Replace(" ", "");
string[] lines = raw.Split(new string[] { "\n" }, StringSplitOptions.None);
foreach (string line in lines)
{
if (line.StartsWith("tokenData"))
{
string[] l = line.Split(new string[] { "'" }, StringSplitOptions.None);
return l[1];
}
}
throw new Exception("OAuth Token konnte nicht gefunden werden");
} }
private String GetCFID() private String GetCFID()
@ -80,7 +82,7 @@ namespace SpotifyAPIv1
} }
private string recv(string request, bool oauth, bool cfid, int wait) private string recv(string request, bool oauth, bool cfid, int wait)
{ {
string parameters = "?&ref=&cors=&_=" + ""; string parameters = "?&ref=&cors=&_=" + TimeStamp;
if (request.Contains("?")) if (request.Contains("?"))
{ {
parameters = parameters.Substring(1); parameters = parameters.Substring(1);
@ -114,5 +116,12 @@ namespace SpotifyAPIv1
} }
return derp; return derp;
} }
private int TimeStamp
{
get
{
return Convert.ToInt32((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds);
}
}
} }
} }

View File

@ -32,7 +32,7 @@
<ItemGroup> <ItemGroup>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> <Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>
<HintPath>..\Newtonsoft.Json.dll</HintPath> <HintPath>..\DebugExample\bin\Debug\Newtonsoft.Json.dll</HintPath>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />

View File

@ -14,11 +14,22 @@ namespace SpotifyAPIv1
public TrackResource track_resource { get; set; } public TrackResource track_resource { get; set; }
public TrackResource artist_resource { get; set; } public TrackResource artist_resource { get; set; }
public TrackResource album_resource { get; set; } public TrackResource album_resource { get; set; }
public int length { get; set; }
public string track_type { get; set; }
public String GetName() public String GetName()
{ {
return track_resource.name; return track_resource.name;
} }
public int GetLength()
{
return length;
}
public String GetFormatedLength()
{
String format = "{0}:{1}";
return String.Format(format, TimeSpan.FromSeconds(GetLength()).ToString("mm:ss"));
}
public String GetAlbum() public String GetAlbum()
{ {
return album_resource.name; return album_resource.name;
@ -42,7 +53,12 @@ namespace SpotifyAPIv1
albumsize = 640; albumsize = 640;
break; break;
} }
String raw = new WebClient().DownloadString("http://open.spotify.com/album/" + album_resource.uri.Split(new string[] { ":" }, StringSplitOptions.None)[2]); String raw = "";
using(WebClient wc = new WebClient())
{
wc.Proxy = null;
raw = wc.DownloadString("http://open.spotify.com/album/" + album_resource.uri.Split(new string[] { ":" }, StringSplitOptions.None)[2]);
}
raw = raw.Replace("\t", ""); ; raw = raw.Replace("\t", ""); ;
string[] lines = raw.Split(new string[] { "\n" }, StringSplitOptions.None); string[] lines = raw.Split(new string[] { "\n" }, StringSplitOptions.None);
foreach (string line in lines) foreach (string line in lines)
@ -57,7 +73,6 @@ namespace SpotifyAPIv1
} }
public Bitmap GetAlbumArt(SizeEnum size) public Bitmap GetAlbumArt(SizeEnum size)
{ {
WebClient wc = new WebClient();
int albumsize = 0; int albumsize = 0;
switch (size) switch (size)
{ {
@ -71,22 +86,27 @@ namespace SpotifyAPIv1
albumsize = 640; albumsize = 640;
break; break;
} }
String raw = wc.DownloadString("http://open.spotify.com/album/" + album_resource.uri.Split(new string[] { ":" }, StringSplitOptions.None)[2]); String raw = "";
raw = raw.Replace("\t", ""); ; using(WebClient wc = new WebClient())
string[] lines = raw.Split(new string[] { "\n" }, StringSplitOptions.None);
foreach (string line in lines)
{ {
if (line.StartsWith("<meta property=\"og:image\"")) wc.Proxy = null;
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)
{ {
string[] l = line.Split(new string[] { "/" }, StringSplitOptions.None); if (line.StartsWith("<meta property=\"og:image\""))
String url = "http://o.scdn.co/" + albumsize + @"/" + l[4].Replace("\"", "").Replace(">", "");
using (MemoryStream ms = new MemoryStream(wc.DownloadData(url)))
{ {
return (Bitmap)Image.FromStream(ms); 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;
} }
return null;
} }
} }
public class TrackResource public class TrackResource
@ -99,7 +119,7 @@ namespace SpotifyAPIv1
{ {
public String og { get; set; } public String og { get; set; }
} }
internal class OpenGraphState public class OpenGraphState
{ {
public Boolean private_session { get; set; } public Boolean private_session { get; set; }
public Boolean posting_disabled { get; set; } public Boolean posting_disabled { get; set; }