-
-
Save gpsnmeajp/1655fbcf44a627b6bff203612618f62e to your computer and use it in GitHub Desktop.
Blueskyでの指定アカウントのお気に入り全部をURLにしてテキストファイルに出力するやつ。未知のdidはplc.directoryに問い合わせてハンドルを取得します。
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
/* | |
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 | |
とかすればいいと思う | |
*/ | |
//改造元 : 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.like'){ | |
analyze_buf.push(decoded) | |
} | |
} | |
console.log("並び替え...") | |
analyze_buf.sort(function(a, b){ | |
return (a["createdAt"] > b["createdAt"] ? 1 : -1); | |
}); | |
console.log("変換...") | |
//didをハンドルに変換する | |
//ブラウザのアドレスに変換する | |
let did2handle = {} | |
for(const block of analyze_buf){ | |
const uri = block["subject"]["uri"] | |
console.log(uri) | |
const splited_uri = uri.split("/") | |
const uri_did = splited_uri[2] | |
const post_id = splited_uri[4] | |
//取得処理 | |
let uri_handle = did2handle[uri_did] | |
if(uri_handle == undefined){ | |
const did_json = await fetch("https://plc.directory/"+uri_did).then(response => response.json()); | |
uri_handle = did_json["alsoKnownAs"][0].split("/")[2] | |
console.log("GET: "+uri_handle) | |
did2handle[uri_did] = uri_handle | |
} | |
console.log(uri_did) | |
console.log(post_id) | |
console.log(uri_handle) | |
outputs += "https://staging.bsky.app/profile/"+uri_handle+"/post/"+post_id+"\n" | |
} | |
fs.writeFileSync('output.txt', outputs); | |
console.log("完了") | |
} | |
run().catch((err) => { | |
console.error(err) | |
process.exit(1) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment