Last active
May 16, 2024 04:59
-
-
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
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
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