Debian Codegraph @

demos/demo2.cpp

raw symbols SCIP
1 /*
2 This software is in the public domain, furnished "as is", without technical
3 support, and with no warranty, express or implied, as to its usefulness for
4 any purpose.
5 */
6
7 #include "ws.h"
8 #include "misc.h"
9 #include "XmlQuery.h"
10 #include "Artist.h"
11
12 #include <QtCore>
13
14
15 struct MyCoreApp : QCoreApplication
16 {
17 Q_OBJECT
18
19 public:
20 MyCoreApp( int& argc, char**& argv ) : QCoreApplication( argc, argv )
21 {}
22
23 private Q_SLOTS:
24 void onWsError( lastfm::ws::Error e )
25 {
26 // QNetworkReply will invoke this slot on application level errors
27 // mostly this is only stuff like Ws::InvalidSessionKey and
28 // Ws::InvalidApiKey
29 qWarning() << e;
30 }
31 };
32
33
34 int main( int argc, char** argv )
35 {
36 MyCoreApp app( argc, argv );
37 // this is used to generate the UserAgent for webservice requests
38 // please set it to something sensible in your application
39 app.setApplicationName( "liblastfm" );
40
41 ////// you'll need to fill these in for this demo to work
42 #warning credentials need to be filled in
43 lastfm::ws::Username = "";
44 lastfm::ws::ApiKey = "";
45 lastfm::ws::SharedSecret = "";
46 QString password = "";
47
48 ////// Usually you never have to construct an Last.fm WS API call manually
49 // eg. Track.getTopTags() just returns a QNetworkReply* but authentication is
50 // different.
51 // We're using getMobileSession here as we're a console app, but you
52 // should use getToken if you can as the user will be presented with a
53 // route that feels my trustworthy to them than entering their password
54 // into some random app they just downloaded... ;)
55 QMap<QString, QString> params;
56 params["method"] = "auth.getMobileSession";
57 params["username"] = lastfm::ws::Username;
58 params["authToken"] = lastfm::md5( (lastfm::ws::Username + lastfm::md5( password.toUtf8() )).toUtf8() );
59 QNetworkReply* reply = lastfm::ws::post( params );
60
61 // never do this when an event loop is running it's a real HACK
62 QEventLoop loop;
63 loop.connect( reply, SIGNAL(finished()), SLOT(quit()) );
64 loop.exec();
65
66 try
67 {
68 ////// Usually there is a convenience function to decode the output from
69 // ws calls too, but again, authentication is different. We think you
70 // need to handle it yourselves :P Also conveniently it means you
71 // can learn more about what our webservices return, eg. this service
72 // will return an XML document like this:
73 //
74 // <lfm status="ok">
75 // <session>
76 // <name>mxcl</name>
77 // <key>d580d57f32848f5dcf574d1ce18d78b2</key>
78 // <subscriber>1</subscriber>
79 // </session>
80 // </lfm>
81 //
82 // If status is not "ok" then this function throws
83 lastfm::XmlQuery const lfm;
84 #warning this code needs to be ported to new api, executable is most likely broken
85 //= lastfm::ws::parse( reply );
86
87 // replace username; because eg. perhaps the user typed their
88 // username with the wrong case
89 lastfm::ws::Username = lfm["session"]["name"].text();
90
91 // we now have a session key, you should save this, forever! Really.
92 // DO NOT AUTHENTICATE EVERY TIME THE APP STARTS! You only have to do
93 // this once. Or again if the user deletes your key on the site. If
94 // that happens you'll get notification to your onWsError() function,
95 // see above.
96 lastfm::ws::SessionKey = lfm["session"]["key"].text();
97
98 qDebug() << "sk:" << lastfm::ws::SessionKey;
99
100 ////// because the SessionKey is now set, the AuthenticatedUser class will
101 // work. And we can call authenticated calls
102 QNetworkReply* reply;
103 #warning this code needs to be ported to new api, executable is most likely broken
104 //= lastfm::AuthenticatedUser().getRecommendedArtists();
105 // again, you shouldn't do this.. ;)
106 QEventLoop loop;
107 loop.connect( reply, SIGNAL(finished()), SLOT(quit()) );
108 loop.exec();
109
110 // yay, a list rec'd artists to stderr :)
111 qDebug() << lastfm::Artist::list( reply );
112 }
113 catch (std::runtime_error& e)
114 {
115 // lastfm::ws::parse() can throw lastfm::ws::ParseError, this
116 // exception derives std::runtime_error
117 qWarning() << e.what();
118 return 1;
119 }
120 }
121
122
123 #include "demo2.moc"