-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDownloadRecordCommand.php
93 lines (74 loc) · 2.75 KB
/
DownloadRecordCommand.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
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
<?php
declare(strict_types=1);
namespace Revolution\Bluesky\Console;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Revolution\Bluesky\Core\CBOR;
use Revolution\Bluesky\Core\CID;
use Revolution\Bluesky\Facades\Bluesky;
use Revolution\Bluesky\Support\AtUri;
/**
* Sample command to download the actor's Record. Download json directly instead of parsing CAR file.
*
* Specify collection with "-C" [default: "app.bsky.feed.post"]
* ```
* php artisan bluesky:download-record ***.bsky.social -C app.bsky.feed.post
* ```
*
* @link https://docs.bsky.app/blog/repo-export
*/
class DownloadRecordCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'bluesky:download-record {actor : DID or handle} {--C|collection=app.bsky.feed.post}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Download actor\'s Record. Does not require auth.';
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
$actor = $this->argument('actor');
$collection = (string) $this->option('collection');
$this->warn('Actor: '.$actor);
$this->warn('Collection: '.$collection);
$cursor = '';
do {
$response = Bluesky::client(auth: false)
->baseUrl(Bluesky::entryway().'/xrpc')
->listRecords(repo: $actor, collection: $collection, limit: 50, cursor: $cursor)
->throw();
$response->collect('records')->each(function (array $record) use ($actor, $collection) {
$at = AtUri::parse(data_get($record, 'uri'));
$cid = data_get($record, 'cid');
$block = data_get($record, 'value');
if (CID::verify(CBOR::encode($block), $cid)) {
$this->info('Verified');
} else {
$this->error('Verify failed');
}
$name = Str::slug($actor, dictionary: ['.' => '-', ':' => '-']);
$file = collect(['bluesky', 'download', $name, $collection, $at->rkey().'.json'])
->implode(DIRECTORY_SEPARATOR);
Storage::put($file, json_encode($record, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR));
$this->line('Download: '.Storage::path($file));
});
/** @var string $cursor */
$cursor = $response->json('cursor');
$this->warn('cursor: '.$cursor);
} while (filled($cursor));
$this->info('Download successful');
return 0;
}
}