<?php
set_time_limit(0);
class friends_vkontakte{
public $my_friends=array();
public $friends=array();
public $pop_list=array();
public $avatars=array();
public $cookie='remixcomments_filter=photo_comments%2Cvideo_comments%2Ctopic_comments%2Cnote_comments; remixpass=89eed440b3f82fcf23ce60ccb7db9f45; remixemail=oleshkevich%40gmail.com; remixfilters=5; remixfriendsCommon=1; remixautobookmark=14; remixMembersBar=1; remixchk=5; remixmid=1276027';
public $id=1276027;
function get_data($data){
if(preg_match("|preparedFriendsArray = \[(\[.*?\])\]|ims",$data,$matches))
return $matches[1];
}
function get_avatars($data){
if(preg_match_all("|href=\"profile.php.*?\"><img src='(.*?)' alt='' />|ims",$data,$matches))
return $matches[0];
}
function parse_page($data){
if(preg_match_all("|\[([0-9]+), \'(.*?)\',.*?\]|ims",$data,$matches,PREG_SET_ORDER)){
$tmp=array();
foreach($matches as $v){
$tmp[$v[1]]=$v[2];
}
return $tmp;
}
return array();
}
private function get_music_page($id){
$url="http://vkontakte.ru/audio.php?id=$id";
print "download $url<br/>";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIE, $this->cookie);
$data=curl_exec($ch);
return $data;
}
function get_music($id){
$data=$this->get_music_page($id);
$pattern='|<div .*? id="audio.*?">.*?operate\(([0-9]+),([0-9]+),([0-9]+),\'([a-z0-9]+)\',([0-9]+)\).*?<b[^>]*.?>(.*?)</b>.*?<span[^>]*.?>(.*?)</span>|ims';
if(preg_match_all($pattern,$data,$matches,PREG_SET_ORDER)){
$tmp=array();
foreach($matches as $file){
$tmp[]=array(
"http://cs{$file[2]}.vkontakte.ru/u{$file[3]}/audio/{$file[4]}.mp3",
$file[6].' - '.strip_tags($file[7]));
}
}
return $tmp;
}
function get_my_music(){
return $this->get_music($this->id);
}
function get_friends($id){
$url="http://vkontakte.ru/friend.php?id=$id";
$path="friends/$id.dat";
if(is_readable($path)!=false/* && filemtime($path)<24*60*60*7*/){
$data=file_get_contents($path);
}
else{
$i=2;
do{
print "#".(log($i)/log(2))."<br>";
$i*=2;
$ch = curl_init($url);
print "download $url";
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIE, $this->cookie);
$data=curl_exec($ch);
sleep($i);
flush();
}while(stripos($data,"Location: blank.php") && stripos($data,"Страница скрыта"));
$data=$this->get_data($data);
//print $data;
file_put_contents($path,$data);
}
return $this->parse_page($data);
}
function add_to_black_list($id){
$url="http://vkontakte.ru/settings.php?act=addToBlackList&id=$id";
print "download $url<br />";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIE, $this->cookie);
$data=curl_exec($ch);
flush();
}
function del_from_black_list($id){
$url="http://vkontakte.ru/settings.php?act=delFromBlackList&id=$id";
print "download $url<br />";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIE, $this->cookie);
$data=curl_exec($ch);
flush();
}
function form_list($fr,$fr_id){
foreach($fr as $k=>$name){
if(isset($this->my_friends[$k]) || $k==$this->id )continue;
if(isset($this->pop_list[$k])){
$this->pop_list[$k]['num']++;
$this->pop_list[$k]['names'].=", ". $this->my_friends[$fr_id];
}
else{
$this->pop_list[$k]['num']=1;
$this->pop_list[$k]['names']=$this->my_friends[$fr_id];
}
}
}
function get_maybe_friends(){
$this->friends=$this->get_friends($this->id);
$this->my_friends=$this->friends;
$i=0;
foreach ($this->my_friends as $fr_id=>$name){
$i++;
$fr=$this->get_friends($fr_id);
$this->form_list($fr,$fr_id);
foreach($fr as $k=>$name){
if(!isset($this->friends[$k])){$this->friends[$k]=$name;}
}
}
uasort($this->pop_list,"compar_num");
// arsort($this->pop_list);
$i=0;
foreach($this->pop_list as $k=>$arr){
if($arr['num']>=1){
$i++;
print "$i. <a href='http://vkontakte.ru/profile.php?id=$k'>{$this->friends[$k]}</a> - {$arr['num']} ({$arr['names']}) <br>";
}
}
}
}
function compar_num($a, $b){
if ($a['num'] == $b['num']) {
return 0;
}
return ($a['num'] < $b['num']) ? 1 : -1;
}
$obj= new friends_vkontakte;
//print $obj->get_maybe_friends(); //получение друзей друзей
$mp3s=$obj->get_my_music();
$path = dirname(__FILE__).'/download_'.date('Y-m-d').'/';
if (!is_dir($path)) mkdir($path);
echo "--------------------------------\n
Total tracks: ".count($mp3s)."\n";
$i = 0;
foreach($mp3s as $k=>$mp3){
$i++;
echo "$i. {$mp3[1]}\nGetting {$mp3[0]}\n";
$track = file_get_contents($mp3[0]);
if ($track) {
echo " OK";
file_put_contents("$path/{$mp3[1]}.mp3", $track);
echo ", saved.\n\n";
} else {
echo " Error!\n\n";
}
//printf("<a href={$mp3[0]}>%03d - {$mp3[1]}</a><br />",$k+1); //печать ссылок
//printf("<a href={$mp3[0]}>%03d - {$mp3[1]}\n",$k+1); //печать ссылок
/*
//переименование
$filename=basename($mp3[0]);
$title=sprintf("%03d - {$mp3[1]}.mp3",$k+1);
$title=str_replace('\\','',$title);
$title=str_replace('/','',$title);
$title=str_replace(':','',$title);
if(is_writeable($path.$filename))
rename($path.$filename,$path.$title);
*/
}
?>