void login(list<int> int_params, list<string> string_params, sp_session *session, sp_track *track) { if (session == NULL) exitl("Logged in before session was initialized"); string username = string_params.front(); string password = string_params.back(); sp_session_login(session, username.c_str(), password.c_str(), true, NULL); }
int main(void) { sp_error error; sp_session *session; // create the spotify session spconfig.application_key_size = g_appkey_size; error = sp_session_create(&spconfig, &session); if (error != SP_ERROR_OK) { fprintf(stderr, "Unable to create session: %s\n", sp_error_message(error)); return 1; } // initialize audio audio_init(&g_audiofifo); // log in g_logged_in = 0; sp_session_login(session, username, password, 0, NULL); // main loop g_playing = 0; g_running = 1; int next_timeout = 0; while (g_running) { sp_session_process_events(session, &next_timeout); if (g_logged_in && !g_playing) { run_search(session); g_playing = 1; } } return 0; }
/* * Called from login_gui when the user has pushed login */ void session_login(const char *username, const char *password) { if (g_session != NULL) { int next_timeout = 0; g_logged_in = 0; sp_session_login(g_session, username, password, 0, NULL); while (!g_logged_in) { sp_session_process_events(g_session, &next_timeout); usleep(1000); } /*sp_session_login(g_session, username, password, 0, NULL); pthread_mutex_lock(&g_notify_mutex); pthread_cond_wait(&g_notify_cond, &g_notify_mutex); pthread_mutex_unlock(&g_notify_mutex); int next_timeout; do { sp_session_process_events(g_session, &next_timeout); } while (next_timeout == 0);*/ //pthread_mutex_lock(&g_notify_mutex); //pthread_cond_wait(&g_notify_cond, &g_notify_mutex); } else { printf("session_login - Session is NULL!\n"); } }
/** * creates a spotify session, and logs in with it */ int log_in(void) { /* Use the session to log in */ sp_session_login(g_session, username, password, 0, NULL); //pthread_mutex_lock(¬ify_mutex); return 1; }
sp_error session_remote_login(const char* username, const char* password) { g_debug("Logging in..."); if (!g_session) g_error("Session is not ready."); return sp_session_login(g_session, username, password, TRUE, NULL); }
static gboolean spotify_login (GstSpotSrc *spot) { sp_error error; if (GST_SPOT_SRC_LOGGED_IN (spot)) { GST_DEBUG_OBJECT (spot, "Already logged in"); return TRUE; } GST_DEBUG_OBJECT (spot, "Trying to login"); /* login using the credentials given on the command line */ error = sp_session_login (GST_SPOT_SRC_SPOTIFY_SESSION (spot), GST_SPOT_SRC_USER (spot), GST_SPOT_SRC_PASS (spot)); if (SP_ERROR_OK != error) { GST_ERROR_OBJECT (spot, "Failed to login: %s", sp_error_message (error)); return FALSE; } int timeout = -1; sp_session_process_events (GST_SPOT_SRC_SPOTIFY_SESSION (spot), &timeout); while (!GST_SPOT_SRC_LOGGED_IN (spot)) { usleep (10000); sp_session_process_events (GST_SPOT_SRC_SPOTIFY_SESSION (spot), &timeout); } GST_DEBUG_OBJECT (spot, "Login ok!"); return TRUE; }
void *start_spotify(void *arg) { printf("Spotify: Started\n"); int next_timeout = 0; sp_error err; /* Create session */ session_config.application_key_size = g_appkey_size; err = sp_session_create(&session_config, &session); if(err != SP_ERROR_OK) { fprintf(stderr, "Unable to create session: %s\n", sp_error_message(err)); return NULL; } sp_session_login(session, username, password, 1); for (;;) { if (next_timeout == 0) { while(!g_notify_do && !g_playback_done) pthread_cond_wait(&g_notify_cond, &g_notify_mutex); } else { struct timespec ts; #if _POSIX_TIMERS > 0 clock_gettime(CLOCK_REALTIME, &ts); #else struct timeval tv; gettimeofday(&tv, NULL); TIMEVAL_TO_TIMESPEC(&tv, &ts); #endif ts.tv_sec += next_timeout / 1000; ts.tv_nsec += (next_timeout % 1000) * 1000000; pthread_cond_timedwait(&g_notify_cond, &g_notify_mutex, &ts); } g_notify_do = 0; pthread_mutex_unlock(&g_notify_mutex); if (g_playback_done) { // track_ended(); g_playback_done = 0; } do { sp_session_process_events(session, &next_timeout); } while (next_timeout == 0); pthread_mutex_lock(&g_notify_mutex); } }
/** * We'll spawn off a new thread which will be the 'spotify main thread' */ static void *spotify_init_thread(void *arg) { gazify_t *gazify = (gazify_t *)arg; 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; memset(&config, 0, sizeof(config)); // 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 = "Gazeify"; // Register the callbacks. config.callbacks = &callbacks; audio_init(&g_audiofifo); error = sp_session_create(&config, &session); if (SP_ERROR_OK != error) { fprintf(stderr, "failed to create session: %s\n", sp_error_message(error)); return (void*)2; } gazify->session = session; // Login using the credentials given on the command line. sp_session_login(session, gazify->username, gazify->password); spotify_loop(arg); return NULL; }
void login(list<int> int_params, list<string> string_params, sp_session *session, sp_track *track) { if (session == NULL) exitl("Logged in before session was initialized"); string username = string_params.front(); string_params.pop_front(); string password = string_params.front(); string_params.pop_front(); string blob = string_params.front(); log ("login %s",username.c_str()); sp_error error; if (password.empty() && !blob.empty()){ error = sp_session_login(session, username.c_str(), NULL, true, blob.c_str()); } else if (!password.empty() && blob.empty()){ error = sp_session_login(session, username.c_str(), password.c_str(), true, NULL); } if (error != SP_ERROR_OK) log ("!!!login error occurred: %s",sp_error_message(error)); }
sp_error sp_session_relogin(sp_session *session) { if ( ! session->username) { return SP_ERROR_NO_CREDENTIALS; } sp_session_login(session, session->username, NULL, true, NULL); return SP_ERROR_OK; }
// // Action handler that tries to login the given user to Spotify // static void login_to_spotify_action(struct owl_state* state, char* username, char* password) { TRACE("Logging in to Spotify...\n"); state->state = OWL_STATE_LOGGING_IN; const sp_error error = sp_session_login(state->spotify_state->session, username, password, 0, NULL); if(error != SP_ERROR_OK) { ERROR("Failed to login to Spotify: %s\n", sp_error_message(error)); respond_error(state->http_request, OWL_HTTP_ERROR_LOGIN, sp_error_message(error)); } }
bool Session::connect() { if (!m_session) { sp_session_config config; Logger::printOut("Creating session"); config.api_version = SPOTIFY_API_VERSION; Logger::printOut("API version:"); char* version = new char[20]; Logger::printOut(itoa(SPOTIFY_API_VERSION, version, 10)); //the api is not copying the string so create a new c string CStdString location = Settings::getCachePath(); char * cstr; cstr = new char [location.size()+1]; strcpy (cstr, location.c_str()); config.cache_location = cstr; config.settings_location = cstr; config.tracefile = NULL; config.application_key = g_appkey; config.application_key_size = g_appkey_size; config.user_agent = "spotyXBMC2"; config.device_id = "XBMC htpc"; sp_session_callbacks cb = m_sessionCallbacks.getCallbacks(); config.callbacks = &cb; config.compress_playlists = true; config.dont_save_metadata_for_playlists = false; config.initially_unload_playlists = false; sp_error error = sp_session_create(&config, &m_session); if (SP_ERROR_OK != error) { Logger::printOut("Failed to create session: error:"); Logger::printOut(sp_error_message(error)); m_session = NULL; return false; } //set high bitrate if (Settings::useHighBitrate()) sp_session_preferred_bitrate(m_session, SP_BITRATE_320k); sp_session_set_connection_type(m_session, SP_CONNECTION_TYPE_WIRED); sp_session_set_connection_rules(m_session, SP_CONNECTION_RULE_NETWORK); sp_session_set_volume_normalization(m_session, Settings::useNormalization()); sp_session_login(m_session, Settings::getUserName().c_str(), Settings::getPassword().c_str(), true); m_isEnabled = true; Logger::printOut("Logged in, returning"); return true; } return false; }
static LRESULT CALLBACK wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { static char *username, *password; switch(uMsg) { case WM_TIMER: DSFYDEBUG("uMsg=%u (%s), wParam=%u, lParam=%u\n", uMsg, WM2STR(uMsg), wParam, lParam); if(session) { int timeout; KillTimer(hwnd, TIMERID); sp_session_process_events(session, &timeout); SetTimer(hwnd, TIMERID, timeout, NULL); } break; case WM_USER: switch(LOWORD(wParam)) { case 1: // Set tree root ID m_TreeRootId = (UINT_PTR)lParam; break; case 2: // Set username username = (char *)lParam; DSFYDEBUG("Got username '%s'\n", username); break; case 3: // Set password password = (char *)lParam; DSFYDEBUG("Got password '%s'\n", password); break; case 4: // Login if not yet logged in if(sp_session_connectionstate(session) != SP_CONNECTION_STATE_LOGGED_IN) sp_session_login(session, username, password); break; case 5: // Get connection status if(!session) return -1; else if(sp_session_connectionstate(session) != SP_CONNECTION_STATE_LOGGED_IN) return -1; return 0; } break; } return DefWindowProc(hwnd, uMsg, wParam, lParam); }
int main() { struct mg_server *server = mg_create_server(NULL, event_handler); mg_set_option(server, "Partyfy", "."); mg_set_option(server, "listening_port", "8080"); const char *username = "******"; //size_t size; char *password = getpass("Enter the password for the account\n"); sp_session *sp; spconfig.application_key_size = g_appkey_size; sp_error err = sp_session_create(&spconfig, &sp); if(SP_ERROR_OK != err) { fprintf(stderr, "Unable to create session: %s\n", sp_error_message(err)); exit(1); } g_sess = sp; pthread_mutex_init(&g_notify_mutex, NULL); pthread_cond_init(&g_notify_cond, NULL); sp_session_login(g_sess, username, password, 0, NULL); // See if the user logged in successfully printConnectionState(); int timeout = 0; sp_connectionstate state = sp_session_connectionstate(g_sess); while (state != SP_CONNECTION_STATE_LOGGED_IN) { sp_session_process_events(g_sess, &timeout); printf("Logging in...\n"); usleep(100000); state = sp_session_connectionstate(g_sess); } //audio_init(&g_audiofifo); printf("Logged in\n"); for(;;) { mg_poll_server(server, 1000); sp_session_process_events(g_sess, &timeout); //while(!g_notify_do) // pthread_cond_wait(&g_notify_cond, &g_notify_mutex); //g_notify_do = 0; //pthread_mutex_unlock(&g_notify_mutex); //if(g_playback_done) { // g_playback_done = 0; //} //pthread_mutex_lock(&g_notify_mutex); } mg_destroy_server(&server); }
Spotify::Error Spotify::Session::login( QString username, QString password ) { // Log user in: m_error = sp_session_login( m_session, username.toAscii(), password.toAscii() ); if( m_error.isError() ) { m_valid = false; } else { m_username = QString(username); m_password = QString(password); } return m_error; }
sp_error session_init(sp_session **session) { sp_error error; spconfig.application_key = &_binary_src_appkey_key_start; spconfig.application_key_size = (size_t)(&_binary_src_appkey_key_end - &_binary_src_appkey_key_start); if((error = sp_session_create(&spconfig, session)) != SP_ERROR_OK) { return error; } char *username = get_username(); char *password = get_password(); char *bitrate = get_bitrate(); sp_bitrate preferred_bitrate; if (bitrate != NULL) { if (strcmp(bitrate, "160") == 0) { preferred_bitrate = SP_BITRATE_160k; } else if (strcmp(bitrate, "320") == 0) { preferred_bitrate = SP_BITRATE_320k; } else if (strcmp(bitrate, "96") == 0) { preferred_bitrate = SP_BITRATE_96k; } else { preferred_bitrate = SP_BITRATE_160k; } } else { preferred_bitrate = SP_BITRATE_160k; } sp_session_preferred_bitrate(*session, preferred_bitrate); error = sp_session_login(*session, username, password, 0, NULL); /* * Don't keep a copy of the password in memory. */ memset(password, 0, strlen(password)); free(password); free(username); return error; }
int session_login(char *username, char *password) { sp_error error; if (g_session == NULL) return -1; error = sp_session_login(g_session, username, password, 0, NULL); if (error != SP_ERROR_OK) { fprintf(stderr, "failed to login %s\n", sp_error_message(error)); return -2; } g_is_logged_in = 1; return 0; }
void QSpotifySession::login(const QString &username, const QString &password) { qDebug() << "QSpotifySession::login"; if (!isValid() || m_isLoggedIn || m_pending_connectionRequest) return; m_pending_connectionRequest = true; emit pendingConnectionRequestChanged(); emit loggingIn(); if (password.isEmpty()) { qDebug() << "Relogin"; sp_session_relogin(m_sp_session); } else { qDebug() << "Fresh login"; sp_session_login(m_sp_session, username.toUtf8().constData(), password.toUtf8().constData(), true, NULL); } }
bool SpotWorker::start(QString username, QString password) { QByteArray cachePath, settingsPath; cachePath = settings.value("spotify/cache").toString().toLocal8Bit(); settingsPath = settings.value("spotify/settings").toString().toLocal8Bit(); sp_session_config config; config.api_version = SPOTIFY_API_VERSION; config.cache_location = strdup(cachePath.data()); config.settings_location = strdup(settingsPath.data()); config.application_key = g_appkey; config.application_key_size = g_appkey_size; config.user_agent = "libspotify-client"; config.callbacks = &g_callbacks; config.compress_playlists = false; config.dont_save_metadata_for_playlists = false; config.initially_unload_playlists = false; config.device_id = NULL; config.tracefile = NULL; sp_error error = sp_session_create(&config, ¤tSession); if (SP_ERROR_OK != error) { fprintf(stderr, "failed to create session: %s\n", sp_error_message(error)); return false; } // TODO: add callback for error checking to adjust to API changes sp_session_login(currentSession, username.toUtf8().data(), password.toUtf8().data(), true); eventTimer = new QTimer(); connect(eventTimer, SIGNAL(timeout()), SLOT(processEvents())); eventTimer->start(3000); watchDog_ = new QTimer(); connect(watchDog_, SIGNAL(timeout()), SLOT(streamingStopped())); soundSaver_ = new SoundSaver(); alsaWorker_ = new AlsaWorker(); DEBUG printf("Spotworker started with soundSaver and alsaWorker constructed\n"); return true; }
static PyObject * Session_login(PyObject *self, PyObject *args, PyObject *kwds) { char *username = NULL, *password = NULL, *blob = NULL; int remember_me = 1; sp_error error; static char *kwlist[] = {"username", "password", "remember_me", "blob", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|O&iz", kwlist, &encoded_string_converter, (void *)&username, &encoded_string_converter, (void *)&password, &remember_me, &blob)) return NULL; if (password == NULL && blob == NULL) { PyErr_SetString(SpotifyError, "one of the password or login blob " \ "is required to login"); PyMem_Free(username); return NULL; } debug_printf("login as %s in progress...", username); Py_BEGIN_ALLOW_THREADS; error = sp_session_login(Session_SP_SESSION(self), username, password, remember_me, blob); Py_END_ALLOW_THREADS; PyMem_Free(username); if (password != NULL) PyMem_Free(password); return none_or_raise_error(error); }
BOOL CALLBACK makeSpotifySession(PINIT_ONCE initOnce, PVOID param, PVOID *context) { SpotifySessionData *ssd = static_cast<SpotifySessionData *>(param); SpotifySession *ss = ssd->ss; sp_session *sess = ss->getAnyway(); pfc::string8 msg = "Enter your username and password to connect to Spotify"; while (true) { { LockedCS lock(ss->getSpotifyCS()); if (SP_ERROR_NO_CREDENTIALS == sp_session_relogin(sess)) { try { std::auto_ptr<CredPromptResult> cpr = credPrompt(msg); sp_session_login(sess, cpr->un.data(), cpr->pw.data(), cpr->save); } catch (std::exception &e) { alert(e.what()); return FALSE; } } } msg = ss->waitForLogin(ssd->p_abort); if (msg.is_empty()) return TRUE; } }
sp_error session_init(sp_session **session) { sp_error error; spconfig.application_key = &_binary_src_appkey_key_start; spconfig.application_key_size = (size_t)(&_binary_src_appkey_key_end - &_binary_src_appkey_key_start); if((error = sp_session_create(&spconfig, session)) != SP_ERROR_OK) { return error; } char *username = get_username(); char *password = get_password(); error = sp_session_login(*session, username, password, 0, NULL); /* * Don't keep a copy of the password in memory. */ memset(password, 0, strlen(password)); free(password); free(username); return error; }
int main(int argc, char **argv) { sp_session *sp; sp_error err; int next_timeout = 0; const char *username = "******"; const char *password = "******"; int opt; while ((opt = getopt(argc, argv, "g:u:")) != EOF) { switch (opt) { case 'g': g_guid = optarg; break; case 'u': g_user = optarg; break; } } if (!g_guid || !g_user) { usage(basename(argv[0])); exit(1); } spconfig.application_key_size = g_appkey_size; err = sp_session_create(&spconfig, &sp); if (SP_ERROR_OK != err) { exit(1); } g_sess = sp; pthread_mutex_init(&g_notify_mutex, NULL); pthread_cond_init(&g_notify_cond, NULL); sp_session_login(sp, username, password); for (;;) { if (next_timeout == 0) { struct timespec ts; #if _POSIX_TIMERS > 0 clock_gettime(CLOCK_REALTIME, &ts); #else struct timeval tv; gettimeofday(&tv, NULL); TIMEVAL_TO_TIMESPEC(&tv, &ts); #endif ts.tv_sec += next_timeout / 1000; ts.tv_nsec += (next_timeout % 1000) * 1000000; pthread_cond_timedwait(&g_notify_cond, &g_notify_mutex, &ts); } pthread_mutex_unlock(&g_notify_mutex); do { sp_session_process_events(sp, &next_timeout); } while (next_timeout == 0); pthread_mutex_lock(&g_notify_mutex); } return 0; }
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; }
/********************** *** Init functions *** **********************/ void session_init() { sp_error error; gchar* cache_path; g_debug("Creating session..."); /* Cache path */ cache_path = g_build_filename(g_get_user_cache_dir(), g_get_prgname(), NULL); /* libspotify session config */ sp_session_config config = { .api_version = SPOTIFY_API_VERSION, .cache_location = cache_path, .settings_location = cache_path, .application_key = g_appkey, .application_key_size = g_appkey_size, .user_agent = "spop " SPOP_VERSION, .callbacks = &g_sp_session_callbacks, .userdata = NULL, .compress_playlists = FALSE, .dont_save_metadata_for_playlists = FALSE, .initially_unload_playlists = FALSE, NULL, }; error = sp_session_create(&config, &g_session); if (error != SP_ERROR_OK) g_error("Failed to create session: %s", sp_error_message(error)); /* Set bitrate */ if (config_get_bool_opt("high_bitrate", TRUE)) { g_debug("Setting preferred bitrate to high."); sp_session_preferred_bitrate(g_session, SP_BITRATE_320k); } else { g_debug("Setting preferred bitrate to low."); sp_session_preferred_bitrate(g_session, SP_BITRATE_160k); } if (config_get_bool_opt("offline_high_bitrate", TRUE)) { g_debug("Setting preferred offline bitrate to high."); sp_session_preferred_offline_bitrate(g_session, SP_BITRATE_320k, FALSE); } else { g_debug("Setting preferred offline bitrate to low."); sp_session_preferred_offline_bitrate(g_session, SP_BITRATE_160k, FALSE); } size_t cache_size = config_get_int_opt("cache_size", 0); g_debug("Setting cache size to %zu.", cache_size); sp_session_set_cache_size(g_session, cache_size); g_debug("Session created."); } void session_login(const char* username, const char* password) { g_debug("Logging in..."); if (!g_session) g_error("Session is not ready."); sp_session_login(g_session, username, password, TRUE, NULL); }
PyObject * session_connect(PyObject *self, PyObject *args) { sp_session_config config; PyObject *client; sp_session *session; sp_error error; char *username, *password; char *cache_location, *settings_location, *user_agent; if (!PyArg_ParseTuple(args, "O", &client)) return NULL; PyEval_InitThreads(); memset(&config, 0, sizeof(config)); config.api_version = SPOTIFY_API_VERSION; config.userdata = (void *)client; config.callbacks = &g_callbacks; cache_location = PySpotify_GetConfigString(client, "cache_location"); if (!cache_location) return NULL; config.cache_location = cache_location; #ifdef DEBUG fprintf(stderr, "[DEBUG]-session- Cache location is '%s'\n", cache_location); #endif settings_location = PySpotify_GetConfigString(client, "settings_location"); config.settings_location = settings_location; #ifdef DEBUG fprintf(stderr, "[DEBUG]-session- Settings location is '%s'\n", settings_location); #endif PyObject *application_key = PyObject_GetAttr(client, PyBytes_FromString("application_key")); if (!application_key) { PyErr_SetString(SpotifyError, "application_key not set"); return NULL; } else if (!PyBytes_Check(application_key)) { PyErr_SetString(SpotifyError, "application_key must be a byte string"); return NULL; } char *s_appkey; Py_ssize_t l_appkey; PyBytes_AsStringAndSize(application_key, &s_appkey, &l_appkey); config.application_key_size = l_appkey; config.application_key = PyMem_Malloc(l_appkey); memcpy((char *)config.application_key, s_appkey, l_appkey); user_agent = PySpotify_GetConfigString(client, "user_agent"); if (!user_agent) return NULL; if (strlen(user_agent) > 255) { PyErr_SetString(SpotifyError, "user agent must be 255 characters max"); } config.user_agent = user_agent; #ifdef DEBUG fprintf(stderr, "[DEBUG]-session- User agent set to '%s'\n", user_agent); #endif username = PySpotify_GetConfigString(client, "username"); if (!username) return NULL; password = PySpotify_GetConfigString(client, "password"); if (!password) return NULL; Py_BEGIN_ALLOW_THREADS; #ifdef DEBUG fprintf(stderr, "[DEBUG]-session- creating session...\n"); #endif error = sp_session_create(&config, &session); Py_END_ALLOW_THREADS; if (error != SP_ERROR_OK) { PyErr_SetString(SpotifyError, sp_error_message(error)); return NULL; } session_constructed = 1; #ifdef DEBUG fprintf(stderr, "[DEBUG]-session- login as %s in progress...\n", username); #endif Py_BEGIN_ALLOW_THREADS; sp_session_login(session, username, password); Py_END_ALLOW_THREADS; g_session = session; Session *psession = (Session *) PyObject_CallObject((PyObject *)&SessionType, NULL); psession->_session = session; return (PyObject *)psession; }
int main(int argc, char **argv) { sp_session *sp; sp_error err; int next_timeout = 0; const char *username = NULL; const char *password = NULL; int opt; while ((opt = getopt(argc, argv, "u:p:l:d")) != EOF) { switch (opt) { case 'u': username = optarg; break; case 'p': password = optarg; break; case 'l': g_listname = optarg; break; case 'd': g_remove_tracks = 1; break; default: exit(1); } } if (!username || !password || !g_listname) { usage(basename(argv[0])); exit(1); } audio_init(&g_audiofifo); /* Create session */ spconfig.application_key_size = g_appkey_size; err = sp_session_create(&spconfig, &sp); if (SP_ERROR_OK != err) { fprintf(stderr, "Unable to create session: %s\n", sp_error_message(err)); exit(1); } g_sess = sp; pthread_mutex_init(&g_notify_mutex, NULL); pthread_cond_init(&g_notify_cond, NULL); sp_session_login(sp, username, password, 0, NULL); pthread_mutex_lock(&g_notify_mutex); for (;;) { if (next_timeout == 0) { while(!g_notify_do) pthread_cond_wait(&g_notify_cond, &g_notify_mutex); } else { struct timespec ts; #if _POSIX_TIMERS > 0 clock_gettime(CLOCK_REALTIME, &ts); #else struct timeval tv; gettimeofday(&tv, NULL); TIMEVAL_TO_TIMESPEC(&tv, &ts); #endif ts.tv_sec += next_timeout / 1000; ts.tv_nsec += (next_timeout % 1000) * 1000000; pthread_cond_timedwait(&g_notify_cond, &g_notify_mutex, &ts); } g_notify_do = 0; pthread_mutex_unlock(&g_notify_mutex); if (g_playback_done) { track_ended(); g_playback_done = 0; } do { sp_session_process_events(sp, &next_timeout); } while (next_timeout == 0); pthread_mutex_lock(&g_notify_mutex); } 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; }
int main(int argc, char** argv) { for (;;) { int option_index = 0; int c = getopt_long(argc, argv, "hl:u:p:r", NULL, &option_index); if (c == -1) { break ; } switch (c) { case 'h': usage(); break; case 'l': playlist_name = strdup(optarg); break; case 'u': user_name = strdup(optarg); break; case 'p': password = strdup(optarg); break; case 'r': reset = true; break; }; } // setup our spotify config sp_session_config config; memset(&config, 0, sizeof(config)); config.api_version = SPOTIFY_API_VERSION; config.cache_location = "/tmp/spiffify"; config.settings_location = "/tmp/spiffify"; config.application_key = g_appkey; config.application_key_size = g_appkey_size; /* mkdir(config.cache_location, 0); */ /* mkdir(config.settings_location, 0); */ config.user_agent = "spiffify"; sp_session_callbacks callbacks; memset(&callbacks, 0, sizeof(callbacks)); callbacks.logged_in = &logged_in; /* callbacks.logged_out = logged_out; */ callbacks.connection_error = &connection_error; callbacks.notify_main_thread = ¬ify_main_thread; config.callbacks = &callbacks; // setup waits pthread_mutex_init(&g_mutex, NULL); pthread_cond_init(&g_cond, NULL); sp_error error = sp_session_create(&config, &session); if (SP_ERROR_OK != error) { fprintf(stderr, "Could not create session: %s\n", sp_error_message(error)); abort(); } error = sp_session_login(session, user_name, password, 0, NULL); if (SP_ERROR_OK != error) { fprintf(stderr, "Could not initiate login: %s\n", sp_error_message(error)); abort(); } pthread_mutex_lock(&g_mutex); int next_timeout=0; int load_completed=0; int messaged_load_waiting=0; for(;;) { if (next_timeout == 0) { while(!g_notify_do) { pthread_cond_wait(&g_cond, &g_mutex); } } else { struct timespec ts; #if _POSIX_TIMERS > 0 clock_gettime(CLOCK_REALTIME, &ts); #else struct timeval tv; gettimeofday(&tv, NULL); TIMEVAL_TO_TIMESPEC(&tv, &ts); #endif pthread_cond_timedwait(&g_cond, &g_mutex, &ts); } g_notify_do = 0; pthread_mutex_unlock(&g_mutex); do { sp_session_process_events(session, &next_timeout); if (load_completed == 0 && validate_complete_load()) { if (did_spiffify == 0 ) { spiffify(); did_spiffify = 1; } sp_session_process_events(session, &next_timeout); // when the container_loaded event comes back (don't ask) we can be sure that our stuff made it to the server and we can exit there. if (can_exit==1) { goto done; } } else { if (messaged_load_waiting==0) { printf("Load not completed.. waiting\n"); messaged_load_waiting = 1; } } } while (next_timeout == 0); pthread_mutex_lock(&g_mutex); } done: error = sp_session_logout(session); if (SP_ERROR_OK != error) { fprintf(stderr, "Could not logout: %s\n", sp_error_message(error)); exit(1); } error = sp_session_release(session); if (SP_ERROR_OK != error) { fprintf(stderr, "Could not destroy session: %s\n", sp_error_message(error)); exit(1); } };
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; }