public Controls(SpotSharp.SpotSharp spotify) { Post["/play"] = x => { spotify.Unpause(); return HttpStatusCode.OK; }; Post["/pause"] = x => { spotify.Pause(); return HttpStatusCode.OK; }; Post["/next"] = x => { spotify.PlayNextTrack(); return HttpStatusCode.OK; }; Post["/prev"] = x => { spotify.PlayPreviousTrack(); return HttpStatusCode.OK; }; }
public Playlists(SpotSharp.SpotSharp spotify) { Get["/playlists"] = x => { var playlists = spotify.GetAllPlaylists(); var response = (Response)JsonConvert.SerializeObject(playlists, new LinkJsonConverter()); response.ContentType = "application/json"; return response; }; }
public Search(SpotSharp.SpotSharp spotify) { Post["/search"] = x => { var searchTerms = this.Bind<SearchQueryModel>(); var tracks = spotify.SearchForTracks(searchTerms.Query); var response = (Response)JsonConvert.SerializeObject(tracks, new LinkJsonConverter()); response.ContentType = "application/json"; return response; }; }
public Session(SpotSharp spotify, byte[] appkey) { _spotify = spotify; _waveOutDevice = new WaveOutEvent { DesiredLatency = 200 }; playbackStoppedHandler = (sender, args) => { _logger.InfoFormat("Track Playback Stopped: {0}", args.Exception); if (endOfTrackCallback != null) { endOfTrackCallback(); } }; var callbacksPtr = AddCallbacks(); var config = new libspotify.sp_session_config { api_version = libspotify.SPOTIFY_API_VERSION, user_agent = "Spotbox", application_key_size = appkey.Length, application_key = Marshal.AllocHGlobal(appkey.Length), cache_location = Path.Combine(Path.GetTempPath(), "spotify_api_temp"), settings_location = Path.Combine(Path.GetTempPath(), "spotify_api_temp"), callbacks = callbacksPtr, compress_playlists = true, dont_save_metadata_for_playlists = false, initially_unload_playlists = false }; _logger.DebugFormat("api_version={0}", config.api_version); _logger.DebugFormat("application_key_size={0}", config.application_key_size); _logger.DebugFormat("cache_location={0}", config.cache_location); _logger.DebugFormat("settings_location={0}", config.settings_location); Marshal.Copy(appkey, 0, config.application_key, appkey.Length); IntPtr sessionPtr; var err = libspotify.sp_session_create(ref config, out sessionPtr); if (err != libspotify.sp_error.OK) { throw new ApplicationException(libspotify.sp_error_message(err)); } SessionPtr = sessionPtr; libspotify.sp_session_set_connection_type(sessionPtr, libspotify.sp_connection_type.SP_CONNECTION_TYPE_WIRED); _waveProvider = new HaltableBufferedWaveProvider(waveFormat); _waveOutDevice.Init(_waveProvider); _waveOutDevice.PlaybackStopped += playbackStoppedHandler; }
public Playlist(SpotSharp.SpotSharp spotify) { Get["/playlist"] = x => { var playlist = spotify.GetCurrentPlaylist(); var response = (Response)JsonConvert.SerializeObject(playlist, new LinkJsonConverter()); response.ContentType = "application/json"; return response; }; Post["/playlist"] = x => { var linkModel = this.Bind<LinkModel>(); if (linkModel.Link != null) { var trackLink = Link.Create(linkModel.Link); if (trackLink != null && trackLink.LinkType == LinkType.Track) { var trackAdded = spotify.GetCurrentPlaylist().AddTrack(trackLink); if (trackAdded) { return HttpStatusCode.NoContent; } } } return HttpStatusCode.BadRequest; }; Put["/playlist"] = x => { var linkModel = this.Bind<LinkModel>(); if (linkModel.Link != null) { var playlistLink = Link.Create(linkModel.Link); if (playlistLink != null && playlistLink.LinkType == LinkType.Playlist) { var playlistSet = spotify.SetCurrentPlaylist(playlistLink); if (playlistSet) { spotify.Play(); return HttpStatusCode.OK; } } } return HttpStatusCode.BadRequest; }; }
public Speak(SpotSharp.SpotSharp spotify) { Post["/speak"] = x => { spotify.Pause(); var speach = this.Bind<LinkModel>(); var synthesizer = new SpeechSynthesizer { Volume = 100, Rate = -2 }; synthesizer.Speak(speach.Link); spotify.Play(); return HttpStatusCode.OK; }; }
public Playing(SpotSharp.SpotSharp spotify) { Get["/playing"] = x => { var track = spotify.GetCurrentTrack(); var response = (Response) JsonConvert.SerializeObject(track, new LinkJsonConverter()); response.ContentType = "application/json"; return response; }; Get["/playing/cover.jpeg"] = x => { var track = spotify.GetCurrentTrack(); if (track != null) { var imageBytes = track.GetAlbumArt(); return new ByteArrayResponse(imageBytes, "image/jpeg"); } return new ByteArrayResponse(new byte[0], "image/jpeg"); }; }
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines) { var spotifyApiKey = File.ReadAllBytes(ConfigurationManager.AppSettings["SpotifyApiKeyPath"]); var spotifyUsername = ConfigurationManager.AppSettings["SpotifyUsername"]; var spotifyPassword = ConfigurationManager.AppSettings["SpotifyPassword"]; var spotify = new SpotSharp.SpotSharp(spotifyApiKey); spotify.Login(spotifyUsername, spotifyPassword); spotify.TrackChanged = BroadcastTrackChange; spotify.PlaylistChanged = SavePlaylistPositionToSettings; // for api modules container.Register(spotify); // for application startup TinyIoCContainer.Current.Register(spotify); pipelines.BeforeRequest += (ctx) => { _logger.InfoFormat("Received request {0} {1} by {2}", ctx.Request.Method, ctx.Request.Path, ctx.Request.UserHostAddress); return null; }; }