Exemple #1
0
/**
 * This callback is called when an attempt to login has succeeded or failed.
 *
 * @sa sp_session_callbacks#logged_in
 */
static void logged_in(sp_session *sess, sp_error error)
{
	sp_playlistcontainer *pc = sp_session_playlistcontainer(sess);
	int i;

	if (SP_ERROR_OK != error) {

		fprintf(stderr, "jukebox: Login failed: %s\n",
			sp_error_message(error));
		exit(2);
	}

	sp_playlistcontainer_add_callbacks(
		sp_session_playlistcontainer(g_sess),
		&pc_callbacks,
		NULL);
	printf("jukebox: Looking at %d playlists\n", sp_playlistcontainer_num_playlists(pc));

	for (i = 0; i < sp_playlistcontainer_num_playlists(pc); ++i) {
		sp_playlist *pl = sp_playlistcontainer_playlist(pc, i);

		sp_playlist_add_callbacks(pl, &pl_callbacks, NULL);

		if (!strcasecmp(sp_playlist_name(pl), g_listname)) {
			g_jukeboxlist = pl;
			try_jukebox_start();
		}
	}

	if (!g_jukeboxlist) {
		printf("jukebox: No such playlist. Waiting for one to pop up...\n");
		fflush(stdout);
	}
}
Exemple #2
0
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;
}
Exemple #3
0
 bool Codec::loadPlayer() {
   Logger::printOut("load player");
   if (!m_isPlayerLoaded) {
     //do we have a track at all?
     if (m_currentTrack) {
       CStdString name;
       Logger::printOut("load player 2");
       if (sp_track_is_loaded(m_currentTrack)) {
         sp_error error = sp_session_player_load(getSession(), m_currentTrack);
         CStdString message;
         Logger::printOut("load player 3");
         message.Format("%s", sp_error_message(error));
         Logger::printOut(message);
         Logger::printOut("load player 4");
         if (SP_ERROR_OK == error) {
           sp_session_player_play(getSession(), true);
           m_isPlayerLoaded = true;
           Logger::printOut("load player 5");
           return true;
         }
       }
     } else
       return false;
   }
   return true;
 }
Exemple #4
0
/*
 * 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,
		&notify_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;
}
Exemple #5
0
//
// -- Spotify callbacks ---------------------------------------------------------------------------
//
static void logged_in_callback(sp_session *session, sp_error error) {
    TRACE("logged_in_callback\n");
    struct owl_state* state = sp_session_userdata(session);

    if(error == SP_ERROR_OK) {
        state->state = OWL_STATE_IDLE;
        INFO("Logged in to Spotify!\n");
        respond_success(state->http_request);
    }
    else {
        state->state = OWL_STATE_INITIALIZED;
        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));
    }
}
/*
 * Callback that will be called by Spotify with result from login
 */
static void logged_in(sp_session *sess, sp_error error) {
  if (SP_ERROR_OK != error) {
    printf("logged_in - Unable to login: %s\n", sp_error_message(error));
  } else {
  	g_logged_in = 1;
    printf("logged_in - Success!\n");
  }
}
void Spotify::logout()
{
    sp_error err = sp_session_logout(sp);
    if (err != SP_ERROR_OK) {
        qDebug() << "Unable to logout:" << QString::fromLocal8Bit(sp_error_message(err));
    } else {
        qDebug() << "Logout request posted";
    }
}
Exemple #8
0
PyObject *handle_error(int err) {
    if(err != 0) {
        PyErr_SetString(SpotifyError, sp_error_message(err));
        return NULL;
    } else {
        Py_INCREF(Py_None);
        return Py_None;
    }
}
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);
  }
}
Exemple #10
0
/**
 * 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;
}
Exemple #11
0
int createTrackFromUri( char *uri , char *name )
{
    TRACE_2( PLAYERMANAGER , "createTrackFromUri( %s , __track__ )" , uri );

    sp_link *link;
    sp_error error;

    if( playing == FALSE && hasNextTrack() == FALSE )
        createFile( name );

    TRACE_1( PLAYERMANAGER , "Creating URI : %s" , uri );

    link = sp_link_create_from_string( uri );

    if( link == NULL )
    {
        TRACE_ERROR( PLAYERMANAGER , "Fail to create link.");

        return PC_ERROR;
    }
    else
    {
        TRACE_1( PLAYERMANAGER , "Success to create link.");
    }

    TRACE_3( PLAYERMANAGER , "Construct track...");

    currentTrack = sp_link_as_track( link );

    if( currentTrack == NULL )
    {
        TRACE_ERROR( PLAYERMANAGER , "Fail to create track.");

        return PC_ERROR;
    }
    else
    {
        TRACE_1( PLAYERMANAGER , "Success to create track.");
    }

    error = sp_track_add_ref( currentTrack );

    if( error != SP_ERROR_OK )
    {
        TRACE_ERROR( PLAYERMANAGER , "Cannot add ref track, reason: %s" , sp_error_message( error ) );

        return PC_ERROR;
    }

    sp_link_release( link );

    running = TRUE;
//    playing = FALSE;

    return PC_SUCCESS;
}
Exemple #12
0
void on_logged_in(list<int> int_params, list<string> string_params, sp_session *session, sp_track *track) {
	sp_error error = (sp_error)int_params.front();
	bool success = (SP_ERROR_OK == error) ? true : false;

	JNIEnv *env;
	jclass class_libspotify = find_class_from_native_thread(&env);

	jmethodID methodId = env->GetStaticMethodID(class_libspotify, "onLogin", "(ZLjava/lang/String;Ljava/lang/String;)V");
	log("on_logged_in: success:%s, error %s, sp_error_message(error) %s, session %s, sp_session_user_name(session) %s",
	    success?"true":"false", error==0?"null":"not null", sp_error_message(error)==0?"null":"not null",
	    session==0?"null":"not null", sp_session_user_name(session)==0?"null":"not null");
	env->CallStaticVoidMethod(class_libspotify, methodId, success, env->NewStringUTF(sp_error_message(error)),
	    env->NewStringUTF(sp_session_user_name(session)));
    if (env->ExceptionCheck()) {
        env->ExceptionDescribe();
        env->ExceptionClear();
    }
	env->DeleteLocalRef(class_libspotify);
}
Exemple #13
0
static void on_login(sp_session *session, sp_error error)
{
	debug("callback: on_login");
	if (error != SP_ERROR_OK) {
		fprintf(stderr, "Login failed: %s\n", sp_error_message(error));
		exit(2);
	}

	g_logged_in = 1;
}
/**
  loggedin
  callback from spotify
  also initilizes the playlistcontainer and callbacks
  **/
void SpotifySession::loggedIn(sp_session *session, sp_error error)
{
   SpotifySession* _session = reinterpret_cast<SpotifySession*>(sp_session_userdata(session));
    if (error == SP_ERROR_OK) {

        qDebug() << "Logged in successfully!!";

        _session->setSession(session);
        _session->setLoggedIn(true);
        _session->setPlaylistContainer( sp_session_playlistcontainer(session) );

        sp_playlistcontainer_add_ref( _session->PlaylistContainer() );
        sp_playlistcontainer_add_callbacks(_session->PlaylistContainer(), &SpotifyCallbacks::containerCallbacks, _session);
    }

    qDebug() << Q_FUNC_INFO << "==== " << sp_error_message( error ) << " ====";
    const QString msg = QString::fromUtf8( sp_error_message( error ) );
    emit _session->loginResponse( error == SP_ERROR_OK, msg );
}
Exemple #15
0
static void logged_in(sp_session *session, sp_error error)
{
	(void)session;
	if (error != SP_ERROR_OK) {
		fprintf(stderr, "error while logging in %s\n",
				sp_error_message(error));
	}
	printf("Logged in!\n");
	fflush(stdout);
}
Exemple #16
0
static sp_session *
create_session(PyObject *client, PyObject *settings)
{
    sp_session_config config;
    sp_session* session;
    sp_error error;

    memset(&config, 0, sizeof(config));
    config.api_version = SPOTIFY_API_VERSION;
    config.userdata = (void*)client;
    config.callbacks = &g_callbacks;
    config.cache_location = "";
    config.user_agent = "pyspotify-fallback";

    config_data(settings, "application_key", &config.application_key, &config.application_key_size);
    config_string(settings, "cache_location", &config.cache_location);
    config_string(settings, "settings_location", &config.settings_location);
    config_string(settings, "user_agent", &config.user_agent);
    config_string(client, "proxy", &config.proxy);
    config_string(client, "proxy_username", &config.proxy_username);
    config_string(client, "proxy_password", &config.proxy_password);

    debug_printf("cache_location = %s", config.cache_location);
    debug_printf("settings_location = %s", config.settings_location);
    debug_printf("user_agent = %s", config.user_agent);
    debug_printf("proxy = %s", config.proxy);
    debug_printf("proxy_username = %s", config.proxy_username);
    debug_printf("proxy_password = %s", config.proxy_password);
    debug_printf("application_key_size = %zu", config.application_key_size);

    if (PyErr_Occurred() != NULL) {
        return NULL;
    }

    if (strlen(config.user_agent) > 255) {
        PyErr_SetString(SpotifyError, "user_agent may not be longer than 255.");
        return NULL;
    }

    if (config.application_key_size == 0) {
        PyErr_SetString(SpotifyError, "application_key must be provided.");
        return NULL;
    }

    debug_printf("creating session...");
    /* TODO: Figure out if we should ever release the session */
    error = sp_session_create(&config, &session);
    if (error != SP_ERROR_OK) {
        PyErr_SetString(SpotifyError, sp_error_message(error));
        return NULL;
    }
    session_constructed = 1;
    g_session = session;
    return session;
}
Exemple #17
0
/**
 * Callback for libspotify
 *
 * @param browse    The browse result object that is now done
 * @param userdata  The opaque pointer given to sp_artistbrowse_create()
 */
