Skip to content

Instantly share code, notes, and snippets.

@mokocup
Last active May 16, 2024 04:59
Show Gist options
  • Save mokocup/63518493cff48a9cb871535d8416bfe1 to your computer and use it in GitHub Desktop.
Save mokocup/63518493cff48a9cb871535d8416bfe1 to your computer and use it in GitHub Desktop.
Allow to fetch notes from user in another Misskey instances and sync it to selfhosted instances
fetchPost = async (userId, untilId) => {
let body = { userId, withRenotes: true, withReplies: true, withFiles: false, withChannelNotes: true, limit: 100, allowPartial: true }
if (untilId) {
body = { ...body, untilId }
}
const response = await fetch("/api/users/notes", {
method: "POST",
headers: {
"Content-Type": "application/json",
// 'Content-Type': 'application/x-www-form-urlencoded',
},
body: JSON.stringify(body)
})
return response.json();
}
wait = (time) =>
new Promise((r) => {
setTimeout(r, time);
});
fetchUser = async (userId) => {
let lastNoteId = 0;
const listNoteIds = new Set();
let responseNotes = await fetchPost(userId);
responseNotes.map(v => v.id).forEach(listNoteIds.add.bind(listNoteIds));
if (responseNotes.length > 0) {
do {
lastNoteId = responseNotes[responseNotes.length - 1].id
responseNotes = await fetchPost(userId, lastNoteId)
responseNotes.map(v => v.id).forEach(listNoteIds.add.bind(listNoteIds));
await wait(3000)
} while (responseNotes.length > 0 && lastNoteId != responseNotes[responseNotes.length - 1].id)
}
return listNoteIds
}
crawlPost = async (token, uri) => {
let body = { i: token, uri }
const response = await fetch("/api/ap/show", {
method: "POST",
headers: {
"Content-Type": "application/json",
// 'Content-Type': 'application/x-www-form-urlencoded',
},
body: JSON.stringify(body)
})
return response.json();
}
crawlUser = async (token, uriList) => {
for (let i = 0; i < uriList.length; i++) {
console.log(`Crawling: ${uriList[i]}`)
await crawlPost(token, uriList[i])
await wait(1000)
console.log(`Crawled: ${uriList[i]}`)
}
console.log(`Crawl Success: ${uriList.length} item`)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment