Exemplo n.º 1
0
        // Obtain the current details of the Spotify player, both track info and the player status
        private MediaPlayerDetails getCurrentSpotifyDetails()
        {
            MediaPlayerDetails retVal = new MediaPlayerDetails();

            Responses.Status Current_Status = m_spotifyApiInstance.Status;
            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);
            }

            if (Current_Status.track != null)
            {
                retVal.currentTrack  = Current_Status.track.track_resource.name;
                retVal.currentAlbum  = Current_Status.track.album_resource.name;
                retVal.currentArtist = Current_Status.track.artist_resource.name;
                int pos = (int)Current_Status.playing_position;
                int len = Current_Status.track.length;
                retVal.playTime = String.Format("{0}:{1:D2}/{2}:{3:D2}", pos / 60, pos % 60, len / 60, len % 60);
            }

            retVal.playing        = Current_Status.playing;
            retVal.online         = Current_Status.online;
            retVal.privateSession = Current_Status.open_graph_state.private_session;

            return(retVal);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
 public SpotifyChecker()
 {
     spotifyApi          = new SpotifyAPI(SpotifyAPI.GetOAuth(), "recordify.spotilocal.com");
     cfid                = spotifyApi.CFID;
     currentStatus       = spotifyApi.Status;
     scanningThreadStart = new ThreadStart(getData);
 }
        private void Update(Responses.Status status)
        {
            if (status == null || status.track == null || status.track.track_resource == null)
            {
                return;
            }
            if (_spotifyStatus == null)
            {
                _spotifyStatus = new SpotifyStatus();
            }
            if ((_spotifyStatus.Uri ?? "") == status.track.track_resource.uri)
            {
                return;
            }
            _spotifyStatus.Uri    = status.track.track_resource.uri;
            _spotifyStatus.Artist = status.track.artist_resource.name;
            _spotifyStatus.Album  = status.track.album_resource.name;
            _spotifyStatus.Track  = status.track.track_resource.name;
            _statusModel.UpdateSongInfo(_spotifyStatus);

            //TODO: Send _spotifyStatus as json to webservice
            var data = JsonConvert.SerializeObject(_spotifyStatus);

            if (!Settings.Default.UrlEncoded)
            {
                WebClientHelper.DoAsyncJsonRequest(Settings.Default.ScrobbleUrl, data);
            }
            else
            {
                WebClientHelper.DoAsyncUrlEncodedRequest(Settings.Default.ScrobbleUrl, data);
            }
        }
Exemplo n.º 5
0
        public void UpdateSpot()
        {
            if (initExcpt != null)
            {
                return;
            }

            try
            {
                currentStatus = api.Status;
            }
            catch (Exception e)
            {
                initExcpt = e;
            }
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
            }
        }
 /// <summary>
 /// Creates a new instance
 /// </summary>
 public SatusChangedEventArgs(Commands.Idle.SubSystems[] systemsChanged, Responses.Status status, Responses.SongInfo currentSong)
 {
     Systems     = systemsChanged;
     Status      = status;
     CurrentSong = currentSong;
 }
 private void OnTimerTick(object sender, EventArgs e)
 {
     Responses.Status status = null;
     RunAsynchronously(() => { status = _spotifyApi.Status; }, () => Update(status));
 }
Exemplo n.º 10
0
 public void Play()
 {
     Current_Status = API.Resume;
 }
Exemplo n.º 11
0
 public void Pause()
 {
     Current_Status = API.Pause;
 }
Exemplo n.º 12
0
 public Spotify()
 {
     cfid           = API.CFID;
     Current_Status = API.Status;
 }
Exemplo n.º 13
0
 public static void CheckStatus()
 {
     _Current_Status = _API.Status;
 }
Exemplo n.º 14
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();
            }
        }