Created
February 25, 2019 04:21
-
-
Save worm-emoji/ff2fa7035f8989b2d6e0faeab52f1ccf to your computer and use it in GitHub Desktop.
Migrates tumblr media folder to have file names
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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