It fuckin' works\!
This commit is contained in:
		
							parent
							
								
									135f10d73a
								
							
						
					
					
						commit
						259cbd6a84
					
				@ -2,12 +2,16 @@ class TrackController < ApplicationController
 | 
			
		||||
  require 'net/http'
 | 
			
		||||
  require 'uri'
 | 
			
		||||
  def listen
 | 
			
		||||
    artist = 'The White Stripes'
 | 
			
		||||
    track = 'Blue Orchid'
 | 
			
		||||
    length = 157
 | 
			
		||||
    pp track = Track.where(:id => params[:id].to_i).first
 | 
			
		||||
    pp release = Release.where(:id => track.release_id).first
 | 
			
		||||
    pp album = Album.where(:id => release.album_id).first
 | 
			
		||||
    pp artist = Artist.where(:id => album.artist_id).first
 | 
			
		||||
    
 | 
			
		||||
    Vkontakte.get(artist, track, length)
 | 
			
		||||
    data = Vkontakte.get(artist.name, track.name, (track.length / 1000).round)
 | 
			
		||||
    #data = open('/Users/chez/Sites/beathaven/tmp/media.mp3').read
 | 
			
		||||
    #send_data data, :type=>"audio/mp3"
 | 
			
		||||
    
 | 
			
		||||
    render :json => 0
 | 
			
		||||
    #render :text => data
 | 
			
		||||
    redirect_to data
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
@ -1,40 +1,46 @@
 | 
			
		||||
module Vkontakte
 | 
			
		||||
  require 'uri'
 | 
			
		||||
  require 'net/http'
 | 
			
		||||
  require 'erb'
 | 
			
		||||
  #require 'nokogiri'
 | 
			
		||||
  
 | 
			
		||||
  @@accounts = YAML.load_file("#{Rails.root.to_s}/config/vk_accounts.yml")
 | 
			
		||||
  @@bot = nil
 | 
			
		||||
  
 | 
			
		||||
  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']
 | 
			
		||||
    self.randomBot
 | 
			
		||||
    html = self.getHtml(artist << ' - ' << track).force_encoding("WINDOWS-1251").encode("UTF-8")
 | 
			
		||||
    #pp html.encoding
 | 
			
		||||
    #File.open('/Users/chez/Sites/beathaven/tmp/vkres1.html', 'w') {|f| f.write(html) }
 | 
			
		||||
    #html = open('/Users/chez/Sites/beathaven/tmp/vkres1.html').read
 | 
			
		||||
    files = self.parseHtml(html)
 | 
			
		||||
    files = self.calcWeight(files, artist, track, length)
 | 
			
		||||
    #self.stream files.first['url']
 | 
			
		||||
    files.first['url']
 | 
			
		||||
  end
 | 
			
		||||
  
 | 
			
		||||
  
 | 
			
		||||
  private
 | 
			
		||||
  
 | 
			
		||||
  def self.randomBot
 | 
			
		||||
    @@accounts[@@accounts.keys[rand(@@accounts.keys.length - 1)]]
 | 
			
		||||
    @@bot = @@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,
 | 
			
		||||
      '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'],
 | 
			
		||||
			'User-Agent'        => @@bot['user_agent'],
 | 
			
		||||
			'Connection'        => 'close'
 | 
			
		||||
    }
 | 
			
		||||
    data = {
 | 
			
		||||
      'act'			=> 'search',
 | 
			
		||||
			'al'			=> 1,
 | 
			
		||||
			'gid'			=> 0,
 | 
			
		||||
			'id'			=> bot['user_id'],
 | 
			
		||||
			'id'			=> @@bot['user_id'],
 | 
			
		||||
			'offset'  => 0,
 | 
			
		||||
			'q'				=> q,
 | 
			
		||||
			'sort'		=> 2
 | 
			
		||||
@ -44,24 +50,22 @@ module Vkontakte
 | 
			
		||||
    
 | 
			
		||||
    http = Net::HTTP.new('vkontakte.ru', 80)
 | 
			
		||||
    resp, data = http.post('/audio', data_str.join('&'), headers)
 | 
			
		||||
    pp data
 | 
			
		||||
    data
 | 
			
		||||
  end
 | 
			
		||||
  
 | 
			
		||||
  def self.parseHtml html
 | 
			
		||||
    html = open('/Users/chez/Sites/beathaven/tmp/vkres.txt').read
 | 
			
		||||
    files = []
 | 
			
		||||
    html
 | 
			
		||||
      .gsub(/\\/, '')
 | 
			
		||||
      .scan(/<table.*?<input.*?value=\"(.*?)\,\d{2,4}\".*?>.*?<div\sclass=\"duration.*?>(.*?)<\/div>.*?<div\sclass=\"title_wrap\".*?>.*?selectPerformer\(event\,\s\'(.*?)\'\).*?<span\sclass=\"title\">(.*?)<\/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
 | 
			
		||||
      .scan(/<table.*?<input.*?value=\"(.*?)\,\d{1,4}\".*?>.*?<div\sclass=\"duration.*?>(.*?)<\/div>.*?<div\sclass=\"title_wrap\".*?>.*?selectPerformer\(event\,\s\'(.*?)\'\).*?<span\sclass=\"title\">(.*?)<\/span><\/div>.*?<\/table>/mi)
 | 
			
		||||
        .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
 | 
			
		||||
  
 | 
			
		||||
@ -90,6 +94,16 @@ module Vkontakte
 | 
			
		||||
  end
 | 
			
		||||
  
 | 
			
		||||
  def self.stream url
 | 
			
		||||
    headers = {
 | 
			
		||||
      'Cookie'            => 'remixsid='+ @@bot['remixsid'] + ';remixchk='+ @@bot['remixchk'].to_s,
 | 
			
		||||
			'Referer'           => 'http://vkontakte.ru/audio?album_id=0',
 | 
			
		||||
			'User-Agent'        => @@bot['user_agent'],
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    uri = URI.parse(url)
 | 
			
		||||
    http = Net::HTTP.new(uri.host, 80)
 | 
			
		||||
    resp, data = http.get2(uri.path, headers)
 | 
			
		||||
    
 | 
			
		||||
    data
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
@ -13,7 +13,7 @@
 | 
			
		||||
				<span id="artist"></span>
 | 
			
		||||
				<span id="dash"> - </span>
 | 
			
		||||
				<span id="title"></span>
 | 
			
		||||
				<audio controls preload style="width:1000px;"></audio>
 | 
			
		||||
				<audio controls preload autoplay style="width:1000px;"></audio>
 | 
			
		||||
			</div>
 | 
			
		||||
			<%= yield %>
 | 
			
		||||
		</div>
 | 
			
		||||
 | 
			
		||||
@ -1,12 +1,8 @@
 | 
			
		||||
$(function(){
 | 
			
		||||
	$('.play').click(function(){
 | 
			
		||||
		$.get('/listen/'+ $(this).parent().attr('id') +'/', function(res){
 | 
			
		||||
			if (res.status == 'ok') {
 | 
			
		||||
				$('#player audio').attr('src', res.data.url);
 | 
			
		||||
				$('#player #artist').html(res.data.artist);
 | 
			
		||||
				$('#player #title').html(res.data.track);
 | 
			
		||||
				$('#player audio').play();
 | 
			
		||||
			}
 | 
			
		||||
		})
 | 
			
		||||
		$('#player audio').attr('src', '/listen/'+ $(this).parent().attr('id')) +'/';
 | 
			
		||||
		$('#player #artist').html($('h1.artist').html());
 | 
			
		||||
		$('#player #title').html($(this).parent().find('.track-name').html());
 | 
			
		||||
		$('#player audio').play();
 | 
			
		||||
	})
 | 
			
		||||
})
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user