Exemple #1
0
/**
 * Print the given track title together with some trivial metadata
 *
 * @param  track   The track object
 */
void print_track(sp_track *track)
{
	int duration = sp_track_duration(track);
	char url[256];
	sp_link *l;

#if WIN32
	printf(" %s ", sp_track_is_starred(track) ? "*" : " ");
#else
	printf(" %s ", sp_track_is_starred(track) ? "★" : "☆");
#endif
	printf("Track %s [%d:%02d] has %d artist(s), %d%% popularity",
	       sp_track_name(track),
	       duration / 60000,
	       (duration / 1000) / 60,
	       sp_track_num_artists(track),
	       sp_track_popularity(track));
	
	if(sp_track_disc(track)) 
		printf(", %d on disc %d",
		       sp_track_index(track),
		       sp_track_disc(track));
	printf("\n");

	l = sp_link_create_from_track(track, 0);
	sp_link_as_string(l, url, sizeof(url));
	printf("\t\t%s\n", url);
	sp_link_release(l);
}
Exemple #2
0
/**
 * Prints the Title - Artists for each item in the search result
 */
static void print_search(sp_search *search) {
    int i;
    for (i=0; i<sp_search_num_tracks(search); i++) {
        sp_track *track = sp_search_track(search, i);
        if (track == NULL) {
            fprintf(stderr, "Search track was null.\n");
            return;
        }
        else {
            int artistBufferSize = 16;
            char* artistBuffer = malloc (artistBufferSize * sizeof(char));
            memset(artistBuffer, '\0', artistBufferSize * sizeof(char));
            int nArtists = sp_track_num_artists(track);
            int j;
            for (j=0; j<nArtists; j++) {
                sp_artist *artist = sp_track_artist(track, j);
                strcat_resize(&artistBuffer, &artistBufferSize, sp_artist_name(artist));
                if (j < nArtists - 1)
                    strcat_resize(&artistBuffer, &artistBufferSize, ",");
//                sp_artist_release(artist);
            }
            printf("\"%s\" - %s\n", sp_track_name(track), artistBuffer);
            
            free (artistBuffer);
        }
//        sp_track_release(track);
    }
}
ListResponse<TrackInfo*>* SpotifyPlaylistContainer::listTracks(
		PlaylistTask* task) {
	ListResponse<TrackInfo*>* response = new ListResponse<TrackInfo*>();
	response->setListType(ListTypeSong);
	ListPlaylistTrackInfo info = task->getCommandInfo().listPlaylistTrackInfo;
	int playlistId = info.playlist;
	if (playlistId > -1
			&& playlistId
					< sp_playlistcontainer_num_playlists(playlistContainer)) {
		bool sendName = ((info.trackInfoFlags & TrackInfoName) == TrackInfoName);
		bool sendArtist = ((info.trackInfoFlags & TrackInfoArtists)
				== TrackInfoArtists);
		bool sendAlbum = ((info.trackInfoFlags & TrackInfoAlbum)
				== TrackInfoAlbum);
		bool sendDuration = ((info.trackInfoFlags & TrackInfoDuration)
				== TrackInfoDuration);
		bool sendArtwork = ((info.trackInfoFlags & TrackInfoArtwork)
				== TrackInfoArtwork);

		sp_playlist* playlist = sp_playlistcontainer_playlist(playlistContainer,
				playlistId);
		int numTracks = sp_playlist_num_tracks(playlist);
		for (int i = 0; i < numTracks; i++) {
			TrackInfo* trackInfo = new TrackInfo();
			trackInfo->id = i;

			sp_track* track = sp_playlist_track(playlist, i);

			if (sendName) {
				trackInfo->name = sp_track_name(track);
			}
			if (sendArtist) {
				trackInfo->numArtists = sp_track_num_artists(track);
				trackInfo->artists = new const char*[trackInfo->numArtists];
				for (int j = 0; j < trackInfo->numArtists; j++) {
					trackInfo->artists[j] = sp_artist_name(
							sp_track_artist(track, j));
				}
			}
			if (sendAlbum) {
				trackInfo->album = sp_album_name(sp_track_album(track));
			}
			if (sendDuration) {
				trackInfo->duration = sp_track_duration(track);
			}
			if (sendArtwork) {
				trackInfo->artwork = sp_image_create(session,
						sp_album_cover(sp_track_album(track),
								SP_IMAGE_SIZE_NORMAL));
			}
			response->addMember(trackInfo);
		}

		return response;
	}

	delete response;
	return nullptr;
}
Exemple #4
0
QString Track::artist() const
{
  if(isValid() && sp_track_num_artists(m_spTrack)) {
    sp_artist *artist = sp_track_artist(m_spTrack, 0);
    return QString::fromUtf8(sp_artist_name(artist));
  }
  return m_ellaTrack.artistName();
}
Exemple #5
0
// ----------------------------------------------------------------------------
//
bool DMX_PLAYER_API GetTrackInfo( LPCSTR track_link, 
                                  LPSTR track_name, size_t track_name_size,
                                  LPSTR artist_name, size_t artist_name_size, 
                                  LPSTR album_name, size_t album_name_size,
                                  DWORD* track_duration_ms, bool* starred )
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());

    sp_track* track = theApp.m_spotify.linkToTrack( track_link );
    if ( !track )
        return false;

    if ( track_name ) {
        LPCSTR title = sp_track_name( track );
        if ( title == NULL )
            return false;
        errno_t err = strncpy_s( track_name, track_name_size, title, strlen(title) );
        if ( err != 0 )
            return false;
    }

    if ( artist_name ) {
        if ( sp_track_num_artists( track ) == 0 )
            *artist_name = '\0';
        else {
            sp_artist *artist = sp_track_artist( track, 0 );
            if ( artist == NULL )
                return false;
            LPCSTR title = sp_artist_name( artist );
            errno_t err = strncpy_s( artist_name, artist_name_size, title, strlen(title) );
            if ( err != 0 )
                return false;
        }
    }

    if ( album_name ) {
        sp_album *album = sp_track_album( track );
        if ( album == NULL )
            return false;
        LPCSTR title = sp_album_name( album );
        errno_t err = strncpy_s( album_name, album_name_size, title, strlen(title) );
        if ( err != 0 )
            return false;
    }

    if ( track_duration_ms )
        *track_duration_ms =  theApp.m_spotify.getTrackLength( track );
    if ( starred )
        *starred = theApp.m_spotify.isTrackStarred( track );

    return true;
}
Exemple #6
0
static PyObject *
Track_artists(Track * self, PyObject *args)
{
    sp_artist *artist;
    int count = sp_track_num_artists(self->_track);
    PyObject *l = PyList_New(count);
    int i;

    for (i = 0; i < count; i++) {
        artist = sp_track_artist(self->_track, i);
        PyObject *a = Artist_FromSpotify(artist);

        PyList_SetItem(l, i, a);
    }
    return l;
}
Exemple #7
0
void track_get_data(sp_track* track, gchar** name, gchar** artist, gchar** album, gchar** link,
                    guint* duration, int* popularity) {
    sp_artist** art = NULL;
    sp_album* alb = NULL;
    sp_link* lnk;
    int i;
    int nb_art = 0;
    const char* s;

    sp_track_add_ref(track);
    if (!sp_track_is_loaded(track)) {
        sp_track_release(track);
       return;
    }

    /* Begin loading everything */
    if (name) {
        *name = g_strdup(sp_track_name(track));
    }
    if (artist) {
        nb_art = sp_track_num_artists(track);
        art = (sp_artist**) malloc(nb_art * sizeof(sp_artist*));
        if (!art)
            g_error("Can't allocate memory.");

        for (i=0; i < nb_art; i++) {
            art[i] = sp_track_artist(track, i);
            sp_artist_add_ref(art[i]);
        }
    }
    if (album) {
        alb = sp_track_album(track);
        sp_album_add_ref(alb);
    }
    if (link) {
        GString* tmp;
        lnk = sp_link_create_from_track(track, 0);
        if (!lnk)
            g_error("Can't get URI from track.");

        tmp = g_string_sized_new(1024);
        if (sp_link_as_string(lnk, tmp->str, 1024) < 0)
            g_error("Can't render URI from link.");
        *link = tmp->str;
        g_string_free(tmp, FALSE);

        sp_link_release(lnk);
    }
    if (duration) {
        *duration = sp_track_duration(track);
    }
    if (popularity) {
        *popularity = sp_track_popularity(track);
    }

    /* Now create destination strings */
    if (artist) {
        GString* tmp = g_string_new("");
        for (i=0; i < nb_art; i++) {
            if (sp_artist_is_loaded(art[i]))
                s = sp_artist_name(art[i]);
            else
                s = "[artist not loaded]";

            if (i != 0)
                g_string_append(tmp, ", ");
            g_string_append(tmp, s);
            sp_artist_release(art[i]);
        }
        *artist = tmp->str;
        g_string_free(tmp, FALSE);
    }
    if (album) {
        if (sp_album_is_loaded(alb))
            *album = g_strdup(sp_album_name(alb));
        else
            *album = g_strdup("[album not loaded]");
        sp_album_release(alb);
    }

    sp_track_release(track);
}
Exemple #8
0
/**
 * Append the JSON representing the track to param json.
 * 
 * Returns TRUE if success - FALSE otherwise
 */
