php date_default_timezon e_set Europe Kiev define VK_USER_ID 53344747

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?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));
}