122 lines
3.9 KiB
JavaScript
122 lines
3.9 KiB
JavaScript
|
/* DO NOT MODIFY. This file was compiled Mon, 27 Jun 2011 17:49:39 GMT from
|
||
|
* /Users/chez/Sites/beathaven/app/coffeescripts/vkontakte.coffee
|
||
|
*/
|
||
|
|
||
|
(function() {
|
||
|
var Vkontakte;
|
||
|
Vkontakte = (function() {
|
||
|
Vkontakte.prototype.qr = [];
|
||
|
function Vkontakte(api_id) {
|
||
|
this.api_id = api_id;
|
||
|
}
|
||
|
Vkontakte.prototype.init = function() {
|
||
|
VK.init({
|
||
|
apiId: this.api_id,
|
||
|
nameTransportPath: '/xd_receiver.html'
|
||
|
});
|
||
|
return VK.Auth.getLoginStatus(this.authInfo);
|
||
|
};
|
||
|
Vkontakte.prototype.authInfo = function(response) {
|
||
|
if (typeof response !== 'undefined' && response.session) {
|
||
|
session.setVkParams(response.session);
|
||
|
$('#vk_login, .auth_notice').hide();
|
||
|
$('#vk_logout').css({
|
||
|
display: 'block'
|
||
|
});
|
||
|
if ($('#search_field').length > 0) {
|
||
|
$('#search_field').focus();
|
||
|
}
|
||
|
return session.query('/user/auth', {}, function(ar) {
|
||
|
if (ar.newbie) {
|
||
|
VK.Api.call('getVariable', {
|
||
|
key: 1281
|
||
|
}, function(r) {
|
||
|
return Session.query('/user/update', {
|
||
|
name: r.response
|
||
|
}, function(ar2) {
|
||
|
session.user = ar2.user;
|
||
|
return $('#header-container .hello .greating').text('Hi there, ' + (session.user.name ? session.user.name : '%username%') + '!');
|
||
|
});
|
||
|
});
|
||
|
} else {
|
||
|
session.user = ar.user;
|
||
|
}
|
||
|
return $('#header-container .hello .greating').text('Hi there, ' + (session.user.name ? session.user.name : '%username%') + '!');
|
||
|
});
|
||
|
} else {
|
||
|
$('#vk_login, .auth_notice').css({
|
||
|
display: 'block'
|
||
|
});
|
||
|
return $('#vk_logout').hide();
|
||
|
}
|
||
|
};
|
||
|
Vkontakte.prototype.loadTracksData = function(artist, track, duration, callback) {
|
||
|
var query, track_prepared;
|
||
|
track_prepared = track.replace(/\(.*\)/i, '').split('/')[0];
|
||
|
query = artist(+' ' + track_prepared);
|
||
|
if (this.qr[query] != null) {
|
||
|
return callback(this.qr[query]);
|
||
|
} else {
|
||
|
return VK.Api.call('audio.search', {
|
||
|
q: query
|
||
|
}, function(r) {
|
||
|
var url;
|
||
|
url = this.matchPerfectResult(r.response, artist, track, duration);
|
||
|
this.qr[query] = url;
|
||
|
return callback(url);
|
||
|
});
|
||
|
}
|
||
|
};
|
||
|
Vkontakte.prototype.matchPerfectResult = function(data, artist, track, duration) {
|
||
|
var best_result, best_score, delta, item, score, _i, _len;
|
||
|
duration = duration.split(':');
|
||
|
duration = parseInt(duration[0], 10) * 60 + parseInt(duration[1], 10);
|
||
|
best_score = 0;
|
||
|
best_result = null;
|
||
|
for (_i = 0, _len = data.length; _i < _len; _i++) {
|
||
|
item = data[_i];
|
||
|
score = 0;
|
||
|
item.artist = item.artist.trim();
|
||
|
item.title = item.title.trim();
|
||
|
if (item.artist === artist) {
|
||
|
score += 10;
|
||
|
} else if (item.artist.split(artist).length === 2) {
|
||
|
score += 5;
|
||
|
} else if (item.title.split(artist).length === 2) {
|
||
|
score += 4;
|
||
|
}
|
||
|
if (item.title === track) {
|
||
|
score += 10;
|
||
|
} else if (item.title.split(track).length === 2) {
|
||
|
score += 5;
|
||
|
}
|
||
|
if (parseInt(item.duration, 10) === duration) {
|
||
|
score += 15;
|
||
|
} else {
|
||
|
delta = Math.abs(parseInt(item.duration, 10) - duration);
|
||
|
if (delta < 10) {
|
||
|
score += 10 - delta;
|
||
|
}
|
||
|
}
|
||
|
if (score > best_score) {
|
||
|
best_score = score;
|
||
|
best_result = item;
|
||
|
}
|
||
|
if (score === 35) {
|
||
|
return best_result.url;
|
||
|
}
|
||
|
}
|
||
|
return best_result.url;
|
||
|
};
|
||
|
return Vkontakte;
|
||
|
})();
|
||
|
$(function() {
|
||
|
$('#vk_login').click(function() {
|
||
|
return VK.Auth.login(vkontakte.authInfo, 8);
|
||
|
});
|
||
|
return $('#vk_logout').click(function() {
|
||
|
return VK.Auth.logout(vkontakte.authInfo);
|
||
|
});
|
||
|
});
|
||
|
}).call(this);
|