-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
61 lines (48 loc) · 1.82 KB
/
index.php
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
<?php
require "vendor/autoload.php";
use EventLoop\EventLoop;
use Rx\Observable;
use Rxnet\Redis\Redis;
require "API.php";
$api = new API();
$redis = new Redis();
$loop = EventLoop::getLoop();
$scheduler = new \Rx\Scheduler\EventLoopScheduler($loop);
$api->getKitties(0)
->flatMap(function($data) use ($redis, $api) {
$total = $data['total'] ?? null;
if ($total === null) {
throw new Exception("No result from Crypto Kitties API");
}
echo "{$total} Kitties\n";
return Observable::range(1, $total)
->flatMap(function($number) use ($api) {
echo "Call /kitties/$number\n";
return $api->getKitten($number)
->map(function($data) {
$image = $data['image_url'] ?? $data['image_url_cdn'];
$ext = $data['is_fancy'] && $data['is_exclusive'] ? 'png' : 'svg';
echo "Download {$data['id']}\n";
$file = fopen("images/{$data['id']}.{$ext}", 'w');
fwrite($file, file_get_contents($image));
return [
"key" => $data['id'],
"value" => $image
];
})
;
})
->doOnNext(function(array $data) use ($redis) {
return $redis
->connect('localhost:6379')
->doOnNext(function() use($redis, $data) {
echo "Save {$data['key']} to Redis\n";
$redis->set($data['key'], $data['value'])->subscribeCallback();
})
->subscribeCallback()
;
})
;
})
->subscribeCallback(null,null, null, $scheduler)
;