Skip to content

Instantly share code, notes, and snippets.

@claus
Last active April 12, 2021 04:10
Show Gist options
  • Save claus/dd909c81d6057fdd1a690e8f6aa92775 to your computer and use it in GitHub Desktop.
Save claus/dd909c81d6057fdd1a690e8f6aa92775 to your computer and use it in GitHub Desktop.

Revisions

  1. claus revised this gist Apr 12, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion outguess.js
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    In that folder, run:
    npm init
    npm i guess-file-type tmp
    npm i guess-file-type tmp png-js
    Change OUTGUESS_APP, WORK_DIR, SOURCE and VERBOSE below as you need them.
    Add your keys to the keys array below.
  2. claus revised this gist Apr 12, 2021. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions outguess.js
    Original file line number Diff line number Diff line change
    @@ -36,16 +36,16 @@ const allKeys = keys
    .flat()
    .filter((item, i, arr) => arr.indexOf(item) == i);

    console.log(`Testing ${allKeys.length} keys...`);

    outguessAll();

    async function outguessAll() {
    for (let i = 0; i < allKeys.length; i++) {
    await outguess(allKeys[i]);
    }
    }

    console.log(`Testing ${allKeys.length} keys...`);

    outguessAll();

    function outguess(key) {
    const promise = new Promise((resolve, reject) => {
    const out = tmp.fileSync();
  3. claus revised this gist Apr 12, 2021. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions outguess.js
    Original file line number Diff line number Diff line change
    @@ -36,15 +36,15 @@ const allKeys = keys
    .flat()
    .filter((item, i, arr) => arr.indexOf(item) == i);

    async function outguessAll() {
    for (let i = 0; i < allKeys.length; i++) {
    await outguess(allKeys[i]);
    }
    }

    console.log(`Testing ${allKeys.length} keys...`);

    allKeys.reduce(
    (acc, key) =>
    acc.then(() => {
    return outguess(key);
    }),
    Promise.resolve()
    );
    outguessAll();

    function outguess(key) {
    const promise = new Promise((resolve, reject) => {
  4. claus revised this gist Apr 12, 2021. 1 changed file with 13 additions and 9 deletions.
    22 changes: 13 additions & 9 deletions outguess.js
    Original file line number Diff line number Diff line change
    @@ -31,16 +31,20 @@ const keys = [
    'Zurich, Switzerland',
    ];

    keys.map(key => permutations(key))
    const allKeys = keys
    .map(key => permutations(key))
    .flat()
    .filter((item, i, arr) => arr.indexOf(item) == i)
    .reduce(
    (acc, key) =>
    acc.then(() => {
    return outguess(key);
    }),
    Promise.resolve()
    );
    .filter((item, i, arr) => arr.indexOf(item) == i);

    console.log(`Testing ${allKeys.length} keys...`);

    allKeys.reduce(
    (acc, key) =>
    acc.then(() => {
    return outguess(key);
    }),
    Promise.resolve()
    );

    function outguess(key) {
    const promise = new Promise((resolve, reject) => {
  5. claus revised this gist Apr 12, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion outguess.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /*
    Make sure you have Node.JS installed.
    Make sure you have Node.JS and Outguess.app installed.
    Copy this file as 'outguess.js' into an empty folder.
  6. claus revised this gist Apr 12, 2021. 1 changed file with 46 additions and 40 deletions.
    86 changes: 46 additions & 40 deletions outguess.js
    Original file line number Diff line number Diff line change
    @@ -1,35 +1,34 @@
    // $ npm init
    // $ npm i read-chunk image-type tmp
    // $ node outguess
    /*
    Make sure you have Node.JS installed.
    Copy this file as 'outguess.js' into an empty folder.
    In that folder, run:
    npm init
    npm i guess-file-type tmp
    Change OUTGUESS_APP, WORK_DIR, SOURCE and VERBOSE below as you need them.
    Add your keys to the keys array below.
    Then run:
    node outguess
    Note: guess-file-type might yield false positives
    */

    const { spawn } = require('child_process');
    const readChunk = require('read-chunk');
    const imageType = require('image-type');
    const fileGuesser = require('guess-file-type');
    const tmp = require('tmp');
    const fs = require('fs');

    const VERBOSE = true;
    const OUTGUESS_APP = '/Applications/Outguess.app/Contents/outguess';
    const WORK_DIR = '/Users/claus/Downloads/';
    const SOURCE = WORK_DIR + 'Syphon-Signal-Registered-VIVIVICIVICIIVIII.jpg';
    const VERBOSE = true;

    const keys = [
    'cyma',
    'berlin',
    'geneva, switzerland',
    'Le Locle, Neuchatel, Switzerland',
    'Jura, Mountains',
    'La Chaux de Fonds',
    'Watch Valley',
    'gorham, retro, railroad, museum',
    'Cascade, Alpine, Brook',
    'Moose, Brook',
    'Presidential rail trail',
    'Mt washington valley',
    'Mount washington, gorham',
    'Mount washington, new hampshire',
    'zurich, switzerland',
    'gorham, new hampshire',
    'Gorham, New Hampshire',
    'Zurich, Switzerland',
    ];

    keys.map(key => permutations(key))
    @@ -47,29 +46,25 @@ function outguess(key) {
    const promise = new Promise((resolve, reject) => {
    const out = tmp.fileSync();
    const child = spawn(OUTGUESS_APP, ['-k', key, '-r', SOURCE, out.name]);
    child.on('close', code => {
    const buffer = readChunk.sync(out.name, 0, 12);
    const type = imageType(buffer);
    if (type != null && typeof type === 'object') {
    const file = `${WORK_DIR}${key}.${type.ext}`;
    child.on('close', async code => {
    const mime = await fileGuesser.guessByFileSignature(out.name);
    if (mime != null && mime !== 'unknown') {
    const ext = fileGuesser.getExtensionFromMime(mime);
    const file = `${WORK_DIR}${key}.${ext}`;
    fs.renameSync(out.name, file);
    resolve({
    ...type,
    file,
    key,
    });
    resolve({ key, mime, file });
    return;
    }
    reject({ key, code });
    });
    });
    return promise
    .then(result => {
    console.log(`### ${result.key}: ${result.file}`);
    console.log(`🎉🎉 ${result.key}: ${result.file} (${result.mime})`);
    return result;
    })
    .catch(e => {
    VERBOSE && console.log(` ${e.key}: FAIL`);
    VERBOSE && console.log(`👎 ${e.key}`);
    return e;
    });
    }
    @@ -95,11 +90,22 @@ function permutator(inputArr) {
    }
    }
    };
    for (let i = inputArr.length; i > 0; i--) {
    for (let j = 0; j <= inputArr.length - i; j++) {
    const arr = inputArr.slice(j, j + i);
    permute(arr);
    }
    }
    combinations(inputArr).forEach(arr => permute(arr));
    return result;
    }

    function combinations(arr) {
    var fn = function (active, rest, a) {
    if (active.length === 0 && rest.length === 0) {
    return;
    }
    if (rest.length === 0) {
    a.push(active);
    } else {
    fn([...active, rest[0]], rest.slice(1), a);
    fn(active, rest.slice(1), a);
    }
    return a;
    };
    return fn([], arr, []);
    }
  7. claus revised this gist Apr 12, 2021. 1 changed file with 13 additions and 10 deletions.
    23 changes: 13 additions & 10 deletions outguess.js
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ const imageType = require('image-type');
    const tmp = require('tmp');
    const fs = require('fs');

    const VERBOSE = false;
    const VERBOSE = true;
    const OUTGUESS_APP = '/Applications/Outguess.app/Contents/outguess';
    const WORK_DIR = '/Users/claus/Downloads/';
    const SOURCE = WORK_DIR + 'Syphon-Signal-Registered-VIVIVICIVICIIVIII.jpg';
    @@ -28,17 +28,20 @@ const keys = [
    'Mt washington valley',
    'Mount washington, gorham',
    'Mount washington, new hampshire',
    'zurich, switzerland',
    'gorham, new hampshire',
    ];

    Promise.all(
    keys
    .map(key => permutations(key))
    .flat()
    .filter((item, i, arr) => arr.indexOf(item) == i)
    .map(key => outguess(key))
    ).then(() => {
    console.log('done.');
    });
    keys.map(key => permutations(key))
    .flat()
    .filter((item, i, arr) => arr.indexOf(item) == i)
    .reduce(
    (acc, key) =>
    acc.then(() => {
    return outguess(key);
    }),
    Promise.resolve()
    );

    function outguess(key) {
    const promise = new Promise((resolve, reject) => {
  8. claus revised this gist Apr 11, 2021. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion outguess.js
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,7 @@ const imageType = require('image-type');
    const tmp = require('tmp');
    const fs = require('fs');

    const VERBOSE = false;
    const OUTGUESS_APP = '/Applications/Outguess.app/Contents/outguess';
    const WORK_DIR = '/Users/claus/Downloads/';
    const SOURCE = WORK_DIR + 'Syphon-Signal-Registered-VIVIVICIVICIIVIII.jpg';
    @@ -20,6 +21,13 @@ const keys = [
    'Jura, Mountains',
    'La Chaux de Fonds',
    'Watch Valley',
    'gorham, retro, railroad, museum',
    'Cascade, Alpine, Brook',
    'Moose, Brook',
    'Presidential rail trail',
    'Mt washington valley',
    'Mount washington, gorham',
    'Mount washington, new hampshire',
    ];

    Promise.all(
    @@ -58,7 +66,7 @@ function outguess(key) {
    return result;
    })
    .catch(e => {
    console.log(` ${e.key}: FAIL`);
    VERBOSE && console.log(` ${e.key}: FAIL`);
    return e;
    });
    }
  9. claus revised this gist Apr 11, 2021. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion outguess.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    // $ npm init
    // $ npm i read-chunk image-type tmp
    // $ node outguess

    const { spawn } = require('child_process');
    const readChunk = require('read-chunk');
    const imageType = require('image-type');
    @@ -86,6 +90,5 @@ function permutator(inputArr) {
    permute(arr);
    }
    }
    console.log(result);
    return result;
    }
  10. claus created this gist Apr 11, 2021.
    91 changes: 91 additions & 0 deletions outguess.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    const { spawn } = require('child_process');
    const readChunk = require('read-chunk');
    const imageType = require('image-type');
    const tmp = require('tmp');
    const fs = require('fs');

    const OUTGUESS_APP = '/Applications/Outguess.app/Contents/outguess';
    const WORK_DIR = '/Users/claus/Downloads/';
    const SOURCE = WORK_DIR + 'Syphon-Signal-Registered-VIVIVICIVICIIVIII.jpg';

    const keys = [
    'cyma',
    'berlin',
    'geneva, switzerland',
    'Le Locle, Neuchatel, Switzerland',
    'Jura, Mountains',
    'La Chaux de Fonds',
    'Watch Valley',
    ];

    Promise.all(
    keys
    .map(key => permutations(key))
    .flat()
    .filter((item, i, arr) => arr.indexOf(item) == i)
    .map(key => outguess(key))
    ).then(() => {
    console.log('done.');
    });

    function outguess(key) {
    const promise = new Promise((resolve, reject) => {
    const out = tmp.fileSync();
    const child = spawn(OUTGUESS_APP, ['-k', key, '-r', SOURCE, out.name]);
    child.on('close', code => {
    const buffer = readChunk.sync(out.name, 0, 12);
    const type = imageType(buffer);
    if (type != null && typeof type === 'object') {
    const file = `${WORK_DIR}${key}.${type.ext}`;
    fs.renameSync(out.name, file);
    resolve({
    ...type,
    file,
    key,
    });
    return;
    }
    reject({ key, code });
    });
    });
    return promise
    .then(result => {
    console.log(`### ${result.key}: ${result.file}`);
    return result;
    })
    .catch(e => {
    console.log(` ${e.key}: FAIL`);
    return e;
    });
    }

    function permutations(input) {
    const rawKeys = input
    .split(',')
    .map(rawKey => rawKey.replace(/\s+/g, '').toLowerCase());
    const keys = permutator(rawKeys).map(key => key.join(''));
    return keys;
    }

    function permutator(inputArr) {
    const result = [];
    const permute = (arr, m = []) => {
    if (arr.length === 0) {
    result.push(m);
    } else {
    for (let i = 0; i < arr.length; i++) {
    let curr = arr.slice();
    let next = curr.splice(i, 1);
    permute(curr.slice(), m.concat(next));
    }
    }
    };
    for (let i = inputArr.length; i > 0; i--) {
    for (let j = 0; j <= inputArr.length - i; j++) {
    const arr = inputArr.slice(j, j + i);
    permute(arr);
    }
    }
    console.log(result);
    return result;
    }