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/main.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'src/main.c') 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 (); -- cgit v1.2.3