int track_to_json(sp_track* track, char** json, int* json_size, int count)
{
  int track_info_size = 256;
  char* append = malloc(track_info_size * sizeof(char));
  memset(append, '\0', track_info_size * sizeof(char));

  if (append == NULL)
  {
    fprintf(stderr, "Failed to allocate memory for track info.\n");
    return FALSE;
  }
        
  // Print track here (watch for quotes!)
  strcat(append, "{\"track_name\":\"");
  append_string_cleanse(&append, &track_info_size, sp_track_name(track));

  int j;
  int nArtists = sp_track_num_artists(track);
  // Print artists here (watch for quotes!)
  strcat_resize(&append, &track_info_size, "\",\"artists\":[\"");
  for (j=0; j<nArtists; j++)
  {
    sp_artist *artist = sp_track_artist(track, j);
    if (artist == NULL)
    {
      fprintf(stderr, "track artist retrieved was null.\n");
      if (append)
        free(append);
      return FALSE;
    }
    append_string_cleanse(&append, &track_info_size, sp_artist_name(artist));
    if (j < nArtists - 1)
      strcat_resize(&append, &track_info_size, "\",\"");
//    sp_artist_release(artist);
  }

  // Print album here (watch for quotes!)
  strcat_resize(&append, &track_info_size, "\"],\"album\":\"");
  sp_album *album = sp_track_album(track);
  append_string_cleanse(&append, &track_info_size, sp_album_name(album));
//  sp_album_release(album);

  // Print track url here (probably safe to assume there are no quotes here...)
  strcat_resize(&append, &track_info_size, "\",\"track_url\":\"");
  sp_link *l;
  char url[256];
  l = sp_link_create_from_track(track, 0);
  sp_link_as_string(l, url, sizeof(url));
  strcat_resize(&append, &track_info_size, url);
//  sp_link_release(l);
  char votes[5];
  sprintf(votes, "%d", count);
  strcat_resize(&append, &track_info_size, "\",\"votes\":\"");
  strcat_resize(&append, &track_info_size, votes);  


  strcat_resize(&append, &track_info_size, "\"}"); // close track_url quotes
  strcat_resize(json, json_size, append);

  if (append)
    free(append);
  return TRUE;
}
Exemple #9
0
	int Track::GetNumArtists()
	{
		return sp_track_num_artists( m_pTrack );
	}
