Skip to content

Commit

Permalink
Add toplist support for tracks, artist and albums. Add track isAvaila…
Browse files Browse the repository at this point in the history
…ble method. Add album getCover method. Add playlist getImage method. Add artist getPortrait method.
  • Loading branch information
Hexxeh authored and vilhelmk committed Feb 20, 2012
1 parent 13d06a0 commit 0318f07
Show file tree
Hide file tree
Showing 10 changed files with 522 additions and 19 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -45,7 +45,8 @@


## Credits ## Credits


Vilhelm K. Vardøy <vilhelmkv@gmail.com> - Vilhelm K. Vardøy <vilhelmkv@gmail.com>
- Liam McLoughlin <hexxeh@hexxeh.net>


### Thanks to ### Thanks to


Expand Down
58 changes: 58 additions & 0 deletions album.c
Expand Up @@ -27,6 +27,8 @@ OTHER DEALINGS IN THE SOFTWARE.


zend_class_entry *spotifyalbum_ce; zend_class_entry *spotifyalbum_ce;


PHP_METHOD(SpotifyAlbum, browse);

PHP_METHOD(SpotifyAlbum, __construct) PHP_METHOD(SpotifyAlbum, __construct)
{ {
zval *object = getThis(); zval *object = getThis();
Expand All @@ -41,6 +43,7 @@ PHP_METHOD(SpotifyAlbum, __construct)
spotifyalbum_object *obj = (spotifyalbum_object*)zend_object_store_get_object(object TSRMLS_CC); spotifyalbum_object *obj = (spotifyalbum_object*)zend_object_store_get_object(object TSRMLS_CC);
obj->session = p->session; obj->session = p->session;
obj->album = album; obj->album = album;
obj->albumbrowse = NULL;


zend_update_property(spotifyalbum_ce, getThis(), "spotify", strlen("spotify"), parent TSRMLS_CC); zend_update_property(spotifyalbum_ce, getThis(), "spotify", strlen("spotify"), parent TSRMLS_CC);


Expand All @@ -50,6 +53,11 @@ PHP_METHOD(SpotifyAlbum, __construct)
PHP_METHOD(SpotifyAlbum, __destruct) PHP_METHOD(SpotifyAlbum, __destruct)
{ {
spotifyalbum_object *obj = (spotifyalbum_object*)zend_object_store_get_object(getThis() TSRMLS_CC); spotifyalbum_object *obj = (spotifyalbum_object*)zend_object_store_get_object(getThis() TSRMLS_CC);

if (obj->albumbrowse != NULL) {
sp_albumbrowse_release(obj->albumbrowse);
}

sp_album_release(obj->album); sp_album_release(obj->album);
} }


Expand Down Expand Up @@ -118,12 +126,60 @@ PHP_METHOD(SpotifyAlbum, getType)
RETURN_LONG(sp_album_type(p->album)); RETURN_LONG(sp_album_type(p->album));
} }


PHP_METHOD(SpotifyAlbum, getCover)
{
int timeout = 0;

spotifyalbum_object *p = (spotifyalbum_object*)zend_object_store_get_object(getThis() TSRMLS_CC);

zval tempretval, *thisptr = getThis();
SPOTIFY_METHOD(SpotifyAlbum, browse, &tempretval, thisptr);

const byte* image_id = sp_album_cover(p->album);
sp_image *image = sp_image_create(p->session, image_id);

while(!sp_image_is_loaded(image))
{
sp_session_process_events(p->session, &timeout);
}

size_t size;
const byte* image_data = sp_image_data(image, &size);

RETURN_STRINGL(image_data, size, 1);

sp_image_release(image);
}

PHP_METHOD(SpotifyAlbum, __toString) PHP_METHOD(SpotifyAlbum, __toString)
{ {
spotifyalbum_object *p = (spotifyalbum_object*)zend_object_store_get_object(getThis() TSRMLS_CC); spotifyalbum_object *p = (spotifyalbum_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
RETURN_STRING(sp_album_name(p->album), 1); RETURN_STRING(sp_album_name(p->album), 1);
} }


static void albumbrowse_complete(sp_albumbrowse *result, void *userdata)
{
spotifyalbum_object *p = (spotifyalbum_object*)userdata;
p->albumbrowse = result;
}

PHP_METHOD(SpotifyAlbum, browse)
{
int timeout = 0;

spotifyalbum_object *p = (spotifyalbum_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
if (p->albumbrowse != NULL) {
RETURN_TRUE;
}

sp_albumbrowse *tmpbrowse = sp_albumbrowse_create(p->session, p->album, albumbrowse_complete, p);
while (!sp_albumbrowse_is_loaded(tmpbrowse)) {
sp_session_process_events(p->session, &timeout);
}

RETURN_TRUE;
}

function_entry spotifyalbum_methods[] = { function_entry spotifyalbum_methods[] = {
PHP_ME(SpotifyAlbum, __construct, NULL, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR) PHP_ME(SpotifyAlbum, __construct, NULL, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
PHP_ME(SpotifyAlbum, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR) PHP_ME(SpotifyAlbum, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
Expand All @@ -134,7 +190,9 @@ function_entry spotifyalbum_methods[] = {
PHP_ME(SpotifyAlbum, getNumTracks, NULL, ZEND_ACC_PUBLIC) PHP_ME(SpotifyAlbum, getNumTracks, NULL, ZEND_ACC_PUBLIC)
PHP_ME(SpotifyAlbum, getTracks, NULL, ZEND_ACC_PUBLIC) PHP_ME(SpotifyAlbum, getTracks, NULL, ZEND_ACC_PUBLIC)
PHP_ME(SpotifyAlbum, getType, NULL, ZEND_ACC_PUBLIC) PHP_ME(SpotifyAlbum, getType, NULL, ZEND_ACC_PUBLIC)
PHP_ME(SpotifyAlbum, getCover, NULL, ZEND_ACC_PUBLIC)
PHP_ME(SpotifyAlbum, __toString, NULL, ZEND_ACC_PUBLIC) PHP_ME(SpotifyAlbum, __toString, NULL, ZEND_ACC_PUBLIC)
PHP_ME(SpotifyAlbum, browse, NULL, ZEND_ACC_PRIVATE)
{NULL, NULL, NULL} {NULL, NULL, NULL}
}; };


Expand Down
2 changes: 1 addition & 1 deletion albumiterator.c
Expand Up @@ -50,7 +50,7 @@ PHP_METHOD(SpotifyAlbumIterator, __construct)
spotifyalbumiterator_object *obj = (spotifyalbumiterator_object*)zend_object_store_get_object(getThis() TSRMLS_CC); spotifyalbumiterator_object *obj = (spotifyalbumiterator_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
obj->session = p->session; obj->session = p->session;
obj->artist = artistobj->artist; obj->artist = artistobj->artist;
obj->artistbrowse = sp_artistbrowse_create(p->session, artistobj->artist, spotify_artistbrowse_complete, obj); obj->artistbrowse = sp_artistbrowse_create(p->session, artistobj->artist, SP_ARTISTBROWSE_FULL, spotify_artistbrowse_complete, obj);


while (!sp_artistbrowse_is_loaded(obj->artistbrowse)) { while (!sp_artistbrowse_is_loaded(obj->artistbrowse)) {
sp_session_process_events(p->session, &timeout); sp_session_process_events(p->session, &timeout);
Expand Down
86 changes: 86 additions & 0 deletions artist.c
Expand Up @@ -27,6 +27,8 @@ OTHER DEALINGS IN THE SOFTWARE.


zend_class_entry *spotifyartist_ce; zend_class_entry *spotifyartist_ce;


PHP_METHOD(SpotifyArtist, browse);

PHP_METHOD(SpotifyArtist, __construct) PHP_METHOD(SpotifyArtist, __construct)
{ {
zval *object = getThis(); zval *object = getThis();
Expand All @@ -41,6 +43,7 @@ PHP_METHOD(SpotifyArtist, __construct)
spotifyartist_object *obj = (spotifyartist_object*)zend_object_store_get_object(object TSRMLS_CC); spotifyartist_object *obj = (spotifyartist_object*)zend_object_store_get_object(object TSRMLS_CC);
obj->session = p->session; obj->session = p->session;
obj->artist = artist; obj->artist = artist;
obj->artistbrowse = NULL;


zend_update_property(spotifyartist_ce, getThis(), "spotify", strlen("spotify"), parent TSRMLS_CC); zend_update_property(spotifyartist_ce, getThis(), "spotify", strlen("spotify"), parent TSRMLS_CC);


Expand All @@ -50,6 +53,11 @@ PHP_METHOD(SpotifyArtist, __construct)
PHP_METHOD(SpotifyArtist, __destruct) PHP_METHOD(SpotifyArtist, __destruct)
{ {
spotifyartist_object *p = (spotifyartist_object*)zend_object_store_get_object(getThis() TSRMLS_CC); spotifyartist_object *p = (spotifyartist_object*)zend_object_store_get_object(getThis() TSRMLS_CC);

if (p->artistbrowse != NULL) {
sp_artistbrowse_release(p->artistbrowse);
}

sp_artist_release(p->artist); sp_artist_release(p->artist);
} }


Expand Down Expand Up @@ -82,6 +90,81 @@ PHP_METHOD(SpotifyArtist, getAlbums)
SPOTIFY_METHOD1(SpotifyAlbumIterator, __construct, &tempretval, return_value, getThis()); SPOTIFY_METHOD1(SpotifyAlbumIterator, __construct, &tempretval, return_value, getThis());
} }


PHP_METHOD(SpotifyArtist, getPortrait)
{
int timeout = 0;

zval *index, *object = getThis();
spotifyartist_object *p = (spotifyartist_object*)zend_object_store_get_object(object TSRMLS_CC);

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &index) == FAILURE) {
return;
}

zval tempretval, *thisptr = getThis();
SPOTIFY_METHOD(SpotifyArtist, browse, &tempretval, thisptr);

int numportraits = sp_artistbrowse_num_portraits(p->artistbrowse);

if(Z_LVAL_P(index) > numportraits)
{
RETURN_FALSE;
}

const byte* image_id = sp_artistbrowse_portrait(p->artistbrowse, Z_LVAL_P(index));
sp_image *image = sp_image_create(p->session, image_id);

while(!sp_image_is_loaded(image))
{
sp_session_process_events(p->session, &timeout);
}

size_t size;
const byte* image_data = sp_image_data(image, &size);

RETURN_STRINGL(image_data, size, 1);

sp_image_release(image);
}

PHP_METHOD(SpotifyArtist, getNumPortraits)
{
int timeout = 0;

zval *object = getThis();
spotifyartist_object *p = (spotifyartist_object*)zend_object_store_get_object(object TSRMLS_CC);

zval tempretval, *thisptr = getThis();
SPOTIFY_METHOD(SpotifyArtist, browse, &tempretval, thisptr);

int numportraits = sp_artistbrowse_num_portraits(p->artistbrowse);

RETURN_LONG(numportraits);
}

static void artistbrowse_complete(sp_artistbrowse *result, void *userdata)
{
spotifyartist_object *p = (spotifyartist_object*)userdata;
p->artistbrowse = result;
}

PHP_METHOD(SpotifyArtist, browse)
{
int timeout = 0;

spotifyartist_object *p = (spotifyartist_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
if (p->artistbrowse != NULL) {
RETURN_TRUE;
}

sp_artistbrowse *tmpbrowse = sp_artistbrowse_create(p->session, p->artist, SP_ARTISTBROWSE_FULL, artistbrowse_complete, p);
while (!sp_artistbrowse_is_loaded(tmpbrowse)) {
sp_session_process_events(p->session, &timeout);
}

RETURN_TRUE;
}

PHP_METHOD(SpotifyArtist, __toString) PHP_METHOD(SpotifyArtist, __toString)
{ {
spotifyartist_object *p = (spotifyartist_object*)zend_object_store_get_object(getThis() TSRMLS_CC); spotifyartist_object *p = (spotifyartist_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
Expand All @@ -94,6 +177,9 @@ function_entry spotifyartist_methods[] = {
PHP_ME(SpotifyArtist, getName, NULL, ZEND_ACC_PUBLIC) PHP_ME(SpotifyArtist, getName, NULL, ZEND_ACC_PUBLIC)
PHP_ME(SpotifyArtist, getURI, NULL, ZEND_ACC_PUBLIC) PHP_ME(SpotifyArtist, getURI, NULL, ZEND_ACC_PUBLIC)
PHP_ME(SpotifyArtist, getAlbums, NULL, ZEND_ACC_PUBLIC) PHP_ME(SpotifyArtist, getAlbums, NULL, ZEND_ACC_PUBLIC)
PHP_ME(SpotifyArtist, getPortrait, NULL, ZEND_ACC_PUBLIC)
PHP_ME(SpotifyArtist, getNumPortraits, NULL, ZEND_ACC_PUBLIC)
PHP_ME(SpotifyArtist, browse, NULL, ZEND_ACC_PRIVATE)
PHP_ME(SpotifyArtist, __toString, NULL, ZEND_ACC_PUBLIC) PHP_ME(SpotifyArtist, __toString, NULL, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL} {NULL, NULL, NULL}
}; };
Expand Down
2 changes: 1 addition & 1 deletion config.m4
Expand Up @@ -4,5 +4,5 @@ if test "$PHP_SPOTIFY" = "yes"; then
AC_DEFINE(HAVE_SPOTIFY, 1, [Whether you have Spotify]) AC_DEFINE(HAVE_SPOTIFY, 1, [Whether you have Spotify])
LDFLAGS="$LDFLAGS -lspotify" LDFLAGS="$LDFLAGS -lspotify"
PHP_ADD_LIBRARY_WITH_PATH(spotify, "", SPOTIFY_SHARED_PATH) PHP_ADD_LIBRARY_WITH_PATH(spotify, "", SPOTIFY_SHARED_PATH)
PHP_NEW_EXTENSION(spotify, spotify.c playlist.c track.c artist.c album.c user.c albumiterator.c trackiterator.c, $ext_shared) PHP_NEW_EXTENSION(spotify, spotify.c playlist.c track.c artist.c album.c user.c toplist.c albumiterator.c trackiterator.c, $ext_shared)
fi fi
8 changes: 8 additions & 0 deletions php_spotify.h
Expand Up @@ -66,6 +66,7 @@ typedef struct {
zend_object std; zend_object std;
sp_session *session; sp_session *session;
sp_artist *artist; sp_artist *artist;
sp_artistbrowse *artistbrowse;
} spotifyartist_object; } spotifyartist_object;


typedef struct { typedef struct {
Expand All @@ -90,6 +91,12 @@ typedef struct {
sp_user *user; sp_user *user;
} spotifyuser_object; } spotifyuser_object;


typedef struct {
zend_object std;
sp_session *session;
sp_toplistbrowse *toplistbrowse;
} spotifytoplist_object;

typedef struct { typedef struct {
zend_object std; zend_object std;
sp_session *session; sp_session *session;
Expand Down Expand Up @@ -118,6 +125,7 @@ extern zend_class_entry *spotifytrack_ce;
extern zend_class_entry *spotifyartist_ce; extern zend_class_entry *spotifyartist_ce;
extern zend_class_entry *spotifyalbum_ce; extern zend_class_entry *spotifyalbum_ce;
extern zend_class_entry *spotifyuser_ce; extern zend_class_entry *spotifyuser_ce;
extern zend_class_entry *spotifytoplist_ce;


extern zend_class_entry *spotifyalbumiterator_ce; extern zend_class_entry *spotifyalbumiterator_ce;
extern zend_class_entry *spotifytrackiterator_ce; extern zend_class_entry *spotifytrackiterator_ce;
Expand Down

0 comments on commit 0318f07

Please sign in to comment.