Skip to content

Instantly share code, notes, and snippets.

@Aries0d0f
Last active August 13, 2021 06:03
Show Gist options
  • Save Aries0d0f/586da4c9d433fcb8cc1cfabccd0dd849 to your computer and use it in GitHub Desktop.
Save Aries0d0f/586da4c9d433fcb8cc1cfabccd0dd849 to your computer and use it in GitHub Desktop.
const version = 'v0.0.1-alpha'
const links = [...new Set(Array.from(document.querySelectorAll('.play-detail__title')).map(ele => ele.href))]
const maxOpenWindowsSameTime = 20
const totalRuntime = Math.ceil(links.length / maxOpenWindowsSameTime)
const offsetTime = 5000 // millionseconds
const oneRuntimeMaxTimeSpend = ((offsetTime * 2) * maxOpenWindowsSameTime) * 2 + offsetTime * 2
let holds = []
let ready = []
let relay = 0
const runtime = () => {
console.log(`
- ${links.length} beatsmaps found.
- ${totalRuntime} parts of download task.
- ${oneRuntimeMaxTimeSpend * totalRuntime / 1000} seconds taken (approximate).
OSU! Beatsmaps auto downloader by @Aries0d0f (aries0d0f.me)
Script version ${version}
`)
console.log(`Please keeps your browser window and console both open, and do not do any operation until all runtime finished.`)
Array(totalRuntime).fill(0).forEach((_, index) => {
setTimeout(() => {
console.log(`Now task runing ${relay + 1}/${totalRuntime}...`)
openBeatsmaps(relay * maxOpenWindowsSameTime, (relay + 1) * maxOpenWindowsSameTime)
relay += 1
}, oneRuntimeMaxTimeSpend * index)
})
}
const openBeatsmaps = (start, end) => {
console.log(`[${relay + 1}/${totalRuntime}] Fetching...`)
links
.slice(start, end)
.forEach((url, index) => {
setTimeout(() => { holds.push(window.open(url)) }, offsetTime * index + Math.random() * offsetTime)
})
setInterval(() => {
if (holds.length === maxOpenWindowsSameTime) {
preDownloadProcess()
}
}, offsetTime)
}
const preDownloadProcess = () => {
console.log(`[${relay + 1}/${totalRuntime}] Fetching down.`)
ready = [...new Set(holds.map(_window => `${_window.document.location.origin}${_window.document.location.pathname}`))]
if (maxOpenWindowsSameTime > ready.length) {
console.log(`[${relay + 1}/${totalRuntime}] Found ${maxOpenWindowsSameTime - ready.length} duplicate beatsmaps, shake it off.`)
}
console.log(`[${relay + 1}/${totalRuntime}] Clear workplace...`)
closeHolding()
setTimeout(() => {
downloadBeatsmaps()
}, offsetTime * 2)
}
const closeHolding = () => {
holds.forEach(_window => _window.close())
holds = []
console.log(`[${relay + 1}/${totalRuntime}] Workplace cleared`)
}
const downloadBeatsmaps = () => {
console.log(`[${relay + 1}/${totalRuntime}] Downloading...`)
ready.forEach((url, index) => {
setTimeout(() => { window.open(`${url}/download`) }, offsetTime * index + Math.random() * offsetTime)
})
}
runtime()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment