Last active
November 16, 2019 10:50
-
-
Save getjump/dfe0afe65d7860f89eb3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
ini_set('memory_limit', '-1'); | |
require 'vendor/autoload.php'; | |
use Moor\TokenFileCache; | |
use GuzzleHttp\Pool; | |
use GuzzleHttp\Promise; | |
use Moor\UserVotes; | |
$timing = 0; | |
function tick() | |
{ | |
global $timing; | |
$timing = microtime(true); | |
} | |
function tack() | |
{ | |
global $timing; | |
print microtime(true) - $timing; | |
} | |
$guzzle = new GuzzleHttp\Client(['verify' => true]); | |
$cache = new TokenFileCache('cache.dat'); | |
$data = $cache->get(); | |
$n = count($data); | |
$queue = new SplQueue(); | |
$api = new SplQueue(); | |
foreach ($data as $token) { | |
$api->enqueue(new \Moor\AsyncVkApiCache($token, '5.5', $guzzle)); | |
} | |
$callback = function ($data) { | |
if (!isset($data->response[0])) | |
return; | |
print $data->response[0]->last_name . PHP_EOL; | |
}; | |
$promises = []; | |
$membersCallback = function ($response, &$data) { | |
if (isset($data->users) && is_array($data->users)) | |
$data->users = array_merge($data->users, $response->response->users); | |
else | |
$data = $response->response; | |
}; | |
$requests = new SplQueue(); | |
$deep = 0; | |
$matrix = []; | |
$groups = new SplQueue(); | |
$groups->enqueue(66228936); | |
$processed = []; | |
while(!$groups->isEmpty()) | |
{ | |
$group_id = $groups->dequeue(); | |
if(isset($processed[$group_id])) | |
continue; | |
$data = batch_get_all('groups.getMembers', ['group_id' => $group_id, 'count' => 1000], $api, $guzzle, $membersCallback); | |
foreach($data->users as $i => $u) | |
{ | |
print $i . "\r"; | |
$matrix[$group_id][$u] = UserVotes::WILL_GO; | |
$d = batch_get_all('groups.get', ['user_id' => $u, 'count' => 1000, 'extended' => 1], $api, $guzzle); | |
foreach(array_filter($d->items, function($x) { return $x->type == 'event'; }) as $ev) | |
{ | |
if(isset($processed[$group_id])) | |
continue; | |
$groups->enqueue($ev->id); | |
} | |
} | |
$data = batch_get_all('groups.getMembers', ['group_id' => $group_id, 'count' => 1000, 'filter' => 'unsure'], $api, $guzzle, $membersCallback); | |
foreach($data->users as $i => $u) | |
{ | |
print $i . "\r"; | |
$matrix[$group_id][$u] = UserVotes::NOT_KNOW; | |
$d = batch_get_all('groups.get', ['user_id' => $u, 'count' => 1000, 'extended' => 1], $api, $guzzle); | |
foreach(array_filter($d->items, function($x) { return $x->type == 'event'; }) as $ev) | |
{ | |
if(isset($processed[$group_id])) | |
continue; | |
$groups->enqueue($ev->id); | |
} | |
} | |
$deep++; | |
file_put_contents('data.txt', serialize($matrix)); | |
$processed[$group_id] = true; | |
} | |
print $deep; | |
function batch_get_all($method, $params, SplQueue $api, $guzzle, Callable $callback = NULL) | |
{ | |
$promises = []; | |
/** | |
* @var $vk \Moor\AsyncVkApi | |
*/ | |
$vk = $api->dequeue(); | |
$data = []; | |
if ($callback === NULL) | |
$callback = function ($response, &$data) { | |
if(!isset($response->response)) | |
{ | |
var_dump($response); | |
exit; | |
} | |
if (isset($data->items) && is_array($data->items)) | |
$data->items = array_merge($data->items, $response->response->items); | |
else | |
$data = $response->response; | |
}; | |
$vk->request($method, $params, $callback, $data)->wait(); | |
$size = $data->count; | |
$offset = $params['count']; | |
$api->enqueue($vk); | |
while (!$api->isEmpty() && $offset < $size) { | |
$vk = $api->dequeue(); | |
$offset += $params['count']; | |
$promises[] = function () use ($vk, $offset, $method, $params, $callback, &$data) { | |
$params['offset'] = $offset; | |
return $vk->request($method, $params, $callback, $data); | |
}; | |
$api->enqueue($vk); | |
} | |
if (count($promises) === 0) { | |
return $data; | |
} | |
/** | |
* @var $res GuzzleHttp\Psr7\Response[] | |
*/ | |
$pool = new Pool($guzzle, $promises, ['concurrency' => sizeof($api)]); | |
$pool->promise()->wait(); | |
return $data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment