1
0
Fork 0

Minor fixes

This commit is contained in:
Gregory Eremin 2011-10-12 12:36:20 +04:00
parent 26de82ee6b
commit 414371393c
2 changed files with 13 additions and 10 deletions

View File

@ -6,4 +6,5 @@
* Licensed under the MIT license. * Licensed under the MIT license.
* https://raw.github.com/magnolia-fan/vkontakte_music_search/master/LICENSE * https://raw.github.com/magnolia-fan/vkontakte_music_search/master/LICENSE
*/ */
var VkontakteMusic;VkontakteMusic=function(){function a(){}return a.prototype.query_results={},a.prototype.search=function(a,b,c,d,e){var f,g;return e==null&&(e=!1),f=this.prepareQuery(a,b),this.query_results[f]!=null&&!e&&d(this.query_results[f]),g=this,VK.Api.call("audio.search",{q:f},function(h){var i,j;return i=g.range(h.response,a,b,c),j=null,i.length>0&&(j=i[0].url),g.query_results[f]=i,d(e?i:j)})},a.prototype.range=function(a,b,c,d){var e,f,g,h,i;typeof d=="string"&&(d=d.split(":"),d=parseInt(d[0],10)*60+parseInt(d[1],10));for(f=0,i=a.length;f<i;f++){g=a[f];if(typeof g!="object")continue;g.score=0,g.artist=this.trim(g.artist),g.title=this.trim(g.title),h=0,g.artist.length>0&&(g.artist===b?h+=10:g.artist.split(b).length===2?h+=5:g.title.split(b).length===2&&(h+=4)),g.artist.length>0&&(g.title===c?h+=10:g.title.split(c).length===2&&(h+=5)),parseInt(g.duration,10)===d?h+=15:(e=Math.abs(parseInt(g.duration,10)-d),e<10&&(h+=10-e)),a[f].score=h}return a.length>0&&typeof a[0]!="object"&&(a.splice(0,1),a.sort(function(a,b){return b.score-a.score})),a},a.prototype.prepareQuery=function(a,b){return a+" "+b.replace(/\(.*\)/i,"").split("/")[0]},a.prototype.trim=function(a){while(a.indexOf(" ")!==-1)a=a.replace(" "," ");return a.charAt(0)===" "&&(a=a.substring(1)),a.charAt(a.length-1)===" "&&(a=a.substring(0,a.length-1)),a},a}()
window.VkontakteMusic=function(){function a(){}a.prototype.query_results={};a.prototype.search=function(a,b,c,d,e){var f,g;if(e==null){e=false}f=this.prepareQuery(a,b);if(this.query_results[f]!=null&&!e){d(this.query_results[f])}g=this;return VK.Api.call("audio.search",{q:f},function(h){var i,j;i=g.range(h.response,a,b,c);j=null;if(i.length>0){j=i[0].url}g.query_results[f]=i;return d(e?i:j)})};a.prototype.range=function(a,b,c,d){var e,f,g,h,i;if(typeof d==="string"){d=d.split(":");d=parseInt(d[0],10)*60+parseInt(d[1],10)}for(f=0,i=a.length;f<i;f++){g=a[f];if(typeof g!=="object"){continue}g.score=0;g.artist=this.trim(g.artist);g.title=this.trim(g.title);h=0;if(g.artist.length>0){if(g.artist===b){h+=10}else if(g.artist.split(b).length===2){h+=5}else if(g.title.split(b).length===2){h+=4}}if(g.artist.length>0){if(g.title===c){h+=10}else if(g.title.split(c).length===2){h+=5}}if(d!==0&&parseInt(g.duration,10)===d){h+=15}else{e=Math.abs(parseInt(g.duration,10)-d);if(e<10){h+=10-e}}a[f].score=h}if(a.length>0){if(typeof a[0]!=="object"){a.splice(0,1)}a.sort(function(a,b){return b.score-a.score})}return a};a.prototype.prepareQuery=function(a,b){return a+" "+b.replace(/\(.*\)/i,"").split("/")[0]};a.prototype.trim=function(a){while(a.indexOf(" ")!==-1){a=a.replace(" "," ")}if(a.charAt(0)===" "){a=a.substring(1)}if(a.charAt(a.length-1)===" "){a=a.substring(0,a.length-1)}return a};return a}()

View File

@ -6,6 +6,7 @@
* Licensed under the MIT license. * Licensed under the MIT license.
* https://raw.github.com/magnolia-fan/vkontakte_music_search/master/LICENSE * https://raw.github.com/magnolia-fan/vkontakte_music_search/master/LICENSE
### ###
class window.VkontakteMusic class window.VkontakteMusic
query_results: {} query_results: {}
@ -29,9 +30,9 @@ class window.VkontakteMusic
for item, i in data for item, i in data
if typeof item isnt 'object' if typeof item isnt 'object'
continue continue
item.score = 0; item.score = 0
item.artist = this.trim(item.artist); item.artist = this.trim(item.artist)
item.title = this.trim(item.title); item.title = this.trim(item.title)
score = 0 score = 0
if item.artist.length > 0 if item.artist.length > 0
if item.artist == artist if item.artist == artist
@ -45,14 +46,15 @@ class window.VkontakteMusic
score += 10 score += 10
else if item.title.split(track).length is 2 else if item.title.split(track).length is 2
score += 5 score += 5
if parseInt(item.duration, 10) == duration if duration != 0 and parseInt(item.duration, 10) == duration
score += 15 score += 15
else else
delta = Math.abs parseInt(item.duration, 10) - duration delta = Math.abs parseInt(item.duration, 10) - duration
score += (10 - delta) if delta < 10 score += (10 - delta) if delta < 10
data[i].score = score data[i].score = score
if data.length > 0 and typeof data[0] isnt 'object' if data.length > 0
data.splice 0, 1 if typeof data[0] isnt 'object'
data.splice(0, 1)
data.sort (a, b) -> data.sort (a, b) ->
b.score - a.score b.score - a.score
data data
@ -62,9 +64,9 @@ class window.VkontakteMusic
trim: (str) -> trim: (str) ->
while str.indexOf(' ') isnt -1 while str.indexOf(' ') isnt -1
str = str.replace ' ', ' ' str = str.replace(' ', ' ')
if str.charAt(0) is ' ' if str.charAt(0) is ' '
str = str.substring 1 str = str.substring(1)
if str.charAt(str.length - 1) is ' ' if str.charAt(str.length - 1) is ' '
str = str.substring 0, str.length - 1 str = str.substring(0, str.length - 1)
str str