这是我的代码:
var idsClean = [];
function id(file) {
return new Promise((resolve, reject) => {
reader = new FileReader();
reader.onload = function(e) {
parsedLines = e.target.result.split(/\r|\n|\r\n/);
resolve(parsedLines);
};
reader.readAsText(file);
});
}
document.getElementById('fileInput').addEventListener('change', getReleasesFromFile);
function getReleasesFromFile(event) {
const file = event.target.files[0]
if (file === undefined) return
parseFile(file).then(idsDirty => {
idsClean = idsDirty.filter(id => id.trim().length > 0)
buildDocument(idsClean)
});
}
function parseFile(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = function (e) {
parsedLines = e.target.result.split(/\r|\n|\r\n/)
resolve(parsedLines)
}
reader.readAsText(file)
})
}
async function buildDocument(idsClean) {
const releaseData = []
for (const id of idsClean) {
const data = await getRelease(id)
releaseData.push(data)
}
const text = releaseData.join('\n')
saveCsv(text, 'output')
}
;(async function accessInterval( ) {
let accessInterval = 2400;
if(idsClean !== undefined) {
for ( const id of idsClean ) { // Wait until one has passed since the previous request was initiated
await new Promise( resolve => setTimeout( resolve, accessInterval ))
try {
lastRequest = new Date;
const release = await getRelease( id );
} catch ( err ) { // Handle request failure here,
// Depending on the error, perhaps add another delay and retry
// or loop up to some limit retrying on each iteration, increasing the delay each time you get a 429 error
// At a minimum just log the skipped release id with console.log( id );
} // end of catch
} // end of for loop
} // end of if statement
} )(); // end of async
function getRelease(id) {
return fetch(`https://api.discogs.com/releases/${id}`,
{
headers: {
'User-Agent': 'CSVforDiscogs/0.1',
}
})
.then(response => response.json())
.then(parseReleaseData)
}
function parseReleaseData(data) {
if (data.message === 'Release not found.')
return `Release with ID ${idFiltered} does not exist`
const id = data.id
const artists = data.artists ? data.artists.map(artist => artist.name) : []
const title = data.title || []
const format = data.formats[0].name || []
const format_qty = data.formats[0].qty || []
const format_descriptions = data.formats[0].descriptions || []
const format_descriptions_formatted = format_descriptions.map(description => description.replace(/"/g, "\"\""))
const label = data.labels[0].name || []
const catno = data.labels[0].catno || []
const identifiers = data.identifiers[0] || []
const barcode = data.identifiers.filter((item) => item.type === 'Barcode')
const barcode_values = barcode.map(barcode => barcode.value)
const country = data.country || 'Unknown'
const released = data.released_formatted || 'Unknown'
const genres = data.genres || []
const styles = data.styles || []
const tracklist = data.tracklist ? data.tracklist.map(track => track.title) : []
return [id, artists, title, label, catno, barcode_values, format, format_qty, '"' + format_descriptions_formatted + '"',
country, released, '"' + genres + '"', '"' + styles + '"', '"' + tracklist + '"'].join(',')
}
function saveCsv(text, fileName) {
const csvData = `data:text/csv;charset=utf-8,${text}`
const encodedUri = encodeURI(csvData)
const hiddenElement = document.createElement('a')
hiddenElement.href = encodedUri
hiddenElement.target = '_blank'
hiddenElement.download = `${fileName}.csv`
document.body.appendChild(hiddenElement)
hiddenElement.click()
hiddenElement.remove()
}
它适用于最多15行的较小文件,但是当我尝试任何较大文件时,会出现类似this ...
的错误获取https://api.discogs.com/releases/380504 429
访问CORS策略已阻止从源“空”获取“ https://api.discogs.com/releases/380504”处的访问:请求的资源上不存在“ Access-Control-Allow-Origin”标头。如果不透明的响应满足您的需求,请将请求的模式设置为“ no-cors”,以在禁用CORS的情况下获取资源。
未捕获(承诺)TypeError:无法获取
有人可以告诉我如何更好地控制它吗?
我正在访问的API的文档位于https://www.discogs.com/developers/
谢谢。