static void search_complete(sp_search *search, void *userdata)
{
  if (sp_search_error(search) == SP_ERROR_OK)
    print_search(search);
  else
    fprintf(stderr, "Failed to search: %s\n",
            sp_error_message(sp_search_error(search)));

  sp_search_release(search);
  cmd_done();
}
Exemple #18
0
/**
 * Callback for libspotify
 *
 * @param browse    The browse result object that is now done
 * @param userdata  The opaque pointer given to sp_artistbrowse_create()
 */
static void browse_artist_callback(sp_artistbrowse *browse, void *userdata)
{
	if (sp_artistbrowse_error(browse) == SP_ERROR_OK)
		print_artistbrowse(browse);
	else
		fprintf(stderr, "Failed to browse artist: %s\n",
		        sp_error_message(sp_artistbrowse_error(browse)));

	sp_artistbrowse_release(browse);
	cmd_done();
}
Exemple #19
0
/**
 * For the session test, we simply log out as soon as we are logged in.
 */
void session_ready(sp_session *session)
{
	sp_error error = sp_session_logout(session);

	if (SP_ERROR_OK != error) {
		fprintf(stderr, "failed to log out from Spotify: %s\n",
		                sp_error_message(error));
		g_exit_code = 5;
		return;
	}
}
Exemple #20
0
PyObject *
handle_error(int err)
{
    if (err != 0) {
        PyErr_SetString(SpotifyError, sp_error_message(err));
        return NULL;
    }
    else {
        Py_RETURN_NONE;
    }
}
Exemple #21
0
void on_logged_in(list<int> int_params, list<string> string_params, sp_session *session, sp_track *track) {
	sp_error error = (sp_error)int_params.front();
	bool success = (SP_ERROR_OK == error) ? true : false;

	JNIEnv *env;
	jclass class_libspotify = find_class_from_native_thread(&env);

	jmethodID methodId = env->GetStaticMethodID(class_libspotify, "onLogin", "(ZLjava/lang/String;)V");
	env->CallStaticVoidMethod(class_libspotify, methodId, success, env->NewStringUTF(sp_error_message(error)));
	env->DeleteLocalRef(class_libspotify);
}
Exemple #22
0
  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;
  }
Exemple #23
0
static void  SP_CALLCONV logged_in(sp_session *session, sp_error error) {
	DSFYDEBUG("SESSION CALLBACK\n");
	if (SP_ERROR_OK != error) {
		fprintf(stderr, "failed to log in to Spotify: %s\n",
		                sp_error_message(error));
		g_exit_code = 4;
		return;
	}

	DSFYDEBUG("Running session_ready()\n");
	session_ready(session);
}
Exemple #24
0
int session_release(void)
{
	sp_error error;

	error = sp_session_release(g_session);
	if (error != SP_ERROR_OK) {
		fprintf(stderr, "failed to release session: %s\n",
				sp_error_message(error));
		return -1;
	}

	return 0;
}
Exemple #25
0
static void
connection_error(sp_session * session, sp_error error)
{
    debug_printf(">> connection_error called: %s", sp_error_message(error));

    PyGILState_STATE gstate = PyGILState_Ensure();
    PyObject *py_error = error_message(error);
    if (py_error != NULL) {
        session_callback(session, "connection_error", py_error);
        Py_DECREF(py_error);
    }
    PyGILState_Release(gstate);
}
Exemple #26
0
static void search_complete(sp_search *search, void *userdata)
{


        if (sp_search_error(search) == SP_ERROR_OK){
             get_search(search);
        }
        else cmd_sendresponse(put_error(400, sp_error_message(sp_search_error(search))), 400);

        sp_search_release(search);

        cmd_done();
}
void Spotify::loggedInCb(sp_session *sp, sp_error err)
{
    if (err == SP_ERROR_OK) {
        sp_playlistcontainer_add_callbacks(sp_session_playlistcontainer(sp),
                                           Spotify_Wrapper::playlistcontainerCallbacks(),
                                           this);
        eq.put(EVENT_LOGGED_IN);
    } else {
        fprintf(stderr, "Failed to login: %s\n",
                sp_error_message(err));
        eq.put(EVENT_LOGGED_OUT);
    }
}
Exemple #28
0
int session_logout(void)
{
	sp_error error;
	int retval = 0;
	if (session_is_logged_in()) {
		error = sp_session_logout(g_session);
		if (error != SP_ERROR_OK) {
			fprintf(stderr, "failed to log out %s\n",
					sp_error_message(error));
			retval = -1;
		}
	}
	return retval;
}
Exemple #29
0
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);
}
Exemple #30
0
static void logged_in(sp_session *session, sp_error error)
{
	spotify_object *p = sp_session_userdata(session);
	p->is_logged_in = 1;

	if (SP_ERROR_OK != error) {
		p->is_logged_out = 1;
		sp_session_release(session);

		char *errMsg;
		spprintf(&errMsg, 0, "login failed: %s", sp_error_message(error));
		zend_throw_exception((zend_class_entry*)zend_exception_get_default(), errMsg, 0 TSRMLS_CC);
	}
}