mirror of
https://gitlab.com/Binaryify/neteasecloudmusicapi.git
synced 2025-05-23 22:37:41 +08:00
Merge pull request #1626 from qazxcdswe123/master
feat: auto retry failed upload (in module_example)
This commit is contained in:
commit
4d2343967d
@ -22,6 +22,30 @@ function getAllMusicFiles(dir, arrayOfFiles) {
|
|||||||
return arrayOfFiles
|
return arrayOfFiles
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function uploadArrayOfFile(token, arrayOfFiles) {
|
||||||
|
let failedFiles = []
|
||||||
|
let failed = 0
|
||||||
|
const fileCount = arrayOfFiles.length
|
||||||
|
for (let k in arrayOfFiles) {
|
||||||
|
const file = arrayOfFiles[k]
|
||||||
|
try {
|
||||||
|
await cloud({
|
||||||
|
songFile: {
|
||||||
|
name: path.basename(file),
|
||||||
|
data: fs.readFileSync(file),
|
||||||
|
},
|
||||||
|
cookie: token.body.cookie,
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error)
|
||||||
|
failed += 1
|
||||||
|
failedFiles.push(file)
|
||||||
|
}
|
||||||
|
console.log(`Uploaded ${k + 1}/${fileCount} songs`)
|
||||||
|
}
|
||||||
|
return { failedFiles, failed }
|
||||||
|
}
|
||||||
|
|
||||||
function getParsedArgs() {
|
function getParsedArgs() {
|
||||||
return yargs(process.argv.slice(2))
|
return yargs(process.argv.slice(2))
|
||||||
.option('country_code', {
|
.option('country_code', {
|
||||||
@ -54,36 +78,28 @@ function getParsedArgs() {
|
|||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const args = getParsedArgs()
|
const args = getParsedArgs()
|
||||||
const result = await login_cellphone({
|
const token = await login_cellphone({
|
||||||
countrycode: args.country_code,
|
countrycode: args.country_code,
|
||||||
phone: args.phone_number,
|
phone: args.phone_number,
|
||||||
password: args.password,
|
password: args.password,
|
||||||
})
|
})
|
||||||
const files = args.file
|
const files = args.file ? [args.file] : getAllMusicFiles(args.dir)
|
||||||
? getAllMusicFiles(args.file)
|
const fileCount = files.length
|
||||||
: getAllMusicFiles(args.dir)
|
|
||||||
let processed = 0
|
console.log(`Found ${fileCount} files, uploading...`)
|
||||||
let failed = 0
|
let res = await uploadArrayOfFile(token, files)
|
||||||
for (let k in files) {
|
if (res.failed) {
|
||||||
const file = files[k]
|
console.log(`Failed to upload ${res.failed} songs, retrying...`)
|
||||||
try {
|
res = await uploadArrayOfFile(token, res.failedFiles)
|
||||||
await cloud({
|
|
||||||
songFile: {
|
|
||||||
name: path.basename(file),
|
|
||||||
data: fs.readFileSync(file),
|
|
||||||
},
|
|
||||||
cookie: result.body.cookie,
|
|
||||||
})
|
|
||||||
} catch (error) {
|
|
||||||
console.log(error)
|
|
||||||
failed += 1
|
|
||||||
}
|
}
|
||||||
processed += 1
|
|
||||||
console.log(`Processed ${processed}/${files.length} songs...`)
|
console.log(`Uploaded ${fileCount - res.failed} songs`)
|
||||||
if (failed) {
|
console.log(
|
||||||
console.log(`Failed to upload ${failed} songs...`)
|
`Failed to upload ${res.failed} songs, you can reupload the files below`,
|
||||||
|
)
|
||||||
|
for (let k in res.failedFiles) {
|
||||||
|
console.log(res.failedFiles[k])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log('Finished!')
|
|
||||||
}
|
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user