tangled
alpha
login
or
join now
t1c.dev
/
rocksky
forked from
rocksky.app/rocksky
2
fork
atom
A decentralized music tracking and discovery platform built on AT Protocol 🎵
2
fork
atom
overview
issues
pulls
pipelines
Fetch repository CAR once and reuse CarReader
tsiry-sandratraina.com
1 month ago
16e7fcc9
fe64557a
+35
-38
1 changed file
expand all
collapse all
unified
split
apps
cli
src
cmd
sync.ts
+35
-38
apps/cli/src/cmd/sync.ts
···
39
39
logger.info` DID: ${did}`;
40
40
logger.info` Handle: ${handle}`;
41
41
42
42
+
const carReader = await downloadCarFile(agent);
43
43
+
42
44
const [artists, albums, songs, scrobbles] = await Promise.all([
43
43
-
getRockskyUserArtists(agent),
44
44
-
getRockskyUserAlbums(agent),
45
45
-
getRockskyUserSongs(agent),
46
46
-
getRockskyUserScrobbles(agent),
45
45
+
getRockskyUserArtists(agent, carReader),
46
46
+
getRockskyUserAlbums(agent, carReader),
47
47
+
getRockskyUserSongs(agent, carReader),
48
48
+
getRockskyUserScrobbles(agent, carReader),
47
49
]);
48
50
49
51
logger.info` Artists: ${artists.length}`;
···
880
882
);
881
883
};
882
884
883
883
-
const getRockskyUserSongs = async (agent: Agent): Promise<Songs> => {
885
885
+
const downloadCarFile = async (agent: Agent) => {
886
886
+
logger.info(`Fetching repository CAR file ...`);
887
887
+
888
888
+
const repoRes = await agent.com.atproto.sync.getRepo({
889
889
+
did: agent.assertDid,
890
890
+
});
891
891
+
892
892
+
return CarReader.fromBytes(new Uint8Array(repoRes.data));
893
893
+
};
894
894
+
895
895
+
const getRockskyUserSongs = async (
896
896
+
agent: Agent,
897
897
+
carReader: CarReader,
898
898
+
): Promise<Songs> => {
884
899
const results: {
885
900
value: Song.Record;
886
901
uri: string;
···
888
903
}[] = [];
889
904
890
905
try {
891
891
-
logger.info(`Fetching repository CAR file for songs...`);
892
892
-
893
893
-
const repoRes = await agent.com.atproto.sync.getRepo({
894
894
-
did: agent.assertDid,
895
895
-
});
896
896
-
897
897
-
const carReader = await CarReader.fromBytes(new Uint8Array(repoRes.data));
898
906
const collection = "app.rocksky.song";
899
907
900
908
for await (const { cid, bytes } of carReader.blocks()) {
···
932
940
return results;
933
941
};
934
942
935
935
-
const getRockskyUserAlbums = async (agent: Agent): Promise<Albums> => {
943
943
+
const getRockskyUserAlbums = async (
944
944
+
agent: Agent,
945
945
+
carReader: CarReader,
946
946
+
): Promise<Albums> => {
936
947
const results: {
937
948
value: Album.Record;
938
949
uri: string;
···
940
951
}[] = [];
941
952
942
953
try {
943
943
-
logger.info(`Fetching repository CAR file for albums...`);
944
944
-
945
945
-
// Use getRepo to fetch the entire repository as a CAR file
946
946
-
const repoRes = await agent.com.atproto.sync.getRepo({
947
947
-
did: agent.assertDid,
948
948
-
});
949
949
-
950
950
-
// Parse the CAR file
951
951
-
const carReader = await CarReader.fromBytes(new Uint8Array(repoRes.data));
952
954
const collection = "app.rocksky.album";
955
955
+
logger.info`Extracting ${collection} records from CAR file ...`;
953
956
954
957
for await (const { cid, bytes } of carReader.blocks()) {
955
958
try {
···
984
987
return results;
985
988
};
986
989
987
987
-
const getRockskyUserArtists = async (agent: Agent): Promise<Artists> => {
990
990
+
const getRockskyUserArtists = async (
991
991
+
agent: Agent,
992
992
+
carReader: CarReader,
993
993
+
): Promise<Artists> => {
988
994
const results: {
989
995
value: Artist.Record;
990
996
uri: string;
···
992
998
}[] = [];
993
999
994
1000
try {
995
995
-
logger.info(`Fetching repository CAR file for artists...`);
996
996
-
997
997
-
const repoRes = await agent.com.atproto.sync.getRepo({
998
998
-
did: agent.assertDid,
999
999
-
});
1000
1000
-
1001
1001
-
const carReader = await CarReader.fromBytes(new Uint8Array(repoRes.data));
1002
1001
const collection = "app.rocksky.artist";
1002
1002
+
logger.info`Extracting ${collection} records from CAR file ...`;
1003
1003
1004
1004
for await (const { cid, bytes } of carReader.blocks()) {
1005
1005
try {
···
1034
1034
return results;
1035
1035
};
1036
1036
1037
1037
-
const getRockskyUserScrobbles = async (agent: Agent): Promise<Scrobbles> => {
1037
1037
+
const getRockskyUserScrobbles = async (
1038
1038
+
agent: Agent,
1039
1039
+
carReader: CarReader,
1040
1040
+
): Promise<Scrobbles> => {
1038
1041
const results: {
1039
1042
value: Scrobble.Record;
1040
1043
uri: string;
···
1042
1045
}[] = [];
1043
1046
1044
1047
try {
1045
1045
-
logger.info(`Fetching repository CAR file for scrobbles...`);
1046
1046
-
1047
1047
-
const repoRes = await agent.com.atproto.sync.getRepo({
1048
1048
-
did: agent.assertDid,
1049
1049
-
});
1050
1050
-
1051
1051
-
const carReader = await CarReader.fromBytes(new Uint8Array(repoRes.data));
1052
1048
const collection = "app.rocksky.scrobble";
1049
1049
+
logger.info`Extracting ${collection} records from CAR file ...`;
1053
1050
1054
1051
for await (const { cid, bytes } of carReader.blocks()) {
1055
1052
try {