close

  
<p> var start_time = new Date();
var split_time = start_time;
var timers = {'start':start_time};

function split(label1, label2) {
  var now = new Date();
  if (label2) {
    if (!timers[label1]) {
      timers[label1] = {};
    }
    timers[label1][label2] = now - split_time;
  } else {
    timers[label1] = now - split_time;
  }
  split_time = now;
}

function format_timer(timers) {
  var strs = [];
  for (var k in timers) {
    v = timers[k];
    if (typeof(v) == 'object') {
        for (var k2 in v) {
          v2 = v[k2];
          strs.push(k+"~"+k2+"="+v2);
        }
    } else {
      strs.push(k+"="+v);
    }
  }
  return strs.join(", ");
}
          

acre.response.status = 200;
acre.response.set_header("content-type", "application/json");
var lib = acre.require("artist_lib");

var p = acre.request.params;
var no_results = {'match':false, 'score':0.0};

// parse parameters, either within dict or on their own
if (p['q']) {
  args = JSON.parse(p['q']);
} else {
  args = p;
}
var artist = (args['artist']=="") ? null:args['artist'];
var album  = (args['album']=="")  ? null:args['album'];
var track  = (args['track']=="")  ? null:args['track'];
var dedup  = (args['dedup']=="")  ? null:args['dedup'];
var filter = (args['filter']=="") ? null:args['filter'];
var album_blurbs = (args['album_blurbs']=="") ? null:args['album_blurbs'];

if (artist != null) {

  // call the relevance service
  split('prep');
  var hit = lib.do_searches(artist, album, track);
  //acre.write(JSON.stringify(hit));
  
  // if we have results, call MQL
  var res={};
  if ((hit) &amp;&amp; (('guid' in hit) || ('canonical:id' in hit))) {
    response = {}
    if ('guid' in hit) {
      hit_artist_id = hit['guid'];
      res = lib.get_artist(hit_artist_id, dedup, filter, 'guid');
      hit_artist_id = '/guid/' + hit_artist_id.substring(1);
    } else {
      hit_artist_id = hit['canonical:id'];
      res = lib.get_artist(hit_artist_id, dedup, filter, 'id');
    }      
    split('discography_mql');
    if (res) {
      response.discography = res.album;
      lib.add_blurbs(response.discography, album_blurbs);
      response.artist = lib.get_blurb_image(hit_artist_id, res.name);
      split('blurbs', 'artist');
      if ('album' in hit) {
        hit_album = hit['album'][0];
        hit_album_id = hit_album['canonical:id'];
        if (album) {
          response.album = lib.get_blurb_image(hit_album_id, hit_album['name']);
          split('blurbs', 'album');
        }
        if ('track' in hit_album &amp;&amp; track != null) {
          hit_track = hit_album['track'][0];
          if (('song' in hit_track) &amp;&amp; (hit_track['song'].length&gt;0)) {
            hit_song_id = hit_track['song'][0]['canonical:id'];
            response.track = lib.get_blurb_image(hit_song_id, hit_track['name']);
            split('blurbs', 'track');
          } else {
            response.track = lib.get_blurb_image(hit_track['canonical:id'], hit_track['name']);          
            split('blurbs', 'track');
          }
        }
        response.match = hit.match;
        response.score = hit.score;
      }
    } else {
      response = no_results;
    }
  } else {
    response = no_results;
  }
} else {
  response = no_results;
}
 

// put the query back in the response
response['query'] = { 'artist' : p['artist'], 
                 'album'  : p['album'], 
                 'track'  : p['track']
               };
timers['total'] = new Date() - start_time;
timers['end'] = new Date();
response['timers'] = timers;

// call the beacon
lib.beacon(response);

// return the results
acre.write(JSON.stringify(response));
acre.response.set_cache_policy("fast");
acre.response.headers['X-acre-service-cost'] = format_timer(timers);
 </p>

Comments

Hide