Skip to content
Browse files

Fix stuff

git-svn-id: svn+ssh://svn.last.fm/svn/clientside/trunk/desktop@125528 ab8f4a45-97f9-0310-bbd1-854ce3dcee89
  • Loading branch information...
1 parent deefd76 commit dc2ce161fb26a4e95ad5a049641d97deefa860f5 @mxcl mxcl committed Mar 27, 2009
View
10 app/client/ExtractIdentifiersJob.cpp
@@ -72,14 +72,12 @@ ExtractIdentifiersJob::fpid()
if (!fp.id().isNull())
return fp.id();
- WsAccessManager wam;
-
try
{
qDebug() << "Doing partial fingerprint for" << m_track;
fp.generate();
- QNetworkReply* reply = fp.submit( &wam );
+ QNetworkReply* reply = fp.submit();
WAIT_FOR_FINISHED( reply );
bool complete_fp_required = false;
@@ -88,14 +86,14 @@ ExtractIdentifiersJob::fpid()
if (complete_fp_required)
{
qDebug() << "Doing complete fingerprint for" << m_track;
- CompleteFingerprint fp( m_track );
+ lastfm::CompleteFingerprint fp( m_track );
fp.generate();
- fp.submit( &wam );
+ fp.submit();
WAIT_FOR_FINISHED( reply );
}
}
- catch (Fp::Error e)
+ catch (Fingerprint::Error e)
{
qWarning() << e;
}
View
44 lib/lastfm/fingerprint/EXAMPLE.cpp → lib/lastfm/fingerprint/EXAMPLE
@@ -37,26 +37,40 @@ int main( int argc, char** argv )
{
QCoreApplication app( argc, argv );
+ // these fields are required
MutableTrack t;
t.setArtist( "Air" );
t.setTitle( "Redhead Girl" );
t.setAlbum( "Pocket Symphony" );
t.setUrl( QUrl::fromLocalFile( "/Users/mxcl/Music/iTunes/iTunes Music/Air/Pocket Symphony/1-11 Redhead Girl.mp3") );
-
- Fingerprint fp( t );
-
- if(fp.id().isNull()){
- fp.generate();
- finish( fp.submit() );
- fp.decode( reply );
- }
- if (fp.id().isNull()) {
- qWarning() << "Still null :(";
- return 1;
- }
-
- finish( fp.id().getSuggestions() );
+ try
+ {
+ Fingerprint fp( t );
+
+ // we cache FingerprintIds in an sqlite3 db, as the generate() function
+ // is expensive
+ if (fp.id().isNull())
+ {
+ // this generates the full fingerprint hash, which is about 20kB
+ fp.generate();
+
+ // this asks Last.fm for a FingerprintId
+ // the finish function is a Qt hack to allow syncronous HTTP
+ finish( fp.submit() );
+
+ // the decode step sets the FingerprintId
+ // the FingerprintId is required to obtain suggestions
+ // id will now be valid, or this function throws
+ fp.decode( reply );
+ }
+
+ finish( fp.id().getSuggestions() );
- qDebug() << FingerprintId::getSuggestions( reply );
+ qDebug() << FingerprintId::getSuggestions( reply );
+ }
+ catch (Fingerprint::Error e)
+ {
+ qWarning() << e; //TODO enum debug thing
+ }
}
View
27 lib/lastfm/fingerprint/Fingerprint.cpp
@@ -252,7 +252,7 @@ lastfm::Fingerprint::submit() const
void
-lastfm::Fingerprint::decode( QNetworkReply* reply, bool* complete_fingerprint_requested )
+lastfm::Fingerprint::decode( QNetworkReply* reply, bool* complete_fingerprint_requested ) throw( Error )
{
// The response data will consist of a number and a string.
// The number is the fpid and the string is either FOUND or NEW
@@ -264,29 +264,28 @@ lastfm::Fingerprint::decode( QNetworkReply* reply, bool* complete_fingerprint_re
QString const response( reply->readAll() );
QStringList const list = response.split( ' ' );
-
- if (response.isEmpty() || list.count() < 2) {
- qWarning() << "Null response";
- return;
- }
-
- if (response == "No response to client error")
- throw InternalError;
-
- if (list.count() != 2)
- qWarning() << "Response looks bad:" << response;
QString const fpid = list.value( 0 );
QString const status = list.value( 1 );
-
+
+ if (response.isEmpty() || list.count() < 2 || response == "No response to client error")
+ goto bad_response;
+ if (list.count() != 2)
+ qWarning() << "Response looks bad but continuing anyway:" << response;
+
bool b;
uint fpid_as_uint = fpid.toUInt( &b );
- if (!b) return;
+ if (!b) goto bad_response;
Collection::instance().setFingerprintId( m_track.url().toLocalFile(), fpid );
if (complete_fingerprint_requested)
*complete_fingerprint_requested = (status == "NEW");
m_id = (int)fpid_as_uint;
+ return;
+
+bad_response:
+ qWarning() << "Response is bad:" << response;
+ throw BadResponseError;
}
View
27 lib/lastfm/fingerprint/Fingerprint.h
@@ -64,6 +64,11 @@ namespace lastfm
/** there is a minimum track duration for fingerprinting */
TrackTooShortError,
+
+ /** the fingerprint service went wrong, or we submitted bad data,
+ * or myabe the request failed, whatever, we couldn't parse the
+ * result */
+ BadResponseError,
/** sorry, liblastfm sucks, report bug with log! */
InternalError
@@ -78,11 +83,10 @@ namespace lastfm
QNetworkReply* submit() const;
/** Pass a finished reply from submit(), if the response is sound, id()
- * will be valid. Otherwise query QNetworkReply for an error, if there is
- * no error, then the reply was malformed, try submit() again.
- * The return value of id() will be updated by this function, if possible.
+ * will be valid. Otherwise we will throw. You always get a valid id
+ * or a throw.
*/
- void decode( QNetworkReply*, bool* lastfm_needs_a_complete_fingerprint = 0 );
+ void decode( QNetworkReply*, bool* lastfm_needs_a_complete_fingerprint = 0 ) throw( Error );
};
@@ -96,4 +100,19 @@ namespace lastfm
};
}
+
+inline QDebug operator<<( QDebug d, lastfm::Fingerprint::Error e )
+{
+ #define CASE(x) case lastfm::Fingerprint::x: return d << #x;
+ switch (e)
+ {
+ CASE(ReadError)
+ CASE(HeadersError)
+ CASE(DecodeError)
+ CASE(TrackTooShortError)
+ CASE(BadResponseError)
+ CASE(InternalError)
+ }
+}
+
#endif

0 comments on commit dc2ce16

Please sign in to comment.
Something went wrong with that request. Please try again.