Skip to content

Example code that allows PHP to interact with Tutor from the CLI

David Chambers edited this page Feb 16, 2014 · 4 revisions

The script below will interact with Tutor and download all sets and all cards into individual files, grouped by set.

function queryTutor($commandParameters = '') {
    $tutorPath = '/usr/local/bin/tutor';
    $command = $tutorPath . ' --format ' . 'json' . $commandParameters;

    try {
        $json = exec(escapeshellcmd($command));
        return json_decode($json);
    }
    catch (Exception $e) {
        die($e->getMessage() . '\r\n');
    }
}

function queryTutotForCardsBySet($setName) {
    echo('searching for set name: ' . $setName . '\r\n');
    return queryTutor(' set ' . $setName);
}

function queryTutorForAllSets() {
    echo('searching for sets' . '\r\n');
    return queryTutor(' sets');
}

function writeAllCardsFromGathererToFile() {
    $allSets = queryTutorForAllSets();

    foreach ($allSets AS $set) {
        $filename = './cards/' . strtolower(str_replace(' ', '_', $set)) . '.json';
        if (!file_exists($filename)) {
            $cards = queryTutotForCardsBySet($set);
            file_put_contents($filename, json_encode($cards));
        }
    }
}

writeAllCardsFromGathererToFile();
Clone this wiki locally