78 lines
1.8 KiB
JavaScript
78 lines
1.8 KiB
JavaScript
const fs = require('fs/promises');
|
|
const thumbs = require('./thumbs.js');
|
|
|
|
const dir = process.argv[2];
|
|
const dest = process.argv[3];
|
|
|
|
fs.readdir(dir)
|
|
.then(async files => {
|
|
const list = [];
|
|
for (const file of files) {
|
|
const regex = /\.json/;
|
|
if (file.match(regex)) {
|
|
const data = await fs.readFile(`${dir}/${file}`);
|
|
const json = JSON.parse(data);
|
|
list.push(json);
|
|
}
|
|
}
|
|
return list;
|
|
})
|
|
.then(list => {
|
|
posts = {}
|
|
for (const element of list) {
|
|
const media = {
|
|
type: element.typename,
|
|
file: `${element.media_id}.${element.extension}`,
|
|
height: element.height,
|
|
width: element.width
|
|
};
|
|
|
|
if (element.post_id in posts) {
|
|
posts[element.post_id].media.push(media);
|
|
} else {
|
|
const newelem = {
|
|
date: element.date,
|
|
description: element.description,
|
|
likes: element.likes,
|
|
post: element.post_id,
|
|
media: [
|
|
media
|
|
]
|
|
};
|
|
|
|
if (element.tags)
|
|
newelem.tags = element.tags;
|
|
if (element.tagged_users)
|
|
newelem.users = element.tagged_users;
|
|
|
|
posts[element.post_id] = newelem;
|
|
}
|
|
}
|
|
return posts;
|
|
})
|
|
.then(async posts => {
|
|
const promises = Object.values(posts).map(async elem =>
|
|
fs.mkdir(`${dest}/${elem.post}`, { recursive: true })
|
|
.then(async () => {
|
|
for (const media of elem.media) {
|
|
let filename;
|
|
if (elem.media.length > 1) {
|
|
filename = `${dir}/${elem.post}_${media.file}`
|
|
} else {
|
|
filename = `${dir}/${media.file}`;
|
|
}
|
|
await fs.copyFile(filename, `${dest}/${elem.post}/${media.file}`);
|
|
thumbs.dothumb(`${dest}/${elem.post}`).then(() => console.log(`${elem.post} miniatura creada`));
|
|
}
|
|
})
|
|
.catch(console.error)
|
|
);
|
|
await Promise.all(promises);
|
|
return posts;
|
|
})
|
|
.then(posts => {
|
|
fs.writeFile(`${dest}/list.json`, JSON.stringify(posts, null, 4));
|
|
|
|
})
|
|
.catch(console.error);
|