Skip to content

Instantly share code, notes, and snippets.

@worm-emoji
Created February 25, 2019 04:21
Show Gist options
  • Save worm-emoji/ff2fa7035f8989b2d6e0faeab52f1ccf to your computer and use it in GitHub Desktop.
Save worm-emoji/ff2fa7035f8989b2d6e0faeab52f1ccf to your computer and use it in GitHub Desktop.
Migrates tumblr media folder to have file names
const fs = require("fs");
const cheerio = require("cheerio");
const moment = require("moment");
const posts = fs.readFileSync("./posts.xml");
const $ = cheerio.load(posts, { xmlMode: false });
const getNewFilename = existing => {
const id = existing.split(".")[0].split("_")[0];
const x = Number($(`#${id}`).attr("unix-timestamp"));
const time = moment.unix(x);
return time.format(`YYYY-MM-DD-HH-mm`) + `-${existing.split(".jpg")[0]}.jpg`;
};
fs.readdirSync("./media").forEach(file => {
if (file.indexOf(".jpg") === -1) {
return console.log(file);
}
fs.renameSync("./media/" + file, "./media/" + getNewFilename(file));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment