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);
[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);
[DllImport("API.dll", EntryPoint = "API_BoxCreate", CallingConvention = CallingConvention.Cdecl)]
public static extern int API_BoxCreate(int x,int y,int width,int height,UInt32 color,Boolean show);
[DllImport("API.dll", EntryPoint = "API_BoxSetPos", CallingConvention = CallingConvention.Cdecl)]
public static extern int API_BoxSetPos(int id,int x,int y);
[DllImport("API.dll", EntryPoint = "API_BoxSetWidth", CallingConvention = CallingConvention.Cdecl)]
public static extern int API_BoxSetWidth(int id, int width);
[DllImport("API.dll", EntryPoint = "API_BoxSetHeight", CallingConvention = CallingConvention.Cdecl)]
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
{
static SpotifyAPI test;
static SpotifyAPI spotify;
static int title;
static int artist;
static int album;
static int time;
static void Main(string[] args)
{
test = new SpotifyAPI();
test.Connect();
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)
API.API_DestroyAllVisual();
int boxid = API.API_BoxCreate(620, 150, 140, 60, 0xB6000000,true);
API.API_BoxSetBorderColor(boxid, 0xFFFF7700);
spotify = new SpotifyAPI();
spotify.Connect();
log("Connected...");
spotify.Update();
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")
{
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() + "]");
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();
}
private static String timede(double sec)
{
String test = "{0}:{1}";
TimeSpan span = TimeSpan.FromSeconds(sec);
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)
{
Console.WriteLine("PlayState: " + e.playing);
log("PlayState: " + e.playing);
}
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
{
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 TrackChangeEventHandler(TrackChangeEventArgs e);
public delegate void PlayStateEventHandler(PlayStateEventArgs 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 VolumeChangeEventHandler OnVolumeChange;
public event TrackTimeChangeEventHandler OnTrackTimeChange;
public EventHandler(SpotifyAPI api, MusicHandler mh)
{
@ -41,9 +42,9 @@ namespace SpotifyAPIv1
this.listen = listen;
}
public void tick(object sender, EventArgs e)
private void tick(object sender, EventArgs e)
{
if (!listen || blocked)
if (!listen)
{
timer.Start();
return;
@ -56,9 +57,9 @@ namespace SpotifyAPIv1
return;
}
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,
new_track = new_response.track
@ -79,6 +80,13 @@ namespace SpotifyAPIv1
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;
timer.Start();
}

View File

@ -10,7 +10,7 @@ namespace SpotifyAPIv1
{
}
public class NameChangeEventArgs
public class TrackChangeEventArgs
{
public Track old_track { get; set; }
public Track new_track { get; set; }
@ -24,4 +24,8 @@ namespace SpotifyAPIv1
public double old_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.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace SpotifyAPIv1
{
public class MusicHandler
{
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
RemoteHandler rh;
StatusResponse sr;
const byte VK_MEDIA_NEXT_TRACK = 0xb0;
const byte VK_MEDIA_PREV_TRACK = 0xb1;
public MusicHandler()
{
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()
{
return sr.playing;
@ -23,7 +38,27 @@ namespace SpotifyAPIv1
{
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()
{
return sr;

View File

@ -24,9 +24,11 @@ namespace SpotifyAPIv1
return instance;
}
public RemoteHandler()
internal RemoteHandler()
{
wc = new WebClient();
wc.Proxy = null;
wc.Headers.Add("Origin", "https://embed.spotify.com");
wc.Headers.Add("Referer", "https://embed.spotify.com/?uri=spotify:track:5Zp4SWOpbuOdnsxLqwgutt");
}
@ -35,6 +37,18 @@ namespace SpotifyAPIv1
oauthKey = GetOAuthKey();
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()
{
String response = recv("remote/status.json", true, true, -1);
@ -49,25 +63,13 @@ namespace SpotifyAPIv1
private String GetOAuthKey()
{
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)
{
}
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");
Dictionary<String, object> lol = (Dictionary<String, object>)JsonConvert.DeserializeObject<Dictionary<String, object>>(raw);
return (String)lol["t"];
}
private String GetCFID()
@ -80,7 +82,7 @@ namespace SpotifyAPIv1
}
private string recv(string request, bool oauth, bool cfid, int wait)
{
string parameters = "?&ref=&cors=&_=" + "";
string parameters = "?&ref=&cors=&_=" + TimeStamp;
if (request.Contains("?"))
{
parameters = parameters.Substring(1);
@ -114,5 +116,12 @@ namespace SpotifyAPIv1
}
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>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Newtonsoft.Json.dll</HintPath>
<HintPath>..\DebugExample\bin\Debug\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />

View File

@ -14,11 +14,22 @@ namespace SpotifyAPIv1
public TrackResource track_resource { get; set; }
public TrackResource artist_resource { get; set; }
public TrackResource album_resource { get; set; }
public int length { get; set; }
public string track_type { get; set; }
public String GetName()
{
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()
{
return album_resource.name;
@ -42,7 +53,12 @@ namespace SpotifyAPIv1
albumsize = 640;
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", ""); ;
string[] lines = raw.Split(new string[] { "\n" }, StringSplitOptions.None);
foreach (string line in lines)
@ -57,7 +73,6 @@ namespace SpotifyAPIv1
}
public Bitmap GetAlbumArt(SizeEnum size)
{
WebClient wc = new WebClient();
int albumsize = 0;
switch (size)
{
@ -71,7 +86,11 @@ namespace SpotifyAPIv1
albumsize = 640;
break;
}
String raw = wc.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", ""); ;
string[] lines = raw.Split(new string[] { "\n" }, StringSplitOptions.None);
foreach (string line in lines)
@ -89,6 +108,7 @@ namespace SpotifyAPIv1
return null;
}
}
}
public class TrackResource
{
public String name { get; set; }
@ -99,7 +119,7 @@ namespace SpotifyAPIv1
{
public String og { get; set; }
}
internal class OpenGraphState
public class OpenGraphState
{
public Boolean private_session { get; set; }
public Boolean posting_disabled { get; set; }