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 pthread_mutex_t curl_lock
;
38 extern pthread_mutex_t handshake_lock
;
40 extern bool notify_about_now_playing
;
42 void ScrobbyErrorCallback(MPD::Connection
*, int, string errormessage
, void *)
44 ignore_newlines(errormessage
);
45 Log(llVerbose
, "MPD: %s", errormessage
.c_str());
48 void ScrobbyStatusChanged(MPD::Connection
*Mpd
, MPD::StatusChanges changed
, void *)
52 old_state
= current_state
;
53 current_state
= Mpd
->GetState();
54 if (old_state
== MPD::psStop
&& current_state
== MPD::psPlay
)
57 if (changed
.ElapsedTime
)
60 if (Mpd
->GetElapsedTime() == ((crossfade
= Mpd
->GetCrossfade()) ? crossfade
: 0 ))
64 if (changed
.SongID
|| (old_state
== MPD::psPlay
&& current_state
== MPD::psStop
))
68 // in this case allow entering only once
69 if (old_state
== MPD::psPlay
&& current_state
== MPD::psStop
)
70 old_state
= MPD::psUnknown
;
72 if (Mpd
->GetElapsedTime() < Mpd
->GetCrossfade()+5)
75 if (current_state
== MPD::psPlay
|| current_state
== MPD::psPause
)
77 s
.SetData(Mpd
->CurrentSong());
78 notify_about_now_playing
= s
.Data() && !s
.isStream();
81 if (notify_about_now_playing
)
83 pthread_mutex_lock(&handshake_lock
);
84 if (s
.Data() && (!s
.Data()->artist
|| !s
.Data()->title
))
86 Log(llInfo
, "Playing song with missing tags detected.");
88 else if (s
.Data() && s
.Data()->time
<= 0)
90 Log(llInfo
, "Playing song with unknown length detected.");
92 else if (s
.Data() && s
.Data()->artist
&& s
.Data()->title
)
94 Log(llVerbose
, "Playing song detected: %s - %s", s
.Data()->artist
, s
.Data()->title
);
96 if (handshake
.status
== "OK" && !handshake
.nowplaying_url
.empty())
98 Log(llInfo
, "Sending now playing notification...");
102 Log(llInfo
, "Notification not sent due to problem with connection.");
103 goto NOTIFICATION_FAILED
;
106 string result
, postdata
;
109 pthread_mutex_lock(&curl_lock
);
110 CURL
*np_notification
= curl_easy_init();
112 char *c_artist
= curl_easy_escape(np_notification
, s
.Data()->artist
, 0);
113 char *c_title
= curl_easy_escape(np_notification
, s
.Data()->title
, 0);
114 char *c_album
= s
.Data()->album
? curl_easy_escape(np_notification
, s
.Data()->album
, 0) : NULL
;
115 char *c_track
= s
.Data()->track
? curl_easy_escape(np_notification
, s
.Data()->track
, 0) : NULL
;
118 postdata
+= handshake
.session_id
;
120 postdata
+= c_artist
;
127 postdata
+= IntoStr(s
.Data()->time
);
138 Log(llVerbose
, "URL: %s", handshake
.nowplaying_url
.c_str());
139 Log(llVerbose
, "Post data: %s", postdata
.c_str());
141 curl_easy_setopt(np_notification
, CURLOPT_URL
, handshake
.nowplaying_url
.c_str());
142 curl_easy_setopt(np_notification
, CURLOPT_POST
, 1);
143 curl_easy_setopt(np_notification
, CURLOPT_POSTFIELDS
, postdata
.c_str());
144 curl_easy_setopt(np_notification
, CURLOPT_WRITEFUNCTION
, write_data
);
145 curl_easy_setopt(np_notification
, CURLOPT_WRITEDATA
, &result
);
146 curl_easy_setopt(np_notification
, CURLOPT_CONNECTTIMEOUT
, curl_timeout
);
147 code
= curl_easy_perform(np_notification
);
148 curl_easy_cleanup(np_notification
);
149 pthread_mutex_unlock(&curl_lock
);
151 ignore_newlines(result
);
155 Log(llInfo
, "Notification about currently playing song sent.");
161 Log(llInfo
, "Error while sending notification: %s", curl_easy_strerror(code
));
165 Log(llInfo
, "Audioscrobbler returned status %s", result
.c_str());
167 goto NOTIFICATION_FAILED
;
174 handshake
.Clear(); // handshake probably failed if we are here, so reset it
175 Log(llVerbose
, "Handshake status reset");
177 pthread_mutex_unlock(&handshake_lock
);
178 notify_about_now_playing
= 0;