downloads are sorted based on artist name (js)

This commit is contained in:
Anthony 2024-09-25 23:05:43 -04:00
parent 4f62717019
commit ebbb5520ef

View File

@ -24,20 +24,36 @@ async function main() {
fs.mkdirSync(downloadDir); fs.mkdirSync(downloadDir);
console.info(`📁 Created directory: ${downloadDir}`); console.info(`📁 Created directory: ${downloadDir}`);
} }
let fileIndex = 1;
for (const key in data) { for (const key in data) {
const subproperty = data[key]; const subproperty = data[key];
if (subproperty && subproperty.dhd) { if (subproperty && subproperty.dhd) {
const imageUrl = subproperty.dhd; const imageUrl = subproperty.dhd;
console.info(`🔍 Found image URL!`); console.info(`🔍 Found image URL!`, imageUrl);
await delay(100); await delay(100);
const ext = path.extname(new URL(imageUrl).pathname) || '.jpg';
const filename = `${fileIndex}${ext}`; const match = imageUrl.match(/\/content\/([^/]+)\//);
const filePath = path.join(downloadDir, filename); let artistName = match[1];
await downloadImage(imageUrl, filePath); artistName = artistName.replace(/^[a~]+|_[^_]+$/g, '');
console.info(`🖼️ Saved image to ${filePath}`); // Create folder with artist's name
fileIndex++; const artistDir = path.join(downloadDir, artistName);
await delay(250); if (!fs.existsSync(artistDir)) {
fs.mkdirSync(artistDir, { recursive: true });
}
// Extract and clean file name
const fileNameMatch = imageUrl.match(/\/([^/]+)\?/);
let cleanFileName = '';
if (fileNameMatch) {
let cleanFileName = fileNameMatch[1].replace(/~/g, ' ');
const filePath = path.join(artistDir, cleanFileName);
console.info('📂', filePath);
await downloadImage(imageUrl, filePath);
console.info(`🖼️ Saved image to ${filePath}`);
await delay(250);
}
} }
} }
} catch (error) { } catch (error) {