Skip to content

Commit

Permalink
adds flickr-clean script
Browse files Browse the repository at this point in the history
  • Loading branch information
davelandry committed Apr 13, 2021
1 parent c92896b commit 19fc6b8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"cube2csv": "node scripts/cube2csv.js",
"dev": "canon-dev",
"docs": "jsdoc2md '+(api|app|db|tools)/**/*.+(js|jsx)' --separators > DOCS.md",
"flickr-clean": "node scripts/flickr-clean.js",
"flickr": "node scripts/flickr-scrape.js",
"setup": "canon-setup",
"start": "node index.js",
Expand Down
54 changes: 54 additions & 0 deletions scripts/flickr-clean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#! /usr/bin/env node

const Sequelize = require("sequelize"),
fs = require("fs"),
path = require("path"),
shell = require("shelljs");

const dbName = process.env.CANON_DB_NAME;
const dbUser = process.env.CANON_DB_USER;
const dbHost = process.env.CANON_DB_HOST || "127.0.0.1";
const dbPw = process.env.CANON_DB_PW || null;

const db = new Sequelize(dbName, dbUser, dbPw,
{
host: dbHost,
dialect: "postgres",
define: {timestamps: true},
logging: () => {}
}
);

const dbFolder = path.join(__dirname, "../node_modules/@datawheel/canon-cms/src/db/");
fs.readdirSync(dbFolder)
.filter(file => file && file.indexOf(".") !== 0)
.forEach(file => {
const model = db.import(path.join(dbFolder, file));
db[model.name] = model;
});

async function run() {

const pagesWithImages = await db.search.findAll({where: {imageId: {[Sequelize.Op.ne]: null}}});
const usedImages = pagesWithImages.map(d => d.imageId);

const unusedImagesRows = await db.image.findAll({where: {id: {[Sequelize.Op.notIn]: usedImages}}});
const unusedImages = unusedImagesRows.map(d => d.id);

console.log(`Deleting ${unusedImages.length} unused images...`);

unusedImages.forEach(id => {
const splashPath = path.join(process.cwd(), `static/images/profile/splash/${id}.jpg`);
const thumbPath = path.join(process.cwd(), `static/images/profile/thumb/${id}.jpg`);
if (shell.test("-e", splashPath)) fs.unlinkSync(splashPath);
if (shell.test("-e", thumbPath)) fs.unlinkSync(thumbPath);
});

await db.image_content.destroy({where: {id: unusedImages}});
await db.image.destroy({where: {id: unusedImages}});

shell.exit(0);

}

run();

0 comments on commit 19fc6b8

Please sign in to comment.