module Vkontakte require 'net/http' require 'erb' #require 'nokogiri' @@accounts = YAML.load_file("#{Rails.root.to_s}/config/vk_accounts.yml") def self.get artist, track, length #html = self.getHtml artist +' - '+ track files = self.parseHtml 1 #html files = self.calcWeight files, artist, track, length self.stream files.first['url'] end private def self.randomBot @@accounts[@@accounts.keys[rand(@@accounts.keys.length - 1)]] end def self.getHtml q bot = self.randomBot headers = { 'Cookie' => 'remixsid='+ bot['remixsid'] + ';remixchk='+ bot['remixchk'].to_s, 'Referer' => 'http://vkontakte.ru/audio?album_id=0', 'X-Requested-With' => 'XMLHttpRequest', 'Origin' => 'http://vkontakte.ru', 'Content-Type' => 'application/x-www-form-urlencoded', 'User-Agent' => bot['user_agent'], 'Connection' => 'close' } data = { 'act' => 'search', 'al' => 1, 'gid' => 0, 'id' => bot['user_id'], 'offset' => 0, 'q' => q, 'sort' => 2 } data_str = [] data.each {|k, v| data_str << k +'='+ ERB::Util.url_encode(v.to_s)} http = Net::HTTP.new('vkontakte.ru', 80) resp, data = http.post('/audio', data_str.join('&'), headers) pp data end def self.parseHtml html html = open('/Users/chez/Sites/beathaven/tmp/vkres.txt').read files = [] html .gsub(/\\/, '') .scan(/.*?(.*?)<\/div>.*?.*?selectPerformer\(event\,\s\'(.*?)\'\).*?(.*?)<\/span><\/div>.*?<\/table>/i) .each do |item| t = item[1].split(':') files << { 'url' => item[0], 'length' => (t[0].to_i * 60) + t[1].to_i, 'artist' => item[2], 'track' => item[3].gsub(/<\/?.*?>/, '') } end files end def self.calcWeight files, artist, track, length files.each do |file| weight = 0 if file['artist'] == artist then weight += 10 else if file['artist'].include? artist then weight += 5 else if file['track'].include? artist then weight += 4 end end end if file['track'] == track then weight += 10 else if file['track'].include? track then weight += 5 end end if file['length'] == length then weight +=10 else delta = (length - file['length']).abs if delta < 5 then weight += (5 - delta) end end file['weight'] = weight end files.sort_by{|file| file['weight']}.reverse end def self.stream url end end