1
0
Fork 0

Major improvements

This commit is contained in:
magnolia-fan 2011-09-15 23:02:34 +04:00
parent 812c2435f9
commit 87d6b209ac
2 changed files with 55 additions and 50 deletions

View File

@ -4,10 +4,11 @@ Don't forget to log in to [vk.com API](http://vk.com/developers.php?oid=-1768004
```coffeescript
vk_music = new VkontakteMusic
url = vk_music.search "Kasabian", "L.S.F. (Lost Souls Forever)", 197
audio = document.createElement "audio"
audio.setAttribute "src", url
document.getElementsByTagName("body")[0].appendChild audio
audio.play()
vk_music.search "Kasabian", "L.S.F. (Lost Souls Forever)", "2:17", (url) ->
audio = document.createElement "audio"
audio.setAttribute "src", url
document.getElementsByTagName("body")[0].appendChild audio
audio.play()
```
If you need a compiled JavaScript version, feel free to compile it yourself on [CoffeeScript's home page](http://jashkenas.github.com/coffee-script/).

View File

@ -1,58 +1,62 @@
class VkontakteMusic
query_results = {}
query_results: {}
search: (artist, track, duration, callback) ->
search: (artist, track, duration, callback, return_all = false) ->
query = this.prepareQuery artist, track
if @query_results[query]
if @query_results[query]? and not return_all
callback @query_results[query]
that = this
VK.Api.call 'audio.search', q: query, (r) ->
best_result = that.matchPerfectResult(r.response)
that.query_results[query] = best_result
callback best_result
results = that.range r.response, artist, track, duration
top_result = null
if results.length > 0
top_result = results[0].url
that.query_results[query] = results
callback if return_all then results else top_result
matchPerfectResult: (data, artist, track, duration) ->
duration = duration.split ':'
duration = parseInt(duration[0], 10) * 60 + parseInt(duration[1], 10)
best_score = 0;
best_result = null;
for item in data
if typeof item is 'object'
score = 0;
item.artist = item.artist.trim();
item.title = item.title.trim();
if item.artist.length > 0
if item.artist == artist
score += 10
else if item.artist.split(artist).length is 2
score += 5
else if item.title.split(artist).length is 2
score += 4
if item.artist.length > 0
if item.title == track
score += 10
else if item.title.split(track).length is 2
score += 5
if parseInt(item.duration, 10) == duration
score += 15
else
delta = Math.abs parseInt(item.duration, 10) - duration
score += (10 - delta) if delta < 10
if score > best_score
best_score = score
best_result = item.url
if score is 35
return best_result
best_result
range: (data, artist, track, duration) ->
if typeof duration is 'string'
duration = duration.split ':'
duration = parseInt(duration[0], 10) * 60 + parseInt(duration[1], 10)
for item, i in data
if typeof item isnt 'object'
continue
item.score = 0;
item.artist = this.trim(item.artist);
item.title = this.trim(item.title);
score = 0
if item.artist.length > 0
if item.artist == artist
score += 10
else if item.artist.split(artist).length is 2
score += 5
else if item.title.split(artist).length is 2
score += 4
if item.artist.length > 0
if item.title == track
score += 10
else if item.title.split(track).length is 2
score += 5
if parseInt(item.duration, 10) == duration
score += 15
else
delta = Math.abs parseInt(item.duration, 10) - duration
score += (10 - delta) if delta < 10
data[i].score = score
if data.length > 0 and typeof data[0] isnt 'object'
data.splice 0, 1
data.sort (a, b) ->
b.score - a.score
data
prepareQuery: (artist, track) ->
artist+" "+track.replace(/\(.*\)/i, '').split('/')[0]
trim: (str) ->
while str.indexOf(' ') != -1
str = str.replace(' ', ' ')
if str.charAt(0) == ' '
while str.indexOf(' ') isnt -1
str = str.replace ' ', ' '
if str.charAt(0) is ' '
str = str.substring 1
if str.charAt(str.length - 1) == ' '
str = str.substring(0, str.length - 1)
if str.charAt(str.length - 1) is ' '
str = str.substring 0, str.length - 1
str