Last active
January 5, 2024 01:45
-
-
Save yukimochi/4e5cd1914e39643d5b5a4f57ee2ba3cc to your computer and use it in GitHub Desktop.
Misskey.io の自分のフォロイーの投稿を自分の Mastodon サーバーに同期的に挿入するやつ (取扱注意)
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
const WebSocket = require("ws"); | |
const MISSKEYIO_APIKEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; | |
const MASTODON_APIKEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; | |
const MASTODON_DOMAIN = "https://mastodon.example.com"; | |
async function push(postUrl) { | |
const params = { | |
q: postUrl, | |
type: "statuses", | |
resolve: true | |
}; | |
const q = "?" + new URLSearchParams(params).toString(); | |
await (await fetch(MASTODON_DOMAIN + "/api/v2/search" + q, { | |
headers: { | |
Authorization: `Bearer ${MASTODON_APIKEY}`, | |
} | |
})).json(); | |
console.log(`Status Push Succeeded! : ${postUrl}`); | |
} | |
async function connect() { | |
const ws = new WebSocket(`wss://misskey.io/streaming?i=${MISSKEYIO_APIKEY}`, { | |
timeout: 1000 * 3600, | |
}); | |
function keepalive() { | |
ws.send("h"); | |
setTimeout(() => { keepalive() }, 15 * 1000); | |
} | |
ws.on("error", console.error) | |
ws.on("open", () => { | |
console.log("Connection to misskey.io is Established!") | |
const connect = { | |
type: 'connect', | |
body: { | |
channel: 'homeTimeline', | |
id: 'homeTimeline', | |
} | |
}; | |
ws.send(JSON.stringify(connect)); | |
keepalive(); | |
}); | |
ws.on("close", (e) => { | |
console.warn("Connection Closed.", e); | |
connect(); | |
}); | |
ws.on("message", async (data) => { | |
var status = JSON.parse(data.toString('utf8')); | |
if (status.body.id == "homeTimeline") { | |
if (status.body.body.renote) { | |
await push(`https://misskey.io/notes/${status.body.body.renote.id}`) | |
} | |
await push(`https://misskey.io/notes/${status.body.body.id}`) | |
} | |
}); | |
} | |
(() => { | |
connect() | |
})(); |
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
{ | |
"dependencies": { | |
"ws": "^8.16.0" | |
}, | |
"optionalDependencies": { | |
"bufferutil": "^4.0.8" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment