PageRenderTime 173ms CodeModel.GetById 90ms app.highlight 9ms RepoModel.GetById 72ms app.codeStats 0ms

/thirdparty/liblastfm2/demos/demo2.cpp

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