Major improvements
This commit is contained in:
parent
812c2435f9
commit
87d6b209ac
13
README.md
13
README.md
|
@ -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,58 +1,62 @@
|
||||||
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) ->
|
||||||
duration = duration.split ':'
|
if typeof duration is 'string'
|
||||||
duration = parseInt(duration[0], 10) * 60 + parseInt(duration[1], 10)
|
duration = duration.split ':'
|
||||||
best_score = 0;
|
duration = parseInt(duration[0], 10) * 60 + parseInt(duration[1], 10)
|
||||||
best_result = null;
|
for item, i in data
|
||||||
for item in data
|
if typeof item isnt 'object'
|
||||||
if typeof item is 'object'
|
continue
|
||||||
score = 0;
|
item.score = 0;
|
||||||
item.artist = item.artist.trim();
|
item.artist = this.trim(item.artist);
|
||||||
item.title = item.title.trim();
|
item.title = this.trim(item.title);
|
||||||
if item.artist.length > 0
|
score = 0
|
||||||
if item.artist == artist
|
if item.artist.length > 0
|
||||||
score += 10
|
if item.artist == artist
|
||||||
else if item.artist.split(artist).length is 2
|
score += 10
|
||||||
score += 5
|
else if item.artist.split(artist).length is 2
|
||||||
else if item.title.split(artist).length is 2
|
score += 5
|
||||||
score += 4
|
else if item.title.split(artist).length is 2
|
||||||
if item.artist.length > 0
|
score += 4
|
||||||
if item.title == track
|
if item.artist.length > 0
|
||||||
score += 10
|
if item.title == track
|
||||||
else if item.title.split(track).length is 2
|
score += 10
|
||||||
score += 5
|
else if item.title.split(track).length is 2
|
||||||
if parseInt(item.duration, 10) == duration
|
score += 5
|
||||||
score += 15
|
if parseInt(item.duration, 10) == duration
|
||||||
else
|
score += 15
|
||||||
delta = Math.abs parseInt(item.duration, 10) - duration
|
else
|
||||||
score += (10 - delta) if delta < 10
|
delta = Math.abs parseInt(item.duration, 10) - duration
|
||||||
if score > best_score
|
score += (10 - delta) if delta < 10
|
||||||
best_score = score
|
data[i].score = score
|
||||||
best_result = item.url
|
if data.length > 0 and typeof data[0] isnt 'object'
|
||||||
if score is 35
|
data.splice 0, 1
|
||||||
return best_result
|
data.sort (a, b) ->
|
||||||
best_result
|
b.score - a.score
|
||||||
|
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