Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add a cheeky random query for the lulz

  • Loading branch information...
commit ce1fa816b0908405592bd3348511bd7e91c13aae 1 parent 1e5ad33
@RJ authored
View
13 resolvers/local/library.cpp
@@ -280,6 +280,19 @@ Library::get_album_id(int artistid, const string& name_orig)
return id;
}
+int
+Library::get_random_fid()
+{
+ boost::mutex::scoped_lock lock(m_mut);
+ string sql = "SELECT id FROM file WHERE size > 0 AND rowid > (abs(random()) % (SELECT max(rowid) FROM file)) LIMIT 1";
+ sqlite3pp::query qry(m_db, sql.c_str());
+ for (sqlite3pp::query::iterator i = qry.begin(); i != qry.end(); ++i)
+ {
+ return (*i).get<int>(0);
+ }
+ return -1;
+}
+
vector<scorepair>
Library::search_catalogue(string table, string name_orig)
{
View
2  resolvers/local/library.h
@@ -59,6 +59,8 @@ class Library
int num_artists();
int num_albums();
int num_tracks();
+
+ int get_random_fid();
bool build_index(std::string);
static std::string sortname(const std::string& name);
View
61 resolvers/local/rs_local_library.cpp
@@ -101,24 +101,50 @@ local::run()
void
local::process( rq_ptr rq )
{
+ //Ignore this if it's missing artist+track fields
+ if( !rq->param_exists( "artist" )
+ || rq->param("artist").type() != str_type
+ || !rq->param_exists( "track" )
+ || rq->param("track").type() != str_type )
+ {
+ return;
+ }
vector< json_spirit::Object > final_results;
- // get candidates (rough potential matches):
- vector<scorepair> candidates = find_candidates(rq, 10);
- // now do the "real" scoring of candidate results:
- string reason; // for scoring debug.
- BOOST_FOREACH(scorepair &sp, candidates)
+
+ // check if this is a special "random" query
+ if( rq->param("artist").get_str() == "*" &&
+ rq->param("track").get_str() == "*" )
+ {
+ int fid = m_library->get_random_fid();
+ if( fid == -1 ) return;
+ json_spirit::Object js;
+ ResolvedItemBuilder::createFromFid( *m_library, fid, js );
+ js.push_back( json_spirit::Pair( "sid", m_pap->gen_uuid()) );
+ js.push_back( json_spirit::Pair( "source", m_pap->hostname()) );
+ js.push_back( json_spirit::Pair( "score", 0.99) );
+ // dangerous, will propagate forever? muhahah
+ final_results.push_back( js );
+ }
+ else // end special random query check
{
- // multiple files in our collection may have matching metadata.
- // add them all to the results.
- vector<int> fids = m_library->get_fids_for_tid(sp.id);
- BOOST_FOREACH(int fid, fids)
+ // get candidates (rough potential matches):
+ vector<scorepair> candidates = find_candidates(rq, 10);
+ // now do the "real" scoring of candidate results:
+ string reason; // for scoring debug.
+ BOOST_FOREACH(scorepair &sp, candidates)
{
- json_spirit::Object js;
- js.reserve(12);
- ResolvedItemBuilder::createFromFid( *m_library, fid, js );
- js.push_back( json_spirit::Pair( "sid", m_pap->gen_uuid()) );
- js.push_back( json_spirit::Pair( "source", m_pap->hostname()) );
- final_results.push_back( js );
+ // multiple files in our collection may have matching metadata.
+ // add them all to the results.
+ vector<int> fids = m_library->get_fids_for_tid(sp.id);
+ BOOST_FOREACH(int fid, fids)
+ {
+ json_spirit::Object js;
+ js.reserve(12);
+ ResolvedItemBuilder::createFromFid( *m_library, fid, js );
+ js.push_back( json_spirit::Pair( "sid", m_pap->gen_uuid()) );
+ js.push_back( json_spirit::Pair( "source", m_pap->hostname()) );
+ final_results.push_back( js );
+ }
}
}
if(final_results.size())
@@ -139,11 +165,6 @@ local::find_candidates(rq_ptr rq, unsigned int limit)
vector<scorepair> candidates;
float maxartscore = 0;
- //Ignore this request_query - nothing that this can resolve from.
- if( !rq->param_exists( "artist" ) ||
- !rq->param_exists( "track" ))
- return candidates;
-
vector<scorepair> artistresults =
m_library->search_catalogue("artist", rq->param( "artist" ).get_str());
BOOST_FOREACH( scorepair & sp, artistresults )
Please sign in to comment.
Something went wrong with that request. Please try again.