/* * Initialize the Spotify session object, called by openspotify_thread() * */ static int openspotify_init(void) { sp_session_config config; sp_error error; sp_session_callbacks callbacks = { &logged_in, &logged_out, &metadata_updated, &connection_error, NULL, ¬ify_main_thread, NULL, NULL, &log_message }; memset(&config, 0, sizeof(config)); config.api_version = SPOTIFY_API_VERSION; config.cache_location = "tmp"; config.settings_location = "tmp"; config.application_key = g_appkey; config.application_key_size = g_appkey_size; config.user_agent = "ml_openspotify"; config.callbacks = &callbacks; error = sp_session_init(&config, &session); if(error != SP_ERROR_OK) { DSFYDEBUG("sp_session_init() failed with error '%s'\n", sp_error_message(error)); return -1; } return 0; }
Spotify::Error Spotify::Session::init( const Spotify::SessionConfig& config ) { // Set callback functions: sp_session_callbacks g_callbacks = { &Spotify::Session::loggedInCallback, &Spotify::Session::loggedOutCallback, &Spotify::Session::metadataUpdatedCallback, &Spotify::Session::connectionErrorCallback, &Spotify::Session::messageToUserCallback, &Spotify::Session::notifyMainThreadCallback, &Spotify::Session::musicDeliveryCallback, &Spotify::Session::playTokenLostCallback, &Spotify::Session::logMessageCallback, &Spotify::Session::endOfTrackCallback }; // Set config: const QByteArray cacheLocation = config.cacheLocation().toUtf8(); const QByteArray settingsLocation = config.settingsLocation().toUtf8(); const QByteArray userAgent = config.userAgent().toUtf8(); m_config.api_version = SPOTIFY_API_VERSION; m_config.cache_location = cacheLocation.constData(); m_config.settings_location = settingsLocation.constData(); m_config.application_key = g_appkey; m_config.application_key_size = g_appkey_size; m_config.user_agent = userAgent.constData(); m_config.callbacks = &g_callbacks; // Initiate session: m_error = sp_session_init( &m_config, &m_session ); if( m_error.isError() ) qDebug() << "FATAL ERROR: Could not initiate session"; // Open audio device audio_init(&m_audiofifo); // Set up connections: connect( this, SIGNAL(metadataUpdated()), this, SLOT(updatePlaylistsMetadataSlot()) ); qDebug() << "Session created"; return m_error; }
static gboolean spotify_create_session (GstSpotSrc *spot) { sp_session_config config; sp_error error; config.api_version = SPOTIFY_API_VERSION; //FIXME check if these paths are appropiate config.cache_location = "tmp"; config.settings_location = "tmp"; config.application_key = g_appkey; config.application_key_size = g_appkey_size; config.user_agent = "spotify-gstreamer-src"; config.callbacks = &g_callbacks; error = sp_session_init (&config, &GST_SPOT_SRC_SPOTIFY_SESSION (spot)); if (SP_ERROR_OK != error) { GST_ERROR_OBJECT (spot, "Failed to create spotify_session: %s", sp_error_message (error)); return FALSE; } GST_DEBUG_OBJECT (spot, "Created spotify session"); return TRUE; }
int main(int argc, char **argv) { sp_session_config config; sp_error error; sp_session *session; char username[256]; char password[256]; char *ptr; if(argc == 1) { printf("Username: "******"Password: "******"Usage: browse <username> <password>\n"); return -1; } #ifdef _WIN32 g_notify_event = CreateEvent(NULL, FALSE, FALSE, NULL); #else g_main_thread = pthread_self(); signal(SIGIO, &sigIgn); #endif config.api_version = SPOTIFY_API_VERSION; config.cache_location = "tmp"; config.settings_location = "tmp"; config.application_key = g_appkey; config.application_key_size = g_appkey_size; config.user_agent = "spotify-session-example"; config.callbacks = &g_callbacks; DSFYDEBUG("Calling sp_session_init()\n"); error = sp_session_init(&config, &session); if (SP_ERROR_OK != error) { fprintf(stderr, "failed to create session: %s\n", sp_error_message(error)); return 2; } test_init(session); DSFYDEBUG("Calling sp_session_login()\n"); error = sp_session_login(session, username, password); if (SP_ERROR_OK != error) { fprintf(stderr, "failed to login: %s\n", sp_error_message(error)); return 3; } DSFYDEBUG("Calling event_loop()\n"); event_loop(session); DSFYDEBUG("Calling session_terminated()\n"); session_terminated(); return 0; }
static void impl_activate (RBPlugin *plugin, RBShell *shell) { // rb_error_dialog (NULL, _("Spotify Plugin"), "Spotify plugin activated, with shell %p", shell); RBSpotifySource *source; RhythmDBEntryType type; RhythmDB *db; char *entry_type_name, *username, *password; int err; RBSpotifyPluginPrivate *pprivate = RB_SPOTIFY_PLUGIN_GET_PRIVATE(plugin); pthread_mutex_init(&g_notify_mutex, NULL); pthread_cond_init(&g_notify_cond, NULL); audio_fifo_init(&g_audio_fifo); spconfig.application_key_size = g_appkey_size; err = sp_session_init(&spconfig, &pprivate->sess); if (err != SP_ERROR_OK) { rb_error_dialog (NULL, _("Spotify Plugin"), "Error initialising spotify session"); pprivate->sess = NULL; return; } fprintf(stderr, "err: %x", err); err = pthread_create(&pprivate->notify_thread, 0, notification_routine, pprivate->sess); fprintf(stderr, "Thread created"); if (err != 0) { fprintf(stderr, "Error creating notification thread %x\n", err); return; } username = eel_gconf_get_string (CONF_SPOTIFY_USERNAME); password = eel_gconf_get_string (CONF_SPOTIFY_PASSWORD); if (username == NULL || password == NULL) { rb_error_dialog (NULL, _("Spotify Plugin"), "Username and password not set."); return; } err = sp_session_login(pprivate->sess, username, password); fprintf(stderr, "err: %x", err); rbspotifysrc_set_plugin(plugin); g_object_get (shell, "db", &db, NULL); entry_type_name = g_strdup_printf ("spotify"); type = rhythmdb_entry_register_type (db, entry_type_name); g_free (entry_type_name); type->save_to_disk = FALSE; type->category = RHYTHMDB_ENTRY_NORMAL; // type->get_playback_uri = (RhythmDBEntryStringFunc) rb_daap_source_get_playback_uri; g_object_unref (db); // icon = rb_daap_plugin_get_icon (RB_DAAP_PLUGIN (plugin), password_protected, FALSE); source = (RBSpotifySource*)RB_SOURCE (g_object_new (RBSPOTIFYSOURCE_TYPE, "name", "spotify", "entry-type", type, "shell", shell, "visibility", TRUE, // "sorting-key", CONF_STATE_SORTING, "source-group", RB_SOURCE_GROUP_SHARED, "plugin", RB_PLUGIN (plugin), NULL)); source->priv->sess = pprivate->sess; source->priv->db = db; source->priv->type = type; rb_shell_register_entry_type_for_source (shell, (RBSource*)source, type); rb_shell_append_source (shell, (RBSource*)source, NULL); // return source; }
PyObject *session_connect(PyObject *self, PyObject *args) { #ifdef DEBUG fprintf(stderr, "> entering session_connect\n"); #endif sp_session_config config; PyObject *client; sp_session *session; sp_error error; PyObject *uobj, *pobj; char *username, *password; if(!PyArg_ParseTuple(args, "O", &client)) return NULL; PyEval_InitThreads(); config.api_version = SPOTIFY_API_VERSION; config.userdata = (void *)client; config.callbacks = &g_callbacks; config.user_agent = "unset"; #ifdef DEBUG fprintf(stderr, "Config mark 1\n"); #endif PyObject *cache_location = PyObject_GetAttr(client, PyString_FromString("cache_location")); #ifdef DEBUG fprintf(stderr, "Cache location is '%s'\n", PyString_AsString(cache_location)); #endif if(cache_location == NULL) { PyErr_SetString(SpotifyError, "Client did not provide a cache_location"); return NULL; } #ifdef DEBUG fprintf(stderr, "Config mark 1.1\n"); #endif config.cache_location = copystring(cache_location); #ifdef DEBUG fprintf(stderr, "Config mark 2\n"); #endif PyObject *settings_location = PyObject_GetAttr(client, PyString_FromString("settings_location")); if(settings_location == NULL) { PyErr_SetString(SpotifyError, "Client did not provide a settings_location"); return NULL; } config.settings_location = copystring(settings_location); #ifdef DEBUG fprintf(stderr, "Config mark 3\n"); #endif PyObject *application_key = PyObject_GetAttr(client, PyString_FromString("application_key")); if(application_key == NULL) { PyErr_SetString(SpotifyError, "Client did not provide an application_key"); return NULL; } char *s_appkey; Py_ssize_t l_appkey; PyString_AsStringAndSize(application_key, &s_appkey, &l_appkey); config.application_key_size = l_appkey; config.application_key = PyMem_Malloc(l_appkey); memcpy(config.application_key, s_appkey, l_appkey); #ifdef DEBUG fprintf(stderr, "Config mark 4\n"); #endif PyObject *user_agent = PyObject_GetAttr(client, PyString_FromString("user_agent")); if(user_agent == NULL) { PyErr_SetString(SpotifyError, "Client did not provide a user_agent"); return NULL; } config.user_agent = copystring(user_agent); #ifdef DEBUG fprintf(stderr, "Config mark 5\n"); #endif uobj = PyObject_GetAttr(client, PyString_FromString("username")); if(uobj == NULL) { PyErr_SetString(SpotifyError, "Client did not provide a username"); return NULL; } username = copystring(uobj); pobj = PyObject_GetAttr(client, PyString_FromString("password")); if(pobj == NULL) { PyErr_SetString(SpotifyError, "Client did not provide a password"); return NULL; } password = copystring(pobj); Py_BEGIN_ALLOW_THREADS #ifdef DEBUG fprintf(stderr, "Calling sp_session_init\n"); #endif error = sp_session_init(&config, &session); #ifdef DEBUG fprintf(stderr, "Returned from sp_session_init\n"); #endif Py_END_ALLOW_THREADS session_constructed = 1; if(error != SP_ERROR_OK) { PyErr_SetString(SpotifyError, sp_error_message(error)); return NULL; } Py_BEGIN_ALLOW_THREADS error = sp_session_login(session, username, password); Py_END_ALLOW_THREADS if(error != SP_ERROR_OK) { PyErr_SetString(SpotifyError, sp_error_message(error)); return NULL; } Session *psession = (Session *)PyObject_CallObject((PyObject *)&SessionType, NULL); Py_INCREF(psession); psession->_session = session; return (PyObject *)psession; }
void sess_init(struct event_base *evbase) { g_session.evbase = evbase; g_session.spot_ev = evtimer_new(evbase, sess_event_cb, &g_session); g_session.stop_ev = evtimer_new(evbase, sess_stop_cb, &g_session); g_session.exiting = false; g_session.current_track = NULL; sp_session_config config = { .api_version = SPOTIFY_API_VERSION, .cache_location = "tmp", .settings_location = "tmp", .application_key = g_appkey, .application_key_size = g_appkey_size, .user_agent = POLSPOT_USER_AGENT, .callbacks = &g_callbacks, .userdata = &g_session }; sp_error err = sp_session_init(&config, &g_session.spotify); if (err != SP_ERROR_OK) panic("sp_session_init() failed: %s", sp_error_message(err)); } void sess_cleanup() { // If user is logged in then initiate logout. This will change the session // state to SESS_DISCONNECTING. Another call to this function will thus exit // the main loop without waiting for the logout process to finish, allowing // users to avoid waiting for timeouts by issuing the quit commant twice. if (g_session.state == SESS_ONLINE) { sess_disconnect(); g_session.exiting = true; } else { free(g_session.username); g_session.username = 0; free(g_session.password); g_session.password = 0; // Free search results. for (sess_search_t *s = g_session.search; s;) { sp_search_release(s->res); sess_search_t *p = s; s = s->next; free(p); } g_session.current_track = NULL; event_free(g_session.spot_ev); // Exit main event loop. event_base_loopbreak(g_session.evbase); } } void sess_connect() { assert(g_session.username && g_session.password); sess_disconnect(); // Login with credentials set by sess_username/sess_password. sp_error err = sp_session_login(g_session.spotify, g_session.username, g_session.password); if (err != SP_ERROR_OK) panic("sp_session_login() failed: %s", sp_error_message(err)); log_append("Connecting..."); // Redraw status info. g_session.state = SESS_CONNECTING; ui_dirty(UI_FOOTER); ui_update_post(0); }
//-------------------------------------------------------- // init the session //-------------------------------------------------------- bool TLSpotify::TSession::Initialise(TRef& ErrorRef) { // init session if ( !m_pSession ) { sp_session_config config; config.api_version = SPOTIFY_API_VERSION; // The path of the directory to store the cache. This must be specified. // Please read the documentation on preferred values. config.cache_location = "tmp"; // The path of the directory to store the settings. This must be specified. // Please read the documentation on preferred values. config.settings_location = "tmp"; // The key of the application. They are generated by Spotify, // and are specific to each application using libspotify. config.application_key = m_Key.GetData(); config.application_key_size = m_Key.GetSize(); // This identifies the application using some // free-text string [1, 255] characters. TFixedArray<char,255> AgentString; m_AgentName.GetAnsi( AgentString ); config.user_agent = AgentString.GetData(); // Register the callbacks. config.callbacks = &g_Callbacks; // https://developer.spotify.com/en/libspotify/docs/group__session.html // In the future, this will be renamed to sp_session_create() and will have a corresponding sp_session_release() function. sp_error Error = sp_session_init( &config, &m_pSession ); if ( Error != SP_ERROR_OK ) { //sp_error_message(error)); ErrorRef = "Init"; Shutdown(); return false; } // where is the session? if ( !m_pSession ) { ErrorRef = "NoSession"; Shutdown(); return false; } } // log in session if ( !IsLoggedIn() ) { TFixedArray<char,255> UserNameString; m_UserName.GetAnsi( UserNameString ); TFixedArray<char,255> PasswordString; m_Password.GetAnsi( PasswordString ); // attempt login sp_error Error = sp_session_login( m_pSession, UserNameString.GetData(), PasswordString.GetData() ); if ( Error != SP_ERROR_OK ) { ErrorRef = "Login"; Shutdown(); return false; } // logged in! fetch the user m_pUser = sp_session_user( m_pSession ); if ( !m_pUser ) { ErrorRef = "NoUser"; // set pointer to something so we attempt to logout, even though it's not valid m_pUser = (sp_user*)0xdeadf00d; Shutdown(); return false; } } return true; }
/** * Added note: The callbacks for the playlist-handling are set * by logged_in_playlist(), for each playlist in the * playlist container. * * The callbacks for the playlist-container-handling are set * by main(). * */ int spshell_init(const char *username, const char *password) { sp_session_config config; sp_error error; sp_session *session; /// The application key is specific to each project, and allows Spotify /// to produce statistics on how our service is used. extern const char g_appkey[]; /// The size of the application key. extern const size_t g_appkey_size; // Always do this. It allows libspotify to check for // header/library inconsistencies. config.api_version = SPOTIFY_API_VERSION; // The path of the directory to store the cache. This must be specified. // Please read the documentation on preferred values. config.cache_location = "tmp"; // The path of the directory to store the settings. // This must be specified. // Please read the documentation on preferred values. config.settings_location = "tmp"; // The key of the application. They are generated by Spotify, // and are specific to each application using libspotify. config.application_key = g_appkey; config.application_key_size = g_appkey_size; // This identifies the application using some // free-text string [1, 255] characters. config.user_agent = "listify"; // Register the callbacks. config.callbacks = &callbacks; error = sp_session_init(&config, &session); if (SP_ERROR_OK != error) { fprintf(stderr, "failed to create session: %s\n", sp_error_message(error)); return 2; } // Added Code: Adding the callbacks for the playlist-container sp_playlistcontainer_add_callbacks( sp_session_playlistcontainer(session), &pc_callbacks, NULL); // Login using the credentials given on the command line. error = sp_session_login(session, username, password); if (SP_ERROR_OK != error) { fprintf(stderr, "failed to login: %s\n", sp_error_message(error)); return 3; } g_session = session; return 0; }