Major improvements
This commit is contained in:
parent
812c2435f9
commit
87d6b209ac
|
@ -4,10 +4,11 @@ Don't forget to log in to [vk.com API](http://vk.com/developers.php?oid=-1768004
|
||||||
|
|
||||||
```coffeescript
|
```coffeescript
|
||||||
vk_music = new VkontakteMusic
|
vk_music = new VkontakteMusic
|
||||||
url = vk_music.search "Kasabian", "L.S.F. (Lost Souls Forever)", 197
|
vk_music.search "Kasabian", "L.S.F. (Lost Souls Forever)", "2:17", (url) ->
|
||||||
|
|
||||||
audio = document.createElement "audio"
|
audio = document.createElement "audio"
|
||||||
audio.setAttribute "src", url
|
audio.setAttribute "src", url
|
||||||
document.getElementsByTagName("body")[0].appendChild audio
|
document.getElementsByTagName("body")[0].appendChild audio
|
||||||
audio.play()
|
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/).
|
||||||
|
|
|
@ -1,26 +1,30 @@
|
||||||
class VkontakteMusic
|
class VkontakteMusic
|
||||||
query_results = {}
|
query_results: {}
|
||||||
|
|
||||||
search: (artist, track, duration, callback) ->
|
search: (artist, track, duration, callback, return_all = false) ->
|
||||||
query = this.prepareQuery artist, track
|
query = this.prepareQuery artist, track
|
||||||
if @query_results[query]
|
if @query_results[query]? and not return_all
|
||||||
callback @query_results[query]
|
callback @query_results[query]
|
||||||
that = this
|
that = this
|
||||||
VK.Api.call 'audio.search', q: query, (r) ->
|
VK.Api.call 'audio.search', q: query, (r) ->
|
||||||
best_result = that.matchPerfectResult(r.response)
|
results = that.range r.response, artist, track, duration
|
||||||
that.query_results[query] = best_result
|
top_result = null
|
||||||
callback best_result
|
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) ->
|
range: (data, artist, track, duration) ->
|
||||||
|
if typeof duration is 'string'
|
||||||
duration = duration.split ':'
|
duration = duration.split ':'
|
||||||
duration = parseInt(duration[0], 10) * 60 + parseInt(duration[1], 10)
|
duration = parseInt(duration[0], 10) * 60 + parseInt(duration[1], 10)
|
||||||
best_score = 0;
|
for item, i in data
|
||||||
best_result = null;
|
if typeof item isnt 'object'
|
||||||
for item in data
|
continue
|
||||||
if typeof item is 'object'
|
item.score = 0;
|
||||||
score = 0;
|
item.artist = this.trim(item.artist);
|
||||||
item.artist = item.artist.trim();
|
item.title = this.trim(item.title);
|
||||||
item.title = item.title.trim();
|
score = 0
|
||||||
if item.artist.length > 0
|
if item.artist.length > 0
|
||||||
if item.artist == artist
|
if item.artist == artist
|
||||||
score += 10
|
score += 10
|
||||||
|
@ -38,21 +42,21 @@ class VkontakteMusic
|
||||||
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
|
||||||
if score > best_score
|
data[i].score = score
|
||||||
best_score = score
|
if data.length > 0 and typeof data[0] isnt 'object'
|
||||||
best_result = item.url
|
data.splice 0, 1
|
||||||
if score is 35
|
data.sort (a, b) ->
|
||||||
return best_result
|
b.score - a.score
|
||||||
best_result
|
data
|
||||||
|
|
||||||
prepareQuery: (artist, track) ->
|
prepareQuery: (artist, track) ->
|
||||||
artist+" "+track.replace(/\(.*\)/i, '').split('/')[0]
|
artist+" "+track.replace(/\(.*\)/i, '').split('/')[0]
|
||||||
|
|
||||||
trim: (str) ->
|
trim: (str) ->
|
||||||
while str.indexOf(' ') != -1
|
while str.indexOf(' ') isnt -1
|
||||||
str = str.replace(' ', ' ')
|
str = str.replace ' ', ' '
|
||||||
if str.charAt(0) == ' '
|
if str.charAt(0) is ' '
|
||||||
str = str.substring 1
|
str = str.substring 1
|
||||||
if str.charAt(str.length - 1) == ' '
|
if str.charAt(str.length - 1) is ' '
|
||||||
str = str.substring(0, str.length - 1)
|
str = str.substring 0, str.length - 1
|
||||||
str
|
str
|
Loading…
Reference in New Issue