Example #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("SpotifyLocalAPI demonstration");
            Console.WriteLine("By JariZ.nl 2012");
            Console.WriteLine("Warning: This demo requires you to have Spotify open and running!");
            Console.WriteLine();
            Console.WriteLine("Authenticating...");

            try
            {
                SpotifyAPI API = new SpotifyAPI(SpotifyAPI.GetOAuth(), "jariz-example.spotilocal.com");
                Responses.CFID cfid = API.CFID; //It's required to get the contents of API.CFID before doing anything, even if you're not intending to do anything with the CFID
                if (cfid.error != null)
                {
                    Console.WriteLine(string.Format("Spotify returned a error {0} (0x{1})", cfid.error.message, cfid.error.type));
                    Thread.Sleep(-1);
                }
                Responses.Status Current_Status = API.Status;
                if (cfid.error != null)
                {
                    Console.WriteLine(string.Format("Spotify returned a error {0} (0x{1})", cfid.error.message, cfid.error.type));
                    Thread.Sleep(-1);
                }

                var v = JObject.FromObject(API.ClientVersion);

                Console.WriteLine(v.ToString());

                if (Current_Status.track != null)
                    Console.WriteLine(string.Format("You're listening to {0} - {1} from the album '{2}'", Current_Status.track.track_resource.name, Current_Status.track.artist_resource.name, Current_Status.track.album_resource.name));
                else
                    Console.WriteLine("You're not listening to any songs");

                Thread.Sleep(1000);
                //Pause playback
                Current_Status = API.Pause;

                Thread.Sleep(1000);
                //Resume playback
                Current_Status = API.Resume;

                Thread.Sleep(1000);
                //Play 'Evil Boy'
                API.URI = "spotify:track:3wcekXbEsDFv9OfyJe1q5d";
                Current_Status = API.Play;

                Thread.Sleep(1000);
                //Get current album art and open it in browser
                string art = API.getArt(Current_Status.track.album_resource.uri); //get current art url
                Process.Start(art); //open url in browser

                Console.WriteLine();
                Console.WriteLine("Tests complete.");
            }
            catch (Exception z)
            {
                Console.WriteLine("Unexpected error:\r\n" + z.ToString());
            }
            Thread.Sleep(-1);
        }
        public static void StopPlayback()
        {
            SpotifyAPI api = new SpotifyAPI(SpotifyAPI.GetOAuth(), "andrew.spotilocal.com");

              Responses.CFID cfid = api.CFID;

              if (cfid.error == null)
              {
            Responses.Status status = api.Pause;
              }
        }
Example #3
0
 public Spotify()
 {
     InitializeComponent();
     // Setup API
     api = new SpotifyAPI(SpotifyAPI.GetOAuth(), "reptail.dk");
     cfid = api.CFID;
     if (cfid.error != null)
     {
         UpdateData();
         this.Focus();
     }
     else
     {
         _error = true;
         lblSongName.Text = "Unknown error...";
     }
 }
Example #4
0
        public void Dispose()
        {
            lcd.Dispose();

            cfid = null;
            api = null;

            spotTimer.Enabled = false;
            spotTimer.Dispose();
            spotTimer = null;

            lcdTimer.Enabled = false;
            lcdTimer.Dispose();
            lcdTimer = null;

            refreshTimer.Enabled = false;
            refreshTimer.Dispose();
            refreshTimer = null;

            initExcpt = null;
        }
        public static SpotifyStatus GetStatus()
        {
            SpotifyStatus status = new SpotifyStatus();

              SpotifyAPI api = new SpotifyAPI(SpotifyAPI.GetOAuth(), "andrew.spotilocal.com");

              Responses.CFID cfid = api.CFID;

              if (cfid.error == null)
              {

            Responses.Status currentStatus = api.Status;

            if (currentStatus.track != null)
            {
              status = MapModel(currentStatus, status);
              status.Artwork = api.getArt(currentStatus.track.album_resource.uri);
            }

              }

              return status;
        }
        public static SpotifyStatus PlaySong(string trackId)
        {
            SpotifyStatus status = new SpotifyStatus();

              SpotifyAPI api = new SpotifyAPI(SpotifyAPI.GetOAuth(), "andrew.spotilocal.com");

              Responses.CFID cfid = api.CFID;

              if (cfid.error == null)
              {
            string spotifyUri = String.Format("spotify:track:{0}", trackId);

            api.URI = spotifyUri;
            Responses.Status currentStatus = api.Play;

            if (currentStatus.track.track_resource.uri == spotifyUri)
            {
              status = MapModel(currentStatus, status);
            }

              }

              return status;
        }
Example #7
0
        public Server()
        {
            // Default message.
            Console.WriteLine(
                "This is the CampFire listener, if you keep this running in the background with Spotify open it will play tracks when they are recieved by the webpage.\r\n");

            _api = new SpotifyAPI(SpotifyAPI.GetOAuth(), "SpotifyServer.spotilocal.com");
            _currentStatus = _api.Status;
            _cfid = _api.CFID;

            FleckLog.Level = LogLevel.Debug;

            var allSockets = new List<IWebSocketConnection>();
            var server = new WebSocketServer("ws://localhost:8181/CampFire");

            server.Start(socket =>
                {
                    socket.OnOpen = () =>
                        {
                            Console.WriteLine("Open connection.");
                            // Add the new connection to our list so we can send messages back.
                            allSockets.Add(socket);
                        };

                    socket.OnClose = () =>
                        {
                            Console.WriteLine("Closed connection, the CampFire webpage has been closed.");

                            // Make sure we remove them from out list when we are not connected to em.
                            allSockets.Remove(socket);
                        };

                    socket.OnMessage = message =>
                        {
                            // When a message is recieved it comes in the from NAME^spotify:track:oiasdoijasd so we split the string with ^ to get the name and uri.
                            string[] details = message.Split('^');

                            // Don't ask why we have to do this, if we don't it borks. heh.
                            string username = Convert.ToString(details[0]);
                            string uri = Convert.ToString(details[1]);

                            // Set our spotify to look at the new uri and then play it
                            _api.URI = uri;
                            _currentStatus = _api.Play;

                            string trackName = _currentStatus.track.track_resource.name;
                            string artistName = _currentStatus.track.artist_resource.name;

                            // Message all users that X user has played X, we have to do this because there is no other way of telling the other people connecting WHO played what song
                            allSockets.ToList().ForEach(s => s.Send(username + " played " + trackName + " - " + artistName));
                            Console.WriteLine("Playing: " + _currentStatus.track.track_resource.name + " - " +
                                                _currentStatus.track.artist_resource.name);
                        };
                });

            var inpt = Console.ReadLine();
            if (inpt != "exit")
            {
                Console.ReadLine();
            }
        }
Example #8
0
        public static bool Init()
        {
            bool ret = false;

            try
            {
                _API = new SpotifyAPI(SpotifyAPI.GetOAuth(), "127.0.0.1");
                //It's required to get the contents of API.CFID before doing anything, even if you're not intending to do anything with the CFID
                _cfid = _API.CFID;

                if (_cfid.error != null)
                {
                    throw new Exception(string.Format("Spotify returned a error {0} (0x{1})", _cfid.error.message, _cfid.error.type));
                }

                _Current_Status = _API.Status;

                if (_cfid.error != null)
                {
                    throw new Exception(string.Format("Spotify returned a error {0} (0x{1})", _cfid.error.message, _cfid.error.type));
                }

                _tmr = new Timer()
                {
                    Interval = 5000, Enabled = true
                };
                _tmr.Elapsed += _tmr_Elapsed;
                _tmr.Start();

                ret = true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);

                ret = false;
            }

            return ret;
        }
Example #9
0
 public static void Dispose()
 {
     _API = null;
     _cfid = null;
     _tmr.Stop();
 }
Example #10
0
 private void InitSpot()
 {
     try
     {
         api = new SpotifyAPI(SpotifyAPI.GetOAuth());
         cfid = api.CFID;
         initExcpt = null;
     }
     catch (Exception e)
     {
         initExcpt = e;
     }
 }
Example #11
0
        // Attempt to acquire the Spotify resources
        private void setupSpotify()
        {
            m_spotifyApiInstance = new SpotifyAPI(SpotifyAPI.GetOAuth(), "jariz-example.spotilocal.com");
            m_cfid = m_spotifyApiInstance.CFID;

            if (m_cfid.error != null)
            {
                Console.WriteLine(string.Format("Spotify returned a error {0} (0x{1})", m_cfid.error.message, m_cfid.error.type));
               // Thread.Sleep(-1);
            }
            else
            {
                // it was ok
            }
        }