Skip to content

Commit 69e69f9

Browse files
author
Johnny @PC
committed
Added Docs/Fixxed Spelling URI --> URL
1 parent 03a18e0 commit 69e69f9

11 files changed

Lines changed: 186 additions & 32 deletions

File tree

‎SpotifyAPI.dll‎

0 Bytes
Binary file not shown.

‎SpotifyAPI/CFID.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
namespace SpotifyAPIv1
77
{
8+
/// <summary>
9+
/// JSON Response, used internaly
10+
/// </summary>
811
class CFID
912
{
1013
public Error error { get; set; }
@@ -13,6 +16,9 @@ class CFID
1316
public String client_version { get; set; }
1417
public Boolean running { get; set; }
1518
}
19+
/// <summary>
20+
/// JSON Response, used internaly
21+
/// </summary>
1622
class Error
1723
{
1824
public String type { get; set; }

‎SpotifyAPI/Enum.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33

44
namespace SpotifyAPIv1
55
{
6+
/// <summary>
7+
/// Enum for the AlbumArt
8+
/// </summary>
69
public enum AlbumArtSize
710
{
811
SIZE_160,
912
SIZE_320,
1013
SIZE_640
1114
}
15+
/// <summary>
16+
/// Not implemented yet
17+
/// </summary>
1218
public enum CFIDResponse
1319
{
1420
SUCCESS,

‎SpotifyAPI/Events.cs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,32 @@
33

44
namespace SpotifyAPIv1
55
{
6+
/// <summary>
7+
/// Event gets triggered, when the Track is changed
8+
/// </summary>
69
public class TrackChangeEventArgs
710
{
811
public Track old_track { get; set; }
912
public Track new_track { get; set; }
1013
}
14+
/// <summary>
15+
/// Event gets triggered, when the Playin-state is changed (e.g Play --> Pause)
16+
/// </summary>
1117
public class PlayStateEventArgs
1218
{
1319
public Boolean playing { get; set; }
1420
}
21+
/// <summary>
22+
/// Event gets triggered, when the volume changes
23+
/// </summary>
1524
public class VolumeChangeEventArgs
1625
{
1726
public double old_volume { get; set; }
1827
public double new_volume { get; set; }
1928
}
29+
/// <summary>
30+
/// Event gets triggered, when the tracktime changes
31+
/// </summary>
2032
public class TrackTimeChangeEventArgs
2133
{
2234
public double track_time { get; set; }

‎SpotifyAPI/RemoteHandler.cs‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ internal StatusResponse Update()
5353
String response = query("remote/status.json", true, true, -1);
5454
if(response == "")
5555
{
56-
return Update();
56+
return null;
5757
}
5858
response = response.Replace("\\n", "");
5959
byte[] bytes = Encoding.Default.GetBytes(response);
6060
response = Encoding.UTF8.GetString(bytes);
6161
List<StatusResponse> raw = (List<StatusResponse>)JsonConvert.DeserializeObject(response,typeof(List<StatusResponse>));
6262
return raw[0];
6363
}
64-
private String GetOAuthKey()
64+
internal String GetOAuthKey()
6565
{
6666
String raw = "";
6767
using(WebClient wc = new WebClient())
@@ -73,7 +73,7 @@ private String GetOAuthKey()
7373
return (String)lol["t"];
7474
}
7575

76-
private String GetCFID()
76+
internal String GetCFID()
7777
{
7878
string a = query("simplecsrf/token.json", false, false, -1);
7979
a = a.Replace(@"\", "");
@@ -84,7 +84,7 @@ private String GetCFID()
8484
throw new Exception("SpotifyWebHelper Error: " + d[0].error.message);
8585
return d[0].token;
8686
}
87-
private string query(string request, bool oauth, bool cfid, int wait)
87+
internal string query(string request, bool oauth, bool cfid, int wait)
8888
{
8989
string parameters = "?&ref=&cors=&_=" + GetTimestamp();
9090
if (request.Contains("?"))
@@ -108,19 +108,18 @@ private string query(string request, bool oauth, bool cfid, int wait)
108108
}
109109

110110
string a = "http://" + host + ":4380/" + request + parameters;
111-
string derp = "";
111+
string response = "";
112112
try
113113
{
114-
derp = wc.DownloadString(a);
115-
derp = "[ " + derp + " ]";
114+
response = "[ " + wc.DownloadString(a) + " ]";
116115
}
117116
catch (Exception z)
118117
{
119-
118+
throw;
120119
}
121-
return derp;
120+
return response;
122121
}
123-
private int GetTimestamp()
122+
internal int GetTimestamp()
124123
{
125124
return Convert.ToInt32((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds);
126125
}

‎SpotifyAPI/SpotifyAPI.cs‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,48 +18,81 @@ public SpotifyAPI()
1818
eh = new SpotifyEventHandler(this, mh);
1919
}
2020

21+
/// <summary>
22+
/// Connects with Spotify. Needs to be called before all other SpotifyAPI functions
23+
/// </summary>
2124
public void Connect()
2225
{
2326
rh.Init();
2427
}
28+
/// <summary>
29+
/// Returns the MusicHandler
30+
/// </summary>
31+
/// <returns>Returns the MusicHandler</returns>
2532
public SpotifyMusicHandler GetMusicHandler()
2633
{
2734
return mh;
2835
}
36+
/// <summary>
37+
/// Returns the EventHanlder
38+
/// </summary>
39+
/// <returns>Returns the EventHanlder</returns>
2940
public SpotifyEventHandler GetEventHandler()
3041
{
3142
return eh;
3243
}
44+
/// <summary>
45+
/// Checks if Spotify is running
46+
/// </summary>
47+
/// <returns>True, if it's running, false if not</returns>
3348
public static Boolean IsSpotifyRunning()
3449
{
3550
if (Process.GetProcessesByName("spotify").Length < 1)
3651
return false;
3752
return true;
3853
}
54+
/// <summary>
55+
/// Checks if Spotify's WebHelper is running (Needed for API Calls)
56+
/// </summary>
57+
/// <returns>True, if it's running, false if not</returns>
3958
public static Boolean IsSpotifyWebHelperRunning()
4059
{
4160
if (Process.GetProcessesByName("SpotifyWebHelper").Length < 1)
4261
return false;
4362
return true;
4463
}
64+
/// <summary>
65+
/// Runs Spotify
66+
/// </summary>
4567
public void RunSpotify()
4668
{
4769
if(!IsSpotifyRunning())
4870
Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Spotify\\spotify.exe");
4971
}
72+
/// <summary>
73+
/// Runs Spotify's WebHelper
74+
/// </summary>
5075
public void RunSpotifyWebHelper()
5176
{
5277
if (!IsSpotifyWebHelperRunning())
5378
Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Spotify\\Data\\SpotifyWebHelper.exe");
5479
}
55-
public static Boolean IsValidSpotifyURL(String url)
80+
/// <summary>
81+
/// Checks for a valid SpotifyURL (Still not finished)
82+
/// </summary>
83+
/// <param name="url">The Spotify URI starting with "spotify:"</param>
84+
/// <returns>True if the URI is valid, false if not</returns>
85+
public static Boolean IsValidSpotifyURI(String uri)
5686
{
5787
String[] types = new String[] { "track","album","local","artist"};
58-
String[] split = url.Split(':');
88+
String[] split = uri.Split(':');
5989
if (split.Length < 3)
6090
return false;
6191
return split[0] == "spotify" && Array.IndexOf(types, split[1]) > -1 && split[2].Length == 22;
6292
}
93+
/// <summary>
94+
/// Updates and Fetches all current information about the current track etc.
95+
/// </summary>
6396
public void Update()
6497
{
6598
if (!SpotifyAPI.IsSpotifyWebHelperRunning())

‎SpotifyAPI/SpotifyEventHandler.cs‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,31 @@ public SpotifyEventHandler(SpotifyAPI api, SpotifyMusicHandler mh)
3030
timer.Interval = 50;
3131
timer.Elapsed += tick;
3232
timer.AutoReset = false;
33-
timer.Enabled = true;
34-
timer.Start();
33+
timer.Enabled = false;
3534

3635
this.api = api;
3736
this.mh = mh;
3837
}
39-
38+
/// <summary>
39+
/// If Events should be triggered
40+
/// </summary>
41+
/// <param name="listen">True if you want to listen for events, false if not</param>
4042
public void ListenForEvents(Boolean listen)
4143
{
42-
this.listen = listen;
44+
timer.Enabled = listen;
45+
if (listen)
46+
timer.Start();
4347
}
48+
/// <summary>
49+
/// Sets a synchronizing object, so you don't need to Invoke
50+
/// </summary>
51+
/// <param name="obj">The SynchronizingObject e.g a Form</param>
4452
public void SetSynchronizingObject(System.ComponentModel.ISynchronizeInvoke obj)
4553
{
4654
timer.SynchronizingObject = obj;
4755
}
48-
private void tick(object sender, EventArgs e)
56+
internal void tick(object sender, EventArgs e)
4957
{
50-
if (!listen)
51-
{
52-
timer.Start();
53-
return;
54-
}
5558
api.Update();
5659
if (response == null)
5760
{

‎SpotifyAPI/SpotifyMusicHandler.cs‎

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ namespace SpotifyAPIv1
1111
public class SpotifyMusicHandler
1212
{
1313
[DllImport("user32.dll")]
14-
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
14+
private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
1515
[DllImport("nircmd.dll")]
16-
public static extern bool DoNirCmd(String NirCmdStr);
16+
private static extern bool DoNirCmd(String NirCmdStr);
1717

1818
RemoteHandler rh;
1919
StatusResponse sr;
2020

21+
//Constants for the Keyboard Event (NextTrack + PreviousTrack)
2122
const byte VK_MEDIA_NEXT_TRACK = 0xb0;
2223
const byte VK_MEDIA_PREV_TRACK = 0xb1;
2324
const int KEYEVENTF_EXTENDEDKEY = 0x1;
@@ -27,66 +28,116 @@ public SpotifyMusicHandler()
2728
{
2829
rh = RemoteHandler.GetInstance();
2930
}
31+
/// <summary>
32+
/// Simulates a KeyPress
33+
/// </summary>
34+
/// <param name="keyCode">The keycode for the represented Key</param>
3035
void PressKey(byte keyCode)
3136
{
3237
keybd_event(keyCode, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
3338
keybd_event(keyCode, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
3439
}
40+
/// <summary>
41+
/// Checks if a song is playing
42+
/// </summary>
43+
/// <returns>True if a song is playing, false if not</returns>
3544
public Boolean IsPlaying()
3645
{
3746
return sr.playing;
3847
}
48+
/// <summary>
49+
/// Returns the current Volume
50+
/// </summary>
51+
/// <returns>A value between 0 and 1</returns>
3952
public double GetVolume()
4053
{
4154
return sr.volume;
4255
}
43-
public void PlayURL(String url)
56+
/// <summary>
57+
/// Plays a Spotify URI
58+
/// </summary>
59+
/// <param name="uri">The Spotify URI. Can be checked with <seealso cref="SpotifyAPI.IsValidSpotifyURI"/></param>
60+
public void PlayURL(String uri)
4461
{
45-
rh.SendPlayRequest(url);
62+
rh.SendPlayRequest(uri);
4663
}
64+
/// <summary>
65+
/// Checks if the current "Track" is an Advert
66+
/// </summary>
67+
/// <returns>True if it's an Advert, false if not</returns>
4768
public Boolean IsAdRunning()
4869
{
4970
return !sr.next_enabled && !sr.prev_enabled;
5071
}
72+
/// <summary>
73+
/// Returns the current Track object
74+
/// </summary>
75+
/// <returns>Returns the current track object</returns>
5176
public Track GetCurrentTrack()
5277
{
5378
return sr.track;
5479
}
80+
/// <summary>
81+
/// Skips the current song (Using keypress simulation)
82+
/// </summary>
5583
public void Skip()
5684
{
5785
PressKey(VK_MEDIA_NEXT_TRACK);
5886
}
87+
/// <summary>
88+
/// Emulates the "Previous" Key (Using keypress simulation)
89+
/// </summary>
90+
public void Previous()
91+
{
92+
PressKey(VK_MEDIA_PREV_TRACK);
93+
}
94+
/// <summary>
95+
/// Returns the current track postion
96+
/// </summary>
97+
/// <returns>A double between 0 and ∞</returns>
5998
public double GetTrackPosition()
6099
{
61100
return sr.playing_position;
62101
}
102+
/// <summary>
103+
/// Mutes Spotify (Requires nircmd.dll)
104+
/// </summary>
63105
public void Mute()
64106
{
65107
if(File.Exists("nircmd.dll"))
66108
DoNirCmd("muteappvolume spotify.exe 1");
67109
}
110+
/// <summary>
111+
/// Unmutes Spotify (Requires nircmd.dll)
112+
/// </summary>
68113
public void UnMute()
69114
{
70115
if (File.Exists("nircmd.dll"))
71116
DoNirCmd("muteappvolume spotify.exe 0");
72117
}
73-
public void Previous()
74-
{
75-
PressKey(VK_MEDIA_PREV_TRACK);
76-
}
118+
/// <summary>
119+
/// Pause function
120+
/// </summary>
77121
public void Pause()
78122
{
79123
rh.SendPauseRequest();
80-
81124
}
125+
/// <summary>
126+
/// Play function
127+
/// </summary>
82128
public void Play()
83129
{
84130
rh.SendPlayRequest();
85131
}
132+
/// <summary>
133+
/// Returns all Informations gathered by the JSON Request
134+
/// </summary>
135+
/// <returns>A StatusResponse object</returns>
86136
public StatusResponse GetStatusResponse()
87137
{
88138
return sr;
89139
}
140+
//Used internaly
90141
internal void Update(StatusResponse sr)
91142
{
92143
this.sr = sr;

0 commit comments

Comments
 (0)