Пример #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);
                }

                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);
        }
Пример #2
0
 public SpotifyChecker()
 {
     spotifyApi          = new SpotifyAPI(SpotifyAPI.GetOAuth(), "recordify.spotilocal.com");
     cfid                = spotifyApi.CFID;
     currentStatus       = spotifyApi.Status;
     scanningThreadStart = new ThreadStart(getData);
 }
Пример #3
0
 private void InitSpot()
 {
     try
     {
         api       = new SpotifyAPI(SpotifyAPI.GetOAuth());
         cfid      = api.CFID;
         initExcpt = null;
     }
     catch (Exception e)
     {
         initExcpt = e;
     }
 }
Пример #4
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
            }
        }
Пример #5
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;
        }
Пример #6
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);
        }
Пример #7
0
        private void getData()
        {
            while (!askStopScanning)
            {
                cfid          = spotifyApi.CFID;
                currentStatus = spotifyApi.Status;

                //Console.Out.WriteLine("Album Art URL: " + spotifyApi.getArt(currentStatus.track.album_resource.uri));
                if (Process.GetProcessesByName("spotify").Length > 0)
                {
                    // Prüfen, ob schon ein Track abgespielt wird, wenn ja als alten Track abspeichern
                    if (trackInfos != null)
                    {
                        oldTrackInfos = trackInfos;
                    }

                    // Prüfen, ob Spotify gerade einen Track abspielt
                    if (currentStatus.playing)
                    {
                        if (!currentStatus.track.album_resource.name.StartsWith("http://"))
                        {
                            trackInfos = new TrackTag(currentStatus.track.artist_resource.name, currentStatus.track.track_resource.name, currentStatus.track.album_resource.name, true);
                        }
                        else
                        {
                            trackInfos = new TrackTag(currentStatus.track.artist_resource.name, currentStatus.track.track_resource.name, currentStatus.track.album_resource.name, false);
                        }
                    }
                    else
                    {
                        trackInfos = new TrackTag(currentStatus.track.artist_resource.name, currentStatus.track.track_resource.name, currentStatus.track.album_resource.name, false);
                    }

                    // Prüfen, ob sich der Track verändert hat!
                    if (oldTrackInfos != null)
                    {
                        if (oldTrackInfos.Artist != trackInfos.Artist || oldTrackInfos.Title != trackInfos.Title || oldTrackInfos.Album != trackInfos.Album)
                        {
                            OnTrackChanged();
                            Console.WriteLine("Track changed to " + trackInfos.Artist + " - " + trackInfos.Title + "!");
                        }

                        if (oldTrackInfos.IsPlaying != trackInfos.IsPlaying)
                        {
                            if (trackInfos.IsPlaying)
                            {
                                OnTrackStarted();
                                Console.WriteLine("Playing started!");
                            }
                            else
                            {
                                OnTrackPaused();
                                Console.WriteLine("Playing paused!");
                            }
                        }
                    }
                    else
                    {
                        OnTrackChanged();
                        Console.WriteLine("Track changed to " + trackInfos.Artist + " - " + trackInfos.Title + "!");
                    }
                }

                #region old
                //if (currentStatus.playing)
                //{
                //    if (trackInfos != null)
                //    {
                //        oldTrackInfos = trackInfos;
                //    }

                //    if (currentStatus.track != null)
                //    {
                //        trackInfos = new TrackTag(currentStatus.track.artist_resource.name, currentStatus.track.track_resource.name, currentStatus.track.album_resource.name);
                //    }
                //    else
                //    {
                //        trackInfos = new TrackTag();
                //    }

                //    if (oldTrackInfos != null)
                //    {
                //        if (trackInfos.Artist != oldTrackInfos.Artist && trackInfos.Title != oldTrackInfos.Title)
                //        {
                //            OnEvent();
                //            Console.WriteLine("Track changed to " + trackInfos.Artist + " - " + trackInfos.Title + "!");
                //        }
                //    }
                //    else
                //    {
                //        OnEvent();
                //        Console.WriteLine("Track changed to " + trackInfos.Artist + " - " + trackInfos.Title + "!");
                //    }
                //}
                //else
                //{
                //    trackInfos = new TrackTag();
                //    OnEvent();
                //}
                #endregion

                Thread.Sleep(50);
            }
        }
Пример #8
0
 public Spotify()
 {
     cfid           = API.CFID;
     Current_Status = API.Status;
 }
Пример #9
0
 public static void Dispose()
 {
     _API  = null;
     _cfid = null;
     _tmr.Stop();
 }
Пример #10
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();
            }
        }