Skip to content

Instantly share code, notes, and snippets.

@SteveUpton
Last active July 3, 2023 16:10
Show Gist options
  • Save SteveUpton/b22cfa656b54da1ab47683d7663a6676 to your computer and use it in GitHub Desktop.
Save SteveUpton/b22cfa656b54da1ab47683d7663a6676 to your computer and use it in GitHub Desktop.
Replaces 'wand' with 'penis' in Harry Potter ebooks

Purpose

Takes a (DRM-free) Harry Potter epub file and replaces every instance of the word 'wand' with 'penis'. Inspired by this Buzzfeed listicle.

Usage

You'll need Node.js, so get that, then install the dependencies:

npm install

Then simply run the script, passing in a Harry Potter epub file as an argument. Assuming your epub files are in the same directory as the script:

node penisify.js "Chamber of Secrets.epub"

A new file named something like Chamber of Secrets.penisified.epub will be created and the original file will remain unaltered.

Notes

Shout-out to anyone selling DRM free ebooks and allowing nonsense like this to exist.

Error handling is basically non-existent, so don't be an idiot.

I'm using a mix of sync and async zip libraries because one of the libraries is broken and not going to be fixed. Don't judge me.

{
"name": "penisify",
"version": "1.0.0",
"description": "Takes a (DRM-free) Harry Potter epub file and replaces every instance of the word 'wand' with 'penis'.",
"repository": {
"url": "https://gist.github.com/b22cfa656b54da1ab47683d7663a6676.git"
},
"keywords": [
"harry potter",
"penis"
],
"author": "SteveUpton",
"license": "MIT",
"dependencies": {
"adm-zip": "^0.4.7",
"yazl": "^2.4.2"
}
}
const admZip = require('adm-zip');
const yazl = require('yazl');
const fs = require('fs');
const bookFileName = process.argv[2];
if (!bookFileName) {
console.log('Pass me a book, like so:');
console.log(' node penisify.js "Chamber of Secrets.epub"');
process.exit();
}
const book = new admZip(bookFileName);
const updated = new yazl.ZipFile();
book.getEntries().forEach(function(entry) {
if (entry.entryName.match(/\.(html|ncx)+$/i)) {
newEntryString = book.readAsText(entry)
.replace(/\bwand\b/g, 'penis')
.replace(/\bwands\b/g, 'penises')
.replace(/\bWand\b/g, 'Penis')
.replace(/\bWands\b/g, 'Penises');
updated.addBuffer(new Buffer(newEntryString), entry.entryName);
} else {
updated.addBuffer(book.readFile(entry), entry.entryName);
}
});
updated.end();
updatedName = bookFileName.replace(/\.epub+$/i, '.penisified.epub');
updated.outputStream.pipe(fs.createWriteStream(updatedName));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment