2011-03-31 02:01:16 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/*****************************************************************
|
|
|
|
|
Пример использования:
|
|
|
|
|
|
2011-04-02 16:44:45 +00:00
|
|
|
|
$vk_parser = new Vkontakte($bot_name);
|
2011-03-31 02:01:16 +00:00
|
|
|
|
$vk_parser->parse('Blondie - Call Me');
|
2011-04-01 06:37:23 +00:00
|
|
|
|
$files = $vk_parser->getFiles();
|
2011-03-31 02:01:16 +00:00
|
|
|
|
*****************************************************************/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Класс парсинга вконтактика
|
|
|
|
|
*
|
|
|
|
|
* @package classes
|
|
|
|
|
* @author chez
|
|
|
|
|
**/
|
|
|
|
|
class Vkontakte {
|
|
|
|
|
|
|
|
|
|
private $_cookies; // Куки, ассоциативный массив
|
|
|
|
|
private $_query; // Запрос, plain text
|
|
|
|
|
|
|
|
|
|
private $_html; // HTML, полученый от вконтактика
|
|
|
|
|
private $_files; // Распарсеные массивы с информацией о файле
|
|
|
|
|
|
2011-04-02 16:44:45 +00:00
|
|
|
|
private $_bot_info; // Информация о боте
|
|
|
|
|
|
2011-04-02 22:18:55 +00:00
|
|
|
|
public function __construct($bot_name = false) {
|
|
|
|
|
if ($bot_name) {
|
|
|
|
|
$this->_bot_info = Config::get('bot:'. $bot_name);
|
|
|
|
|
}
|
2011-04-02 16:44:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-31 02:01:16 +00:00
|
|
|
|
/**
|
|
|
|
|
* Оболочка парсера
|
|
|
|
|
*
|
|
|
|
|
* @param string $q Запрос
|
|
|
|
|
* @return array Массив с файлами
|
|
|
|
|
* @author chez
|
|
|
|
|
**/
|
2011-04-02 16:44:45 +00:00
|
|
|
|
public function getTracks($q) {
|
2011-03-31 02:01:16 +00:00
|
|
|
|
$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',
|
2011-04-02 16:44:45 +00:00
|
|
|
|
'User-Agent: '. $this->_bot_info['user_agent'],
|
2011-03-31 02:01:16 +00:00
|
|
|
|
'Connection: close'
|
|
|
|
|
));
|
2011-04-02 16:44:45 +00:00
|
|
|
|
$html = RemoteFile::getData('http://vkontakte.ru/audio', array(
|
|
|
|
|
'act' => 'search',
|
|
|
|
|
'al' => '1',
|
|
|
|
|
'gid' => '0',
|
|
|
|
|
'id' => $this->_bot_info['user_id'],
|
|
|
|
|
'offset' => '0',
|
2011-03-31 02:01:16 +00:00
|
|
|
|
'q' => urlencode($this->_query),
|
2011-04-02 16:44:45 +00:00
|
|
|
|
'sort' => '2'
|
|
|
|
|
));
|
|
|
|
|
$this->setHtml($html);
|
|
|
|
|
return (strlen($html) > 150);
|
2011-03-31 02:01:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Пишет полученый 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(
|
2011-04-02 16:44:45 +00:00
|
|
|
|
'remixchk' => $this->_bot_info['remixchk'],
|
|
|
|
|
'remixsid' => $this->_bot_info['remixsid'],
|
2011-04-02 14:57:17 +00:00
|
|
|
|
'remixlang' => 777
|
2011-03-31 02:01:16 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Разбирает 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;
|
|
|
|
|
}
|
|
|
|
|
}
|