Exemple #10
0
jobject createJTrackInstance(JNIEnv *env, sp_track *track)
{
    jclass jClass;
    jobject trackInstance;

    jClass = (*env)->FindClass(env, "jahspotify/media/Track");
    if (jClass == NULL)
    {
        fprintf(stderr,"jahspotify::createJTrackInstance: could not load jahnotify.media.Track\n");
        return NULL;
    }

    trackInstance = (*env)->AllocObject(env,jClass);
    if (!trackInstance)
    {
        fprintf(stderr,"jahspotify::createJTrackInstance: could not create instance of jahspotify.media.Track\n");
        return NULL;
    }

    sp_link *trackLink = sp_link_create_from_track(track,0);

    if (trackLink)
    {
        sp_link_add_ref(trackLink);
        jobject trackJLink = createJLinkInstance(env,trackLink);
        setObjectObjectField(env,trackInstance,"id","Ljahspotify/media/Link;",trackJLink);

        setObjectStringField(env,trackInstance,"title",sp_track_name(track));
        setObjectIntField(env,trackInstance,"length",sp_track_duration(track));
        setObjectIntField(env,trackInstance,"popularity",sp_track_popularity(track));
        setObjectIntField(env,trackInstance,"trackNumber",sp_track_index(track));

        sp_album *album = sp_track_album(track);
        if (album)
        {
            sp_album_add_ref(album);
            sp_link *albumLink = sp_link_create_from_album(album);
            if (albumLink)
            {
                sp_link_add_ref(albumLink);

                jobject albumJLink = createJLinkInstance(env,albumLink);

                jmethodID jMethod = (*env)->GetMethodID(env,jClass,"setAlbum","(Ljahspotify/media/Link;)V");

                if (jMethod == NULL)
                {
                    fprintf(stderr,"jahspotify::createJTrackInstance: could not load method setAlbum(link) on class Track\n");
                    return NULL;
                }

                // set it on the track
                (*env)->CallVoidMethod(env,trackInstance,jMethod,albumJLink);

                sp_link_release(albumLink);

            }

            int numArtists = sp_track_num_artists(track);
            if (numArtists > 0)
            {
                jmethodID jMethod = (*env)->GetMethodID(env,jClass,"addArtist","(Ljahspotify/media/Link;)V");

                if (jMethod == NULL)
                {
                    fprintf(stderr,"jahspotify::createJTrackInstance: could not load method addArtist(link) on class Track\n");
                    return NULL;
                }

                int i = 0;
                for (i = 0; i < numArtists; i++)
                {
                    sp_artist *artist = sp_track_artist(track,i);
                    if (artist)
                    {
                        sp_artist_add_ref(artist);

                        sp_link *artistLink = sp_link_create_from_artist(artist);
                        if (artistLink)
                        {
                            sp_link_add_ref(artistLink);

                            jobject artistJLink = createJLinkInstance(env,artistLink);

                            // set it on the track
                            (*env)->CallVoidMethod(env,trackInstance,jMethod,artistJLink);

                            sp_link_release(artistLink);

                        }
                        sp_artist_release(artist);
                    }
                }
            }

            sp_album_release(album);
        }
        sp_link_release(trackLink);
    }
    return trackInstance;
}