-
Notifications
You must be signed in to change notification settings - Fork 0
/
lastfm.cpp
84 lines (75 loc) · 2.67 KB
/
lastfm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "lastfm.h"
LastFM::LastFM() : m_scrobbler("GM Player"){
lastfm::ws::ApiKey = "036521024cdba0712f16f59833b50d06";
lastfm::ws::SharedSecret = "12b55a5bff43c973fc83f5e188037649";
m_is_authenticated = false;
loadSettings();
const QString authToken = md5( QString( "%1%2" ).arg( lastfm::ws::Username ).arg(md5( m_password.toUtf8() ) ).toUtf8() );
QMap<QString, QString> query;
query[ "method" ] = "auth.getMobileSession";
query[ "username" ] = lastfm::ws::Username;
query[ "authToken" ] = authToken;
m_authenticateReply = lastfm::ws::post( query );
connect( m_authenticateReply, SIGNAL(finished()), SLOT(authenticated()) );
}
QString LastFM::md5( const QByteArray& src ) {
QByteArray const digest = QCryptographicHash::hash( src, QCryptographicHash::Md5 );
return QString::fromLatin1( digest.toHex() ).rightJustified( 32, '0' );
}
void LastFM::authenticated() {
switch( m_authenticateReply ? m_authenticateReply->error() : QNetworkReply::UnknownNetworkError )
{
case QNetworkReply::NoError:
{
lastfm::XmlQuery lfm;
if( !lfm.parse( m_authenticateReply->readAll() ) || lfm.children( "error" ).size() > 0 )
{
qDebug() << "error authenticating with last.fm service:" << lfm.text();
break;
}
lastfm::ws::Username = lfm["session"]["name"].text();
lastfm::ws::SessionKey = lfm["session"]["key"].text();
m_is_authenticated = true;
break;
}
case QNetworkReply::AuthenticationRequiredError:
{
qDebug() << "Authenticated required error";
break;
}
default:
{
qDebug() << "Network problems";
break;
}
}
}
void LastFM::scrobbled() {
qDebug() << "we scrobbled";
}
void LastFM::scrobble() {
lastfm::MutableTrack t;
t.setArtist("Test Artist");
t.setTitle("Test Track");
t.setTimeStamp(QDateTime::currentDateTime());
m_authenticateReply = t.scrobble();
connect( m_authenticateReply, SIGNAL(finished()), SLOT(scrobbled()) );
switch( t.scrobbleStatus() )
{
case lastfm::Track::Cached:
case lastfm::Track::Submitted:
qDebug() << "submit";
break;
case lastfm::Track::Null:
qDebug() << "null";
break;
case lastfm::Track::Error:
qDebug() << "error";
break;
}
}
void LastFM::loadSettings() {
QSettings settings(QLatin1String("lastfm.ini"), QSettings::IniFormat);
lastfm::ws::Username = settings.value("username").toString();
m_password = settings.value("password").toString();
}