Example #1
0
 private async static void WebSocket_Closed(IWebSocket sender, WebSocketClosedEventArgs args)
 {
     //Websocket closed, burn everything down
     SpotifyState    = null;
     IsWebSocketOpen = false;
     SpotifyStateUpdated?.Invoke(null, null);
 }
Example #2
0
        public async static Task <bool> GetInitialPlayerStatus()
        {
            using (HttpClient client = new HttpClient())
            {
                //Ask about options
                await client.SendAsync(new HttpRequestMessage(new HttpMethod(HttpMethods.Options), "https://api.spotify.com/v1/me/player"));
            }
            using (HttpClient client = new HttpClient())
            {
                //Get current state
                string url = "https://api.spotify.com/v1/me/player";
                client.DefaultRequestHeaders.Add("authorization", "Bearer " + spotifyToken);
                var resp = await client.GetAsync(url);

                if (resp.IsSuccessStatusCode)
                {
                    string content = await resp.Content.ReadAsStringAsync();

                    SpotifyState = JsonConvert.DeserializeObject <JSON.State>(content);
                    SpotifyStateUpdated?.Invoke(null, null);
                    return(true);
                }
                else if (resp.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                {
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
        }
Example #3
0
        private async static void WebSocket_MessageReceived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args)
        {
            if (timer != null)
            {
                await App.dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                                              () =>
                {
                    timer.Stop();
                    timer.Start();
                });
            }

            var dr = args.GetDataReader();

            JSON.Root root = Newtonsoft.Json.JsonConvert.DeserializeObject <JSON.Root>(dr.ReadString(dr.UnconsumedBufferLength));
            if (root.Type == "message")
            {
                if (root.Payloads != null)
                {
                    foreach (var payload in root.Payloads)
                    {
                        if (payload.Events != null)
                        {
                            foreach (var ev in payload.Events)
                            {
                                if (ev.Type == "PLAYER_STATE_CHANGED")
                                {
                                    SpotifyState = ev.Event.State;
                                    SpotifyStateUpdated?.Invoke(null, null);
                                }
                                else if (ev.Type == "DEVICE_STATE_CHANGED")
                                {
                                    //check for the spotify status again, maybe it's null (with invalid token verification)
                                    if (!await GetInitialPlayerStatus())
                                    {
                                        if (await UpdateToken())
                                        {
                                            await GetInitialPlayerStatus();
                                        }
                                        else //otherwise, just give up and burn the entire thing down
                                        {
                                            SpotifyState = null; SpotifyStateUpdated?.Invoke(null, null);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else if (root.Headers != null && root.Headers.SpotifyConnectionId != null)
                {
                    using (HttpClient client = new HttpClient())
                    {
                        string url = "https://api.spotify.com/v1/me/notifications/player?connection_id=" + Uri.EscapeDataString(root.Headers.SpotifyConnectionId);
                        client.DefaultRequestHeaders.Add("Access-Control-Request-Method", "PUT");
                        client.DefaultRequestHeaders.Add("Access-Control-Request-Headers", "authorization");
                        client.DefaultRequestHeaders.Add("Referer", "https://discordapp.com/channels/@me");
                        client.DefaultRequestHeaders.Add("Origin", "https://discordapp.com");
                        await client.SendAsync(new HttpRequestMessage(HttpMethod.Options, url));
                    }
                    using (HttpClient client = new HttpClient())
                    {
                        string url = "https://api.spotify.com/v1/me/notifications/player?connection_id=" + Uri.EscapeDataString(root.Headers.SpotifyConnectionId);
                        client.DefaultRequestHeaders.Add("authorization", "Bearer " + spotifyToken);
                        client.DefaultRequestHeaders.Add("Origin", "https://discordapp.com");
                        await client.SendAsync(new HttpRequestMessage(HttpMethod.Put, url));
                    }
                }
            }
        }