1 /***************************************************************************
2 * Copyright (C) 2008 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include <curl/curl.h>
31 MPD::State old_state
= MPD::psUnknown
;
32 MPD::State current_state
= MPD::psUnknown
;
34 extern Handshake handshake
;
37 extern bool notify_about_now_playing
;
39 void ScrobbyErrorCallback(MPD::Connection
*, int, string errormessage
, void *)
41 ignore_newlines(errormessage
);
42 Log(llVerbose
, "MPD: %s", errormessage
.c_str());
45 void ScrobbyStatusChanged(MPD::Connection
*Mpd
, MPD::StatusChanges changed
, void *)
49 old_state
= current_state
;
50 current_state
= Mpd
->GetState();
51 if (old_state
== MPD::psStop
&& current_state
== MPD::psPlay
)
54 if (changed
.ElapsedTime
)
57 if (Mpd
->GetElapsedTime() == ((crossfade
= Mpd
->GetCrossfade()) ? crossfade
: 0 ))
61 if (changed
.SongID
|| (old_state
== MPD::psPlay
&& current_state
== MPD::psStop
))
65 // in this case allow entering only once
66 if (old_state
== MPD::psPlay
&& current_state
== MPD::psStop
)
67 old_state
= MPD::psUnknown
;
69 if (Mpd
->GetElapsedTime() < Mpd
->GetCrossfade()+10)
72 if (current_state
== MPD::psPlay
|| current_state
== MPD::psPause
)
74 s
.SetData(Mpd
->CurrentSong());
75 notify_about_now_playing
= s
.Data
&& !s
.isStream();
78 if (notify_about_now_playing
)
80 if (s
.Data
&& (!s
.Data
->artist
|| !s
.Data
->title
))
82 Log(llInfo
, "Playing song with missing tags detected.");
84 else if (s
.Data
&& s
.Data
->time
<= 0)
86 Log(llInfo
, "Playing song with unknown length detected.");
88 else if (s
.Data
&& s
.Data
->artist
&& s
.Data
->title
)
90 Log(llVerbose
, "Playing song detected: %s - %s", s
.Data
->artist
, s
.Data
->title
);
92 if (handshake
.status
== "OK" && !handshake
.nowplaying_url
.empty())
94 Log(llInfo
, "Sending now playing notification...");
98 Log(llInfo
, "Notification not sent due to problem with connection.");
99 goto NOTIFICATION_FAILED
;
102 std::ostringstream postdata
;
103 string result
, postdata_str
;
106 char *c_artist
= curl_easy_escape(0, s
.Data
->artist
, 0);
107 char *c_title
= curl_easy_escape(0, s
.Data
->title
, 0);
108 char *c_album
= s
.Data
->album
? curl_easy_escape(0, s
.Data
->album
, 0) : NULL
;
109 char *c_track
= s
.Data
->track
? curl_easy_escape(0, s
.Data
->track
, 0) : NULL
;
112 << "s=" << handshake
.session_id
118 postdata
<< "&l=" << s
.Data
->time
129 postdata_str
= postdata
.str();
131 Log(llVerbose
, "URL: %s", handshake
.nowplaying_url
.c_str());
132 Log(llVerbose
, "Post data: %s", postdata_str
.c_str());
134 CURL
*np_notification
= curl_easy_init();
135 curl_easy_setopt(np_notification
, CURLOPT_URL
, handshake
.nowplaying_url
.c_str());
136 curl_easy_setopt(np_notification
, CURLOPT_POST
, 1);
137 curl_easy_setopt(np_notification
, CURLOPT_POSTFIELDS
, postdata_str
.c_str());
138 curl_easy_setopt(np_notification
, CURLOPT_WRITEFUNCTION
, write_data
);
139 curl_easy_setopt(np_notification
, CURLOPT_WRITEDATA
, &result
);
140 curl_easy_setopt(np_notification
, CURLOPT_CONNECTTIMEOUT
, curl_timeout
);
141 code
= curl_easy_perform(np_notification
);
142 curl_easy_cleanup(np_notification
);
144 ignore_newlines(result
);
148 Log(llInfo
, "Notification about currently playing song sent.");
154 Log(llInfo
, "Error while sending notification: %s", curl_easy_strerror(code
));
158 Log(llInfo
, "Audioscrobbler returned status %s", result
.c_str());
160 goto NOTIFICATION_FAILED
;
167 handshake
.Clear(); // handshake probably failed if we are here, so reset it
168 Log(llVerbose
, "Handshake status reset");
170 notify_about_now_playing
= 0;