Skip to content

Instantly share code, notes, and snippets.

@yukimochi
Last active January 5, 2024 01:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yukimochi/4e5cd1914e39643d5b5a4f57ee2ba3cc to your computer and use it in GitHub Desktop.
Save yukimochi/4e5cd1914e39643d5b5a4f57ee2ba3cc to your computer and use it in GitHub Desktop.
Misskey.io の自分のフォロイーの投稿を自分の Mastodon サーバーに同期的に挿入するやつ (取扱注意)
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()
})();
{
"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