Skip to content

Instantly share code, notes, and snippets.

@gpsnmeajp
Created May 9, 2023 16:48
Show Gist options
  • Save gpsnmeajp/c549330dfdbc0a11643b0f0a52f5aa53 to your computer and use it in GitHub Desktop.
Save gpsnmeajp/c549330dfdbc0a11643b0f0a52f5aa53 to your computer and use it in GitHub Desktop.
Blueskyでの指定アカウントの投稿を全部テキストファイルに出力するやつ
/*
npm init -y
"type": "module",をpackage.jsonに追記
npm i @ipld/dag-cbor
npm i multiformats
npm i @ipld/car
npm i @ipld/dag-pb
npm i @ipld/dag-json
node .\index.js
とかすればいいと思う。
output.txtに出力される。
*/
//改造元 : https://github.com/ipld/js-car/blob/af655e05137facba8ecfa9ca728e35a47594fafe/examples/dump-car.js
// ライセンスは右記に準じます: https://github.com/ipld/js-car/blob/af655e05137facba8ecfa9ca728e35a47594fafe/LICENSE
import fs from 'fs'
import { CarBlockIterator } from '@ipld/car/iterator'
import * as dagCbor from '@ipld/dag-cbor'
import * as dagPb from '@ipld/dag-pb'
import * as dagJson from '@ipld/dag-json'
import * as raw from 'multiformats/codecs/raw'
import * as json from 'multiformats/codecs/json'
// 所属サーバー
const xrpc_url = "https://bsky.social/xrpc/"
// 取得したい対象のHandle
const handle = "gpsnmeajp.bsky.social"
const codecs = {
[dagCbor.code]: dagCbor,
[dagPb.code]: dagPb,
[dagJson.code]: dagJson,
[raw.code]: raw,
[json.code]: json
}
function decode (cid, bytes) {
if (!codecs[cid.code]) {
throw new Error(`Unknown codec code: 0x${cid.code.toString(16)}`)
}
return codecs[cid.code].decode(bytes)
}
async function run () {
console.log("didを取得中..." + xrpc_url + " : "+ handle)
const did = await fetch(xrpc_url+"com.atproto.identity.resolveHandle?handle="+handle).then(response => response.json());
console.log("リポジトリを取得中..." + did["did"])
const data = await fetch(xrpc_url + "com.atproto.sync.getRepo?did="+did["did"]);
const arraybuf = await data.arrayBuffer();
const uint8data = new Uint8Array(arraybuf);
console.log("デコード中...")
const reader = await CarBlockIterator.fromBytes(uint8data);
console.log(`Version: ${reader.version}`)
console.log(`Roots: [${(await reader.getRoots()).map((r) => r.toString()).join(', ')}]`)
console.log('Blocks:')
let i = 1
let outputs = "";
let analyze_buf = []
for await (const { cid, bytes } of reader) {
const decoded = decode(cid, bytes)
if(decoded['$type'] === 'app.bsky.feed.post'){
analyze_buf.push(decoded)
}
}
console.log("並び替え...")
analyze_buf.sort(function(a, b){
return (a["createdAt"] > b["createdAt"] ? 1 : -1);
});
outputs += JSON.stringify(analyze_buf, null, ' ')
fs.writeFileSync('output.txt', outputs);
console.log("完了")
}
run().catch((err) => {
console.error(err)
process.exit(1)
})
@gpsnmeajp
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment