module Vkontakte require 'uri' require 'net/http' require 'erb' require 'pp' #require 'nokogiri' @@accounts = YAML.load_file("#{Rails.root.to_s}/config/vk_accounts.yml") @@bot = nil def self.get(artist, track, length) self.randomBot() html = self.getHtml(artist << ' - ' << track).force_encoding("WINDOWS-1251").encode("UTF-8") files = self.parseHtml(html) files = self.calcWeight(files, artist, track, length) return { 'url' => files.first['url'], 'remixsid' => @@bot['remixsid'], 'remixchk' => @@bot['remixchk'] } end private def self.randomBot() botname = @@accounts.keys[rand(@@accounts.keys.length - 1)] @@bot = @@accounts[botname] pp 'Using bot ' << botname end def self.getHtml(q) 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) data end def self.parseHtml(html) files = [] html .scan(/