From a3a3898e37944e64a58d412022931496318e6209 Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Sun, 22 Jun 2008 12:16:31 +0200 Subject: client: last.fm scrobbling implemented Very very ugly... We should spawn another thread. But for now it's working. (Though the played times are wrong for me, could be a wardrobe issue...) --- src/Makefile.am | 4 ++-- src/main.c | 36 ++++++++++++++++++++++++++++++++++++ src/pianobar.1 | 9 +++++++++ src/settings.c | 10 ++++++++++ src/settings.h | 3 +++ 5 files changed, 60 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 606430b..e3a98a4 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -3,8 +3,8 @@ bin_PROGRAMS = pianobar pianobar_SOURCES = main.c terminal.c terminal.h settings.c settings.h \ player.c player.h pianobar_CPPFLAGS = ${LIBCURL_CFLAGS} ${LIBAO_CFLAGS} ${LIBXML_CFLAGS} \ - -I../libpiano/src + -I../libpiano/src -I../libwardrobe/src pianobar_LDADD = ${LIBCURL_LIBS} ${LIBAO_LIBS} ${LIBFAAD_LIBS} \ ${LIBREADLINE_LIBS} ${LIBXML_LIBS} -lpthread \ - ../libpiano/src/libpiano.la + ../libpiano/src/libpiano.la ../libwardrobe/src/libwardrobe.la dist_man1_MANS = pianobar.1 diff --git a/src/main.c b/src/main.c index 872bc62..f8ce101 100644 --- a/src/main.c +++ b/src/main.c @@ -21,6 +21,7 @@ THE SOFTWARE. */ #include +#include #include #include #include @@ -31,6 +32,7 @@ THE SOFTWARE. #include #include #include +#include #include "terminal.h" #include "settings.h" @@ -156,6 +158,8 @@ int main (int argc, char **argv) { PianoStation_t *curStation = NULL; BarSettings_t bsettings; pthread_t playerThread; + WardrobeSong_t scrobbleSong; + WardrobeHandle_t wh; printf ("Welcome to " PACKAGE_STRING "! Press ? for help.\n"); @@ -177,6 +181,13 @@ int main (int argc, char **argv) { } PianoInit (&ph); + WardrobeInit (&wh); + + if (bsettings.enableScrobbling) { + wh.user = strdup (bsettings.lastfmUser); + wh.password = strdup (bsettings.lastfmPassword); + } + /* setup control connection */ if (bsettings.controlProxy != NULL && bsettings.controlProxyType != -1) { @@ -216,6 +227,21 @@ int main (int argc, char **argv) { if (player.finishedPlayback == 1) { /* already played a song, clean up things */ if (player.url != NULL) { + scrobbleSong.length = BarSamplesToSeconds (player.samplerate, + player.channels, player.sampleSizeN); + /* scrobble when >= 90% are played */ + if (BarSamplesToSeconds (player.samplerate, + player.channels, player.sampleSizeCurr) * 100 / + scrobbleSong.length >= 90 && + bsettings.enableScrobbling) { + if (WardrobeSubmit (&wh, &scrobbleSong) == + WARDROBE_RET_OK) { + printf ("Scrobbled. \n"); + } else { + printf ("Errror while scrobbling. \n"); + } + } + WardrobeSongDestroy (&scrobbleSong); free (player.url); memset (&player, 0, sizeof (player)); pthread_join (playerThread, NULL); @@ -236,9 +262,18 @@ int main (int argc, char **argv) { } } if (curSong != NULL) { + time_t currTime = time (NULL); + time_t currGmTime = mktime (gmtime (&currTime)); printf ("\"%s\" by \"%s\"%s\n", curSong->title, curSong->artist, (curSong->rating == PIANO_RATE_LOVE) ? " (Loved)" : ""); + /* setup artist and song name for scrobbling (curSong + * may be NULL later) */ + WardrobeSongInit (&scrobbleSong); + scrobbleSong.artist = strdup (curSong->artist); + scrobbleSong.title = strdup (curSong->title); + scrobbleSong.started = currGmTime; + /* FIXME: why do we need to zero everything again? */ memset (&player, 0, sizeof (player)); player.url = strdup (curSong->audioUrl); @@ -394,6 +429,7 @@ int main (int argc, char **argv) { } /* destroy everything (including the world...) */ PianoDestroy (&ph); + WardrobeDestroy (&wh); curl_global_cleanup (); ao_shutdown(); xmlCleanupParser (); diff --git a/src/pianobar.1 b/src/pianobar.1 index 0478af3..c17a3c3 100644 --- a/src/pianobar.1 +++ b/src/pianobar.1 @@ -86,6 +86,15 @@ use. Note that needs an IP address specified in .B control_proxy +.TP +.B lastfm_user = your_username +If you want to send your played song to last.fm set this to your last.fm +username. + +.TP +.B lastfm_password = plain_password +A password is needed too if you want to scrobble your played song. + .TP .B password = plaintext_password Your pandora.com password. Plain-text. diff --git a/src/settings.c b/src/settings.c index 91fce9a..abdf5b7 100644 --- a/src/settings.c +++ b/src/settings.c @@ -62,6 +62,9 @@ void BarSettingsDestroy (BarSettings_t *settings) { free (settings->controlProxy); free (settings->username); free (settings->password); + free (settings->lastfmUser); + free (settings->lastfmPassword); + memset (settings, 0, sizeof (*settings)); } /* read app settings from file; format is: key = value\n @@ -104,8 +107,15 @@ void readSettings (BarSettings_t *settings) { settings->username = strdup (val); } else if (strcmp ("password", key) == 0) { settings->password = strdup (val); + } else if (strcmp ("lastfm_user", key) == 0) { + settings->lastfmUser = strdup (val); + } else if (strcmp ("lastfm_password", key) == 0) { + settings->lastfmPassword = strdup (val); } } + if (settings->lastfmUser != NULL && settings->lastfmPassword != NULL) { + settings->enableScrobbling = 1; + } fclose (configfd); } diff --git a/src/settings.h b/src/settings.h index 15b5396..71737f2 100644 --- a/src/settings.h +++ b/src/settings.h @@ -30,6 +30,9 @@ struct BarSettings { char *password; char *controlProxy; /* non-american listeners need this */ curl_proxytype controlProxyType; + char *lastfmUser; + char *lastfmPassword; + char enableScrobbling; }; typedef struct BarSettings BarSettings_t; -- cgit v1.2.3