JNIEXPORT jobject JNICALL Java_jahspotify_impl_JahSpotifyImpl_retrieveArtist ( JNIEnv *env, jobject obj, jstring uri) { jobject artistInstance; uint8_t *nativeUri = NULL; nativeUri = ( uint8_t * ) ( *env )->GetStringUTFChars ( env, uri, NULL ); sp_link *link = sp_link_create_from_string(nativeUri); if (link) { sp_artist *artist= sp_link_as_artist(link); if (artist) { sp_artist_add_ref(artist); artistInstance = createJArtistInstance(env, artist); sp_artist_release(artist); } sp_link_release(link); } if (nativeUri) (*env)->ReleaseStringUTFChars(env, uri,nativeUri); return artistInstance; }
bool SpotifySong::updateMetadata() { if(sp_track_is_loaded(this->track)) { this->duration = sp_track_duration(this->track); this->title = sp_track_name(this->track); if(this->artist == nullptr) { this->artist = sp_track_artist(this->track,0); sp_artist_add_ref(this->artist); } if(this->album == nullptr) { this->album = sp_track_album(this->track); sp_album_add_ref(this->album); } } else return false; if(this->artist != nullptr && sp_artist_is_loaded(this->artist)) this->artistName = sp_artist_name(this->artist); else return false; if(this->album != nullptr && sp_album_is_loaded(this->album)) this->albumName = sp_album_name(this->album); else return false; return true; }
PHP_METHOD(SpotifyArtist, __construct) { zval *object = getThis(); zval *parent; sp_artist *artist; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Oz", &parent, spotify_ce, &artist) == FAILURE) { return; } spotify_object *p = (spotify_object*)zend_object_store_get_object((parent) TSRMLS_CC); spotifyartist_object *obj = (spotifyartist_object*)zend_object_store_get_object(object TSRMLS_CC); obj->session = p->session; obj->artist = artist; zend_update_property(spotifyartist_ce, getThis(), "spotify", strlen("spotify"), parent TSRMLS_CC); sp_artist_add_ref(obj->artist); }
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); }
void Artist::Load( sp_artist* pArtist ) { m_pArtist = pArtist; sp_artist_add_ref( m_pArtist ); }
jobject createJArtistInstance(JNIEnv *env, sp_artist *artist) { jclass jClass; jobject artistInstance = createInstance(env,"jahspotify/media/Artist"); sp_link *artistLink = sp_link_create_from_artist(artist); jClass = (*env)->FindClass(env, "jahspotify/media/Artist"); if (jClass == NULL) { fprintf(stderr,"jahspotify::createJArtistInstance: could not load jahnotify.media.Artist\n"); return NULL; } if (artistLink) { sp_link_add_ref(artistLink); jobject artistJLink = createJLinkInstance(env, artistLink); setObjectObjectField(env,artistInstance,"id","Ljahspotify/media/Link;",artistJLink); sp_link_release(artistLink); setObjectStringField(env,artistInstance,"name",sp_artist_name(artist)); sp_artistbrowse *artistBrowse = sp_artistbrowse_create(g_sess,artist,artistBrowseCompleteCallback,NULL); if (artistBrowse) { sp_artistbrowse_add_ref(artistBrowse); while (!sp_artistbrowse_is_loaded(artistBrowse)) { usleep(100); } int numSimilarArtists = sp_artistbrowse_num_similar_artists(artistBrowse); if (numSimilarArtists > 0) { jmethodID jMethod = (*env)->GetMethodID(env,jClass,"addSimilarArtist","(Ljahspotify/media/Link;)V"); if (jMethod == NULL) { fprintf(stderr,"jahspotify::createJTrackInstance: could not load method setAlbum(link) on class Track\n"); return NULL; } // Load the artist links int count = 0; for (count = 0; count < numSimilarArtists; count++) { sp_artist *similarArtist = sp_artistbrowse_similar_artist(artistBrowse,0); if (similarArtist) { sp_artist_add_ref(similarArtist); sp_link *similarArtistLink = sp_link_create_from_artist(similarArtist); if (similarArtistLink) { sp_link_add_ref(similarArtistLink); jobject similarArtistJLink = createJLinkInstance(env,similarArtistLink); // set it on the track (*env)->CallVoidMethod(env,artistInstance,jMethod,similarArtistJLink); sp_link_release(similarArtistLink); } sp_artist_release(similarArtist); } } } int numPortraits = sp_artistbrowse_num_portraits(artistBrowse); if (numPortraits > 0) { // Load portrait url const byte *portraitURI = sp_artistbrowse_portrait(artistBrowse,0); if (portraitURI) { char *portraitURIStr = toHexString((byte*)portraitURI); const char spotifyURI[] = "spotify:image:"; int len = strlen(spotifyURI) + strlen(portraitURIStr); char *dest = malloc(len+1); dest[0] = 0; strcat(dest,spotifyURI); strcat(dest,portraitURIStr); sp_link *portraitLink = sp_link_create_from_string(dest); sp_image *portrait = sp_image_create_from_link(g_sess,portraitLink); if (portrait) { sp_image_add_ref(portrait); sp_image_add_load_callback(portrait,imageLoadedCallback,NULL); } free(dest); free(portraitURIStr); if (portraitLink) { sp_link_add_ref(portraitLink); jobject portraitJLlink = createJLinkInstance(env,portraitLink); setObjectObjectField(env,artistInstance,"portrait","Ljahspotify/media/Link;",portraitJLlink); sp_link_release(portraitLink); } } } // sp_artistbrowse_num_albums() // sp_artistbrowse_album() const char *bios = sp_artistbrowse_biography(artistBrowse); if (bios) { setObjectStringField(env,artistInstance,"bios",bios); } // sp_artistbrowse_num_tracks() // sp_artistbrowse_track() sp_artistbrowse_release(artistBrowse); } } return artistInstance; }
jobject createJAlbumInstance(JNIEnv *env, sp_album *album) { jclass albumJClass; jobject albumInstance; albumJClass = (*env)->FindClass(env, "jahspotify/media/Album"); if (albumJClass == NULL) { fprintf(stderr,"jahspotify::createJAlbumInstance: could not load jahnotify.media.Album\n"); return NULL; } albumInstance = (*env)->AllocObject(env,albumJClass); if (!albumInstance) { fprintf(stderr,"jahspotify::createJAlbumInstance: could not create instance of jahspotify.media.Album\n"); return NULL; } sp_albumbrowse *albumBrowse = sp_albumbrowse_create(g_sess,album,albumBrowseCompleteCallback,NULL); if (albumBrowse) { sp_albumbrowse_add_ref(albumBrowse); int count = 0; while (!sp_albumbrowse_is_loaded(albumBrowse) && count < 5) { fprintf(stderr,"jahspotify::createJAlbumInstance: waiting for album browse load to complete\n"); sleep(1); count++; } if (count == 5) { sp_albumbrowse_release(albumBrowse); return NULL; } // By now it looks like the album will also be loaded sp_link *albumLink = sp_link_create_from_album(album); if (albumLink) { sp_link_add_ref(albumLink); jobject albumJLink = createJLinkInstance(env, albumLink); setObjectObjectField(env,albumInstance,"id","Ljahspotify/media/Link;",albumJLink); setObjectStringField(env,albumInstance,"name",sp_album_name(album)); setObjectIntField(env,albumInstance,"year",sp_album_year(album)); sp_albumtype albumType = sp_album_type(album); jclass albumTypeJClass = (*env)->FindClass(env, "jahspotify/media/AlbumType"); jmethodID jMethod = (*env)->GetStaticMethodID(env,albumTypeJClass,"fromOrdinal","(I)Ljahspotify/media/AlbumType;"); jobject albumTypeEnum = (jobjectArray)(*env)->CallStaticObjectMethod(env, albumTypeJClass, jMethod,(int)albumType); setObjectObjectField(env,albumInstance,"type","Ljahspotify/media/AlbumType;",albumTypeEnum); sp_link *albumCoverLink = sp_link_create_from_album_cover(album); if (albumCoverLink) { sp_link_add_ref(albumCoverLink); jobject albumCoverJLink = createJLinkInstance(env, albumCoverLink); setObjectObjectField(env,albumInstance,"cover","Ljahspotify/media/Link;",albumCoverJLink); sp_image *albumCoverImage = sp_image_create_from_link(g_sess,albumCoverLink); if (albumCoverImage) { sp_image_add_ref(albumCoverImage); sp_image_add_load_callback(albumCoverImage,imageLoadedCallback,NULL); } sp_link_release(albumCoverLink); } sp_artist *artist = sp_album_artist(album); 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); setObjectObjectField(env,albumInstance,"artist","Ljahspotify/media/Link;",artistJLink); sp_link_release(artistLink); } sp_artist_release(artist); } sp_link_release(albumLink); } int numTracks = sp_albumbrowse_num_tracks(albumBrowse); if (numTracks > 0) { // Add each track to the album - also pass in the disk as need be jmethodID addTrackJMethodID = (*env)->GetMethodID(env,albumJClass,"addTrack","(ILjahspotify/media/Link;)V"); int i = 0; for (i = 0; i < numTracks; i++) { sp_track *track = sp_albumbrowse_track(albumBrowse,i); if (track) { sp_track_add_ref(track); sp_link *trackLink = sp_link_create_from_track(track,0); if (trackLink) { sp_link_add_ref(trackLink); jobject trackJLink = createJLinkInstance(env,trackLink); (*env)->CallVoidMethod(env, albumInstance, addTrackJMethodID,sp_track_disc(track),trackJLink); sp_link_release(trackLink); } } } } int numCopyrights = sp_albumbrowse_num_copyrights(albumBrowse); if (numCopyrights > 0) { // Add copyrights to album jmethodID addCopyrightMethodID = (*env)->GetMethodID(env,albumJClass,"addCopyright","(Ljava/lang/String;)V"); int i = 0; for (i = 0; i < numCopyrights; i++) { const char *copyright = sp_albumbrowse_copyright(albumBrowse,i); if (copyright) { jstring str = (*env)->NewStringUTF(env, copyright); (*env)->CallVoidMethod(env, albumInstance, addCopyrightMethodID,str); } } } const char *review = sp_albumbrowse_review(albumBrowse); if (review) { setObjectStringField(env,albumInstance,"review",review); } sp_albumbrowse_release(albumBrowse); } return albumInstance; }
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; }
SP_LIBEXPORT(sp_artistbrowse *) sp_artistbrowse_create(sp_session *session, sp_artist *artist, artistbrowse_complete_cb *callback, void *userdata) { sp_artistbrowse *arb; void **container; struct browse_callback_ctx *brctx; arb = malloc(sizeof(sp_artistbrowse)); DSFYDEBUG("Allocated artistbrowse at %p\n", arb); if(arb == NULL) return NULL; arb->artist = artist; sp_artist_add_ref(artist); DSFYDEBUG("Referenced artist at %p\n", arb->artist); arb->num_tracks = 0; arb->tracks = NULL; arb->num_portraits = 0; arb->portraits = 0; arb->num_similar_artists = 0; arb->similar_artists = NULL; arb->num_albums = 0; arb->albums = NULL; arb->biography = NULL; arb->callback = callback; arb->userdata = userdata; arb->error = SP_ERROR_IS_LOADING; arb->is_loaded = 0; arb->ref_count = 1; /* * Temporarily increase ref count for the albumbrowse so it's not free'd * accidentily. It will be decreaed by the chanel callback. * */ sp_artistbrowse_add_ref(arb); /* The album callback context */ brctx = (struct browse_callback_ctx *)malloc(sizeof(struct browse_callback_ctx)); brctx->session = session; brctx->req = NULL; /* Filled in by the request processor */ brctx->buf = NULL; /* Filled in by the request processor */ brctx->type = REQ_TYPE_ARTISTBROWSE; brctx->data.artistbrowses = (sp_artistbrowse **)malloc(sizeof(sp_artistbrowse *)); brctx->data.artistbrowses[0] = arb; brctx->num_total = 1; brctx->num_browsed = 0; brctx->num_in_request = 0; /* Our gzip'd XML parser */ brctx->browse_parser = osfy_artistbrowse_browse_callback; /* Request input container. Will be free'd when the request is finished. */ container = (void **)malloc(sizeof(void *)); *container = brctx; request_post(session, REQ_TYPE_ARTISTBROWSE, container); return arb; }
static int osfy_artistbrowse_load_from_xml(sp_session *session, sp_artistbrowse *arb, ezxml_t root) { unsigned char id[20]; int disc_number, i; sp_track *track; sp_album *album; ezxml_t node, album_node, loop_node, track_node; /* Load artist from XML if not yet loaded */ if(sp_artist_is_loaded(arb->artist) == 0) osfy_artist_load_artist_from_xml(session, arb->artist, root); assert(sp_artist_is_loaded(arb->artist)); /* Load portraits */ for(loop_node = ezxml_get(root, "bios", 0, "bio", 0, "portraits", 0, "portrait", -1); loop_node; loop_node = loop_node->next) { if((node = ezxml_get(loop_node, "id", -1)) == NULL) continue; arb->portraits = realloc(arb->portraits, sizeof(unsigned char *) * (1 + arb->num_portraits)); arb->portraits[arb->num_portraits] = malloc(20); hex_ascii_to_bytes(node->txt, arb->portraits[arb->num_portraits], 20); arb->num_portraits++; } /* Load biography */ if((node = ezxml_get(root, "bios", 0, "bio", 0, "text", -1)) != NULL) arb->biography = strdup(node->txt); else arb->biography = strdup(""); /* Load similar artists */ for(loop_node = ezxml_get(root, "similar-artists", 0, "artist", -1); loop_node; loop_node = loop_node->next) { if((node = ezxml_get(loop_node, "id", -1)) == NULL) continue; arb->similar_artists = realloc(arb->similar_artists, sizeof(sp_artist *) * (1 + arb->num_similar_artists)); hex_ascii_to_bytes(node->txt, id, 16); arb->similar_artists[arb->num_similar_artists] = osfy_artist_add(session, id); sp_artist_add_ref(arb->similar_artists[arb->num_similar_artists]); if(sp_artist_is_loaded(arb->similar_artists[arb->num_similar_artists]) == 0) { DSFYDEBUG("Loading similar artist from artistbrowse XML\n"); osfy_artist_load_artist_from_xml(session, arb->similar_artists[arb->num_similar_artists], loop_node); } assert(sp_artist_is_loaded(arb->similar_artists[arb->num_similar_artists])); arb->num_similar_artists++; } /* Loop over each album listed */ for(album_node = ezxml_get(root, "albums", 0, "album", -1); album_node; album_node = album_node->next) { /* Extract album ID and add it */ if((node = ezxml_get(album_node, "id", -1)) == NULL) continue; hex_ascii_to_bytes(node->txt, id, 16); album = sp_album_add(session, id); /* Load album if necessary */ if(sp_album_is_loaded(album) == 0) osfy_album_load_from_album_xml(session, album, album_node); assert(sp_album_is_loaded(album)); /* Add album to artistbrowse's list of albums */ arb->albums = realloc(arb->albums, sizeof(sp_album *) * (1 + arb->num_albums)); arb->albums[arb->num_albums] = album; sp_album_add_ref(arb->albums[arb->num_albums]); arb->num_albums++; /* Loop over each disc in the album and add tracks */ for(loop_node = ezxml_get(album_node, "discs", 0, "disc", -1); loop_node; loop_node = loop_node->next) { /* Cache disc number */ if((node = ezxml_get(loop_node, "disc-number", -1)) == NULL) { DSFYDEBUG("BUG: Found no 'disc-numner' under discs -> disc\n"); continue; } disc_number = atoi(node->txt); /* Loop over each track and add it to the artistbrowse tracks list */ for(track_node = ezxml_get(loop_node, "track", -1), i = 1; track_node; track_node = track_node->next, i++) { /* Extract track ID and add it */ if((node = ezxml_get(track_node, "id", -1)) == NULL) continue; hex_ascii_to_bytes(node->txt, id, 16); track = osfy_track_add(session, id); /* Add album to track */ if(track->album) sp_album_release(track->album); track->album = album; sp_album_add_ref(track->album); /* Set disc number */ track->disc = disc_number; /* Set track index on disc */ if(track->index == 0) track->index = i; /* Load track details from XML if not already loaded */ if(sp_track_is_loaded(track) == 0) osfy_track_load_from_xml(session, track, track_node); assert(sp_track_is_loaded(track)); /* Mark track as available if the album is available and the album has a non-zero duration (i.e, associated files) */ if(!track->is_available && track->duration) { DSFYDEBUG("Track '%s' marked as not available but has files, force-marking track as %savailable\n", node->txt, !album->is_available? "not ": ""); track->is_available = album->is_available; } /* Add track to artistbrowse and increase the track's ref count */ arb->tracks = realloc(arb->tracks, sizeof(sp_track *) * (1 + arb->num_tracks)); arb->tracks[arb->num_tracks] = track; sp_track_add_ref(arb->tracks[arb->num_tracks]); arb->num_tracks++; } } /* for each disc in album */ } /* for each album */ return 0; }
void Artist::Load(sp_artist *pArtist) { artist_ = pArtist; sp_artist_add_ref(artist_); }