139 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			139 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | ||
| 
 | ||
| /*****************************************************************
 | ||
| Пример использования:
 | ||
| 
 | ||
| $vk_parser = new Vkontakte($bot_name);
 | ||
| $vk_parser->parse('Blondie - Call Me');
 | ||
| $files = $vk_parser->getFiles();
 | ||
| *****************************************************************/
 | ||
| 
 | ||
| /**
 | ||
|  * Класс парсинга вконтактика
 | ||
|  *
 | ||
|  * @package classes
 | ||
|  * @author chez
 | ||
|  **/
 | ||
| class Vkontakte {
 | ||
| 
 | ||
| 	private $_cookies;	// Куки, ассоциативный массив
 | ||
| 	private $_query;	// Запрос, plain text
 | ||
| 
 | ||
| 	private $_html;		// HTML, полученый от вконтактика
 | ||
| 	private $_files;	// Распарсеные массивы с информацией о файле
 | ||
| 	
 | ||
| 	private $_bot_info;	// Информация о боте
 | ||
| 	
 | ||
| 	public function __construct($bot_name = false) {
 | ||
| 		if ($bot_name) {
 | ||
| 			$this->_bot_info = Config::get('bot:'. $bot_name);
 | ||
| 		}
 | ||
| 	}
 | ||
| 	
 | ||
| 	/**
 | ||
| 	 * Оболочка парсера
 | ||
| 	 *
 | ||
| 	 * @param string $q	Запрос
 | ||
| 	 * @return array	Массив с файлами
 | ||
| 	 * @author chez
 | ||
| 	 **/
 | ||
| 	public function getTracks($q) {
 | ||
| 		$this->_query = $q;
 | ||
| 		$this->auth();
 | ||
| 		$cookie = array();
 | ||
| 		foreach ($this->_cookies as $key => $val) {
 | ||
| 			$cookie[] = $key .'='. $val;
 | ||
| 		}
 | ||
| 		RemoteFile::setHeaders(array(
 | ||
| 			'Cookie: '. implode('; ', $cookie),
 | ||
| 			'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: '. $this->_bot_info['user_agent'],
 | ||
| 			'Connection: close'
 | ||
| 		));
 | ||
| 		$html = RemoteFile::getData('http://vkontakte.ru/audio', array(
 | ||
| 			'act'			=> 'search',
 | ||
| 			'al'			=> '1',
 | ||
| 			'gid'			=> '0',
 | ||
| 			'id'			=> $this->_bot_info['user_id'],
 | ||
| 			'offset'		=> '0',
 | ||
| 			'q'				=> urlencode($this->_query),
 | ||
| 			'sort'			=> '2'
 | ||
| 		));
 | ||
| 		$this->setHtml($html);
 | ||
| 		return (strlen($html) > 150);
 | ||
| 	}
 | ||
| 
 | ||
| 	/**
 | ||
| 	 * Пишет полученый html в член объекта
 | ||
| 	 *
 | ||
| 	 * @param string $html HTML
 | ||
| 	 * @return void
 | ||
| 	 * @author chez
 | ||
| 	 **/
 | ||
| 	public function setHtml($html) {
 | ||
| 		$this->_html = $html;
 | ||
| 	}
 | ||
| 
 | ||
| 	/**
 | ||
| 	 * Возвращает хранимый html
 | ||
| 	 *
 | ||
| 	 * @return string HTML
 | ||
| 	 * @author chez
 | ||
| 	 **/
 | ||
| 	public function getHtml() {
 | ||
| 		return $this->_html;
 | ||
| 	}
 | ||
| 
 | ||
| 	/**
 | ||
| 	 * Возвращает хранимый массив файлов
 | ||
| 	 *
 | ||
| 	 * @return array Список файлов
 | ||
| 	 * @author chez
 | ||
| 	 **/
 | ||
| 	public function getFiles() {
 | ||
| 		return $this->_files;
 | ||
| 	}
 | ||
| 
 | ||
| 	/**
 | ||
| 	 * Проводит авторизацию на вконтактике и получает куки
 | ||
| 	 * Пока костыль, потом нужно будет переписать
 | ||
| 	 *
 | ||
| 	 * @return array Ассоциативный массив с куками
 | ||
| 	 * @author chez
 | ||
| 	 **/
 | ||
| 	private function auth() {
 | ||
| 		$this->_cookies = array(
 | ||
| 			'remixchk' => $this->_bot_info['remixchk'],
 | ||
| 			'remixsid' => $this->_bot_info['remixsid'],
 | ||
| 			'remixlang' => 777
 | ||
| 		);
 | ||
| 	}
 | ||
| 
 | ||
| 	/**
 | ||
| 	 * Разбирает HTML, полученый от вконтактика
 | ||
| 	 *
 | ||
| 	 * @return array Список файлов
 | ||
| 	 * @author chez
 | ||
| 	 **/
 | ||
| 	public function parseHtml() {
 | ||
| 		preg_match_all('/<tr>(.*)<\/tr>/Usi', $this->_html, $m);
 | ||
| 		$files = array();
 | ||
| 		if (count($m) > 0) foreach ($m[0] as $res) {
 | ||
| 			$m1 = array();
 | ||
| 			preg_match('/<input.*value=\"(.*)\,(\d{2,4})\".*<div\sclass=\"duration.*>(.*)<\/div>.*<a.*>(.*)<\/a>.*\s-\s(.*)<\/div>/Usi', $res, $m1);
 | ||
| 			if (count($m1) > 0) {
 | ||
| 				$duration = explode(':', $m1[3]);
 | ||
| 				$files[] = array(
 | ||
| 					'url'		=> $m1[1],
 | ||
| 					'duration'	=> $duration[0] * 60 + $duration[1],
 | ||
| 					'artist'	=> strip_tags($m1[4]),
 | ||
| 					'track'		=> strip_tags($m1[5])
 | ||
| 				);
 | ||
| 			}
 | ||
| 		}
 | ||
| 		$this->_files = $files;
 | ||
| 	}
 | ||
| } |