This commit is contained in:
magnolia-fan
2011-10-05 21:15:57 +04:00
parent 03ca130524
commit 49147fd80a
22 changed files with 5558 additions and 0 deletions
+20
View File
@@ -0,0 +1,20 @@
Copyright (c) 2011 Gregory Eremin
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+28
View File
@@ -0,0 +1,28 @@
# Vkontakte (VK.com) API music search tool
Don't forget to log in to [vk.com API](http://vk.com/developers.php?oid=-17680044&p=Open_API)
### CoffeeScript example
```coffeescript
vk_music = new VkontakteMusic
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()
false
```
### JavaScript example
```javascript
var vk_music;
vk_music = new VkontakteMusic;
vk_music.search("Kasabian", "L.S.F. (Lost Souls Forever)", "2:17", function(url) {
var audio;
audio = document.createElement("audio");
audio.setAttribute("src", url);
document.getElementsByTagName("body")[0].appendChild(audio);
audio.play();
return false;
});
```
@@ -0,0 +1,9 @@
/*
* Vkontakte (VK.com) API music search tool
* https://github.com/magnolia-fan/vkontakte_music_search
*
* Copyright 2011, Gregory Eremin
* Licensed under the MIT 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}()
@@ -0,0 +1,70 @@
###
* Vkontakte (VK.com) API music search tool
* https://github.com/magnolia-fan/vkontakte_music_search
*
* Copyright 2011, Gregory Eremin
* Licensed under the MIT license.
* https://raw.github.com/magnolia-fan/vkontakte_music_search/master/LICENSE
###
class window.VkontakteMusic
query_results: {}
search: (artist, track, duration, callback, return_all = false) ->
query = this.prepareQuery artist, track
if @query_results[query]? and not return_all
callback @query_results[query]
that = this
VK.Api.call 'audio.search', q: query, (r) ->
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
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(' ') isnt -1
str = str.replace ' ', ' '
if str.charAt(0) is ' '
str = str.substring 1
if str.charAt(str.length - 1) is ' '
str = str.substring 0, str.length - 1
str