1 /***************************************************************************
2 * Copyright (C) 2008-2009 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include <curl/curl.h>
31 extern Handshake myHandshake
;
34 void ScrobbyErrorCallback(MPD::Connection
*, int, string errormessage
, void *)
36 IgnoreNewlines(errormessage
);
37 Log(llVerbose
, "MPD: %s", errormessage
.c_str());
40 void ScrobbyStatusChanged(MPD::Connection
*Mpd
, MPD::StatusChanges changed
, void *)
42 static MPD::State old_state
= MPD::psUnknown
;
43 static MPD::State current_state
= MPD::psUnknown
;
44 static bool NowPlayingNotify
= 0;
48 old_state
= current_state
;
49 current_state
= Mpd
->GetState();
50 if (old_state
== MPD::psStop
&& current_state
== MPD::psPlay
)
53 if (changed
.ElapsedTime
)
56 if (Mpd
->GetElapsedTime() == ((crossfade
= Mpd
->GetCrossfade()) ? crossfade
: 0 ))
60 if (changed
.SongID
|| (old_state
== MPD::psPlay
&& current_state
== MPD::psStop
))
64 // in this case allow entering only once
65 if (old_state
== MPD::psPlay
&& current_state
== MPD::psStop
)
66 old_state
= MPD::psUnknown
;
68 if (Mpd
->GetElapsedTime() < Mpd
->GetCrossfade()+10)
71 if (current_state
== MPD::psPlay
|| current_state
== MPD::psPause
)
73 s
.SetData(Mpd
->CurrentSong());
74 NowPlayingNotify
= s
.Data
&& !s
.isStream();
77 if (!NowPlayingNotify
|| !s
.Data
|| myHandshake
.Status
!= "OK" || myHandshake
.NowPlayingURL
.empty())
82 if (!s
.Data
->artist
|| !s
.Data
->title
)
84 Log(llInfo
, "Playing song with missing tags detected.");
86 else if (s
.Data
->time
<= 0)
88 Log(llInfo
, "Playing song with unknown length detected.");
90 else if (s
.Data
->artist
&& s
.Data
->title
)
92 Log(llVerbose
, "Playing song detected: %s - %s", s
.Data
->artist
, s
.Data
->title
);
93 Log(llInfo
, "Sending now playing notification...");
95 std::ostringstream postdata
;
96 string result
, postdata_str
;
99 char *c_artist
= curl_easy_escape(0, s
.Data
->artist
, 0);
100 char *c_title
= curl_easy_escape(0, s
.Data
->title
, 0);
101 char *c_album
= s
.Data
->album
? curl_easy_escape(0, s
.Data
->album
, 0) : NULL
;
102 char *c_track
= s
.Data
->track
? curl_easy_escape(0, s
.Data
->track
, 0) : NULL
;
103 char *c_mb_trackid
= s
.Data
->musicbrainz_trackid
? curl_easy_escape(0, s
.Data
->musicbrainz_trackid
, 0) : NULL
;
106 << "s=" << myHandshake
.SessionID
112 postdata
<< "&l=" << s
.Data
->time
118 postdata
<< c_mb_trackid
;
124 curl_free(c_mb_trackid
);
126 postdata_str
= postdata
.str();
128 Log(llVerbose
, "URL: %s", myHandshake
.NowPlayingURL
.c_str());
129 Log(llVerbose
, "Post data: %s", postdata_str
.c_str());
131 CURL
*np_notification
= curl_easy_init();
132 curl_easy_setopt(np_notification
, CURLOPT_URL
, myHandshake
.NowPlayingURL
.c_str());
133 curl_easy_setopt(np_notification
, CURLOPT_POST
, 1);
134 curl_easy_setopt(np_notification
, CURLOPT_POSTFIELDS
, postdata_str
.c_str());
135 curl_easy_setopt(np_notification
, CURLOPT_WRITEFUNCTION
, write_data
);
136 curl_easy_setopt(np_notification
, CURLOPT_WRITEDATA
, &result
);
137 curl_easy_setopt(np_notification
, CURLOPT_CONNECTTIMEOUT
, curl_timeout
);
138 code
= curl_easy_perform(np_notification
);
139 curl_easy_cleanup(np_notification
);
141 IgnoreNewlines(result
);
145 Log(llInfo
, "Notification about currently playing song sent.");
151 Log(llInfo
, "Error while sending notification: %s", curl_easy_strerror(code
));
155 Log(llInfo
, "Audioscrobbler returned status %s", result
.c_str());
156 myHandshake
.Clear(); // handshake probably failed if we are here, so reset it
157 Log(llVerbose
, "Handshake reset");
158 NowPlayingNotify
= 1;