<?php
date_default_timezone_set('Europe/Kiev');
define('VK_USER_ID', '53344747');
define('VK_APP_ID', '4510068');
define('VK_APP_SECRET', '...');
define('VK_USER_SECRET', '...');
define('VK_USER_ACCESS_TOKEN', '...');
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_USERAGENT => "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.132 Safari/537.36 ",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "gzip",
CURLOPT_HEADER => false,
CURLOPT_COOKIE => '...'
));
function check_geo_stat_is_new(&$old, &$new) {
if (!$old || !$old['cities'])
return $new['cities'];
$new_cities = array();
foreach ($new['cities'] as $city => $cnt)
if (!isset($old['cities'][$city]) || $old['cities'][$city] != $cnt)
$new_cities[$city] = $cnt - (isset($old['cities'][$city]) ? $old['cities'][$city] : 0);
return $new_cities;
}
function chek_bdate($bdate, $sex, &$ages) {
$dt = explode(".", $bdate);
if (count($dt) != 3 || $sex == 0)
return true;
$age = DateTime::createFromFormat('d.m.Y', $bdate)->diff(new DateTime('now'))->y;
$sex_names = array(NULL, 'Ж', 'М');
foreach ($ages[$sex_names[$sex]] as $a => $cnt) {
if (preg_match("/(\d+)-(\d+)/", $a, $age_ranges) > 0) {
if ($age_ranges[1] <= $age && $age <= $age_ranges[2]) {
return true;
}
} elseif (preg_match("/(\d+)\+/", $a, $age_ranges) > 0) {
if ($age_ranges[1] <= $age)
return true;
}
}
return 1;
}
function find_users_by_city(&$friends, $city, $sex, $is_online = false, $ages = array()) {
$result = array();
foreach ($friends->response->items as $friend) {
if (isset($friend->city) && $friend->city->title == $city && ($sex == 0 || $friend->sex == $sex))
if (!$is_online || ($friend->online || (time() - $friend->last_seen->time < 15 * 60)))
if (!$ages || !isset($friend->bdate) || chek_bdate($friend->bdate, $friend->sex, $ages))
$result[] = $friend;
}
return $result;
}
$additional_users_id = array();
$user_id = VK_USER_ID;
$stat_file = "/tmp/last_geo_vk_".$user_id.".txt";
$last_date = NULL;
$last_stat = NULL;
if (file_exists($stat_file))
list($last_date, $last_stat) = unserialize(file_get_contents($stat_file));
$users_fields = 'city,country,online,last_seen,sex,bdate';
while (true) {
$dt = date("Y-m-d");
if ($last_date != $dt) {
echo "========== ".date("d/m/Y")." ==========\n";
$last_stat = array();
}
$last_date = $dt;
$stat = get_user_stat($ch, $user_id, $dt, $dt);
if ($stat === false) {
echo "fetch stat error\n";
sleep(5);
continue;
}
$stat = array_shift($stat);
$new_cities = NULL;
if (isset($stat['cities']))
$new_cities = check_geo_stat_is_new($last_stat, $stat);
if ($new_cities) {
$last_stat = $stat;
file_put_contents($stat_file, serialize(array($last_date, $last_stat)));
$tries = 100;
do {
$friends = vk('friends.get', ['fields' => $users_fields, 'user_id' => $user_id, 'order' => 'last_seen']);
if (!isset($friends->response, $friends->response->items)) {
if (!--$tries) {
var_dump($friends);
echo "something wrong!\n";
exit;
}
sleep(2);
} else
break;
} while (true);
if ($additional_users_id) {
$users = vk('users.get', ['fields' => $users_fields, 'user_ids' => implode(",", $additional_users_id)], true);
if (isset($users->response))
$friends->response->items = array_merge($friends->response->items, $users->response);
}
$sex = 0;
if ($stat['gender']['М'] != $stat['gender']['Ж']) {
if ($stat['gender']['М'] == 0)
$sex = 2;
elseif ($stat['gender']['Ж'] == 0)
$sex = 1;
}
echo "[".date("H:i:s d/m/Y")."] Зашли новые гости:\n";
foreach ($new_cities as $city => $cnt) {
$city_online_users = find_users_by_city($friends, $city, $sex, true, $stat['gender_age']);
if (!$city_online_users) {
echo " $city [$cnt]\n";
} else {
echo " $city [$cnt]:\n";
foreach ($city_online_users as $user)
echo " [".date("H:i:s", $user->last_seen->time)."] ".$user->first_name." ".$user->last_name."\n";
}
}
}
sleep(300);
}
function get_user_stat($ch, $user_id, $date_start, $date_end) {
curl_setopt($ch, CURLOPT_REFERER, 'https://vk.com/stats?mid='.$user_id);
$res = w_request($ch, 'https://vk.com/al_stats.php', [
'act' => 'export_stats_box',
'al' => 1,
'cid' => $user_id
]);
if (!preg_match('#hash" value="([^"]+)"#', $res, $hash) > 0) {
echo $res;
return false;
}
$hash = $hash[1];
list($y0, $m0, $d0) = explode("-", $date_start, 3);
list($y1, $m1, $d1) = explode("-", $date_end, 3);
$res = request($ch, 'https://vk.com/al_stats.php', [
'act' => 'export_stats',
'cid' => $user_id,
'hash' => $hash,
'format' => 1,
'check' => 0,
'date_from_day' => $d0,
'date_from_month' => $m0,
'date_from_year' => $y0,
'date_to_day' => $d1,
'date_to_month' => $m1,
'date_to_year' => $y1,
'data_types' => '0,1,2,5,6'
]);
$stat = array();
foreach (explode("\n", $res) as $i => $line) {
if (!$i || !$line)
continue;
$row = str_getcsv($line);
if (!isset($stat[$row[0]]))
$stat[$row[0]] = array();
if ($row[2] || (isset($stat[$row[0]][$row[1]]) && is_array($stat[$row[0]][$row[1]]))) {
if (!isset($stat[$row[0]][$row[1]]))
$stat[$row[0]][$row[1]] = array();
if (!isset($stat[$row[0]][$row[1]][$row[2]]))
$stat[$row[0]][$row[1]][$row[2]] = array();
if ($row[3] || (isset($stat[$row[0]][$row[1]][$row[2]][$row[3]]) && is_array($stat[$row[0]][$row[1]][$row[2]][$row[3]]))) {
if (!isset($stat[$row[0]][$row[1]][$row[2]][$row[3]]))
$stat[$row[0]][$row[1]][$row[2]][$row[3]] = array();
$stat[$row[0]][$row[1]][$row[2]][$row[3]] = $row[4];
} else
$stat[$row[0]][$row[1]][$row[2]] = $row[4];
} else
$stat[$row[0]][$row[1]] = $row[4];
}
return $stat;
}
function vk($method, $args, $open = false) {
$sig = '';
$args['v'] = '5.24';
$args['lang'] = 'ru';
if ($open) {
$args['app_id'] = VK_APP_ID;
$request_url = "/method/".$method."?".http_build_query($args);
$request_url .= "&sig=".md5($request_url.VK_APP_SECRET);
} else {
$args['access_token'] = VK_USER_ACCESS_TOKEN;
$request_url = "/method/".$method."?".http_build_query($args);
$request_url .= "&sig=".md5($request_url.VK_USER_SECRET);
}
return json_decode(file_get_contents("http://api.vk.com/".$request_url));
}
function request($ch, $url, $data = array()) {
curl_setopt($ch, CURLOPT_URL, $url);
if ($data) {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_POST, true);
}
$data = curl_exec($ch);
curl_setopt($ch, CURLOPT_POST, false);
return $data;
}
function w_request($ch, $url, $data = array()) {
return iconv("cp1251", "utf-8", request($ch, $url, $data));
}