On Thu, 23 Mar 2017, Martin Tarenskeen wrote:

lilybin infile.ly
---> process infile.ly without downloading from Lilybin server, returns an ID for download

lilybin -d ID.pdf
lilybin -d ID.midi
---> download results from Lilybin server

lilybin -a infile.ly outfile.pdf
---> upload infile.ly and save as outfile.pdf

lilybin -a -m infile.ly outfile.mid
---> upload infile.ly and save as outfile.mid


Hi, I have re-organised Sven's lilybin php script again. The -a and -m options are not needed and used anymore. You can now simply do:

lilybin infile.ly outfile.mid
lilybin infile.ly outfile.pdf

The outfile extension (.pdf, .mid, or .midi) will automatically tell the script what to download.

If only infile is specified, an ID will be returned that can be used with the -d option.

With the -u option the unstable/devel version of Lilypond on the Lilybin server will be used.

I think it's more intuitive this way. I have also rewritten the help text.

See attachment. I hope it's useful for someone.

--

Martin
#!/usr/bin/env php
<?php
/*
 * lilybin v0.2
 * Copyright (c)2017
 * contributors:
 * Sven Axelsson <[email protected]>
 * Martin Tarenskeen <[email protected]>
*/

function do_curl($url, $opts)
{
    $curl = curl_init($url);
    curl_setopt_array($curl, $opts);
    $result = curl_exec($curl);
    curl_close($curl);

    return $result;
}

$VERSION = "lilybin v0.2 (2017-03-27)" . PHP_EOL;
$opts = getopt('dhuv', $argv);
$sep = array_search('--', $argv);
$files = array_slice($argv, $sep ? ++$sep : count($opts) + 1);
$download = isset($opts['d']);
$unstable = isset($opts['u']);
$help = "Usage:" . PHP_EOL .
    "\tlilybin [-u] INFILE.ly OUTFILE.pdf" . PHP_EOL .
    "\tlilybin [-u] INFILE.ly OUTFILE.mid" . PHP_EOL .
    "\t\t=> Download and directly save result" . PHP_EOL .
    "\tlilybin [-u] INFILE.ly" . PHP_EOL .
    "\t\t=> Only print ID for download" . PHP_EOL .
    "\tlilybin -d (ID.pdf|ID.midi)" . PHP_EOL .
    "\t\t=> Download result from remote server" . PHP_EOL .
    "Other options:" . PHP_EOL .
    "\t-h print this Help message" . PHP_EOL .
    "\t-u use Unstable lilypond-devel version" . PHP_EOL;

if (count($files) === 0 || count($files) > 2) {
    echo $VERSION;
    die($help);
}

if (count($files) == 2) {
    $all = True;
    $ext = "." . pathinfo($files[1])['extension'];
    if ($ext == ".mid") {
        $ext = ".midi";
    }
} else {
    $all = False;
}

if ($unstable) {
    $stable = 'unstable';
} else {
    $stable = 'stable';
}

if ($all || !$download) {
    $url = 
'https://7icpm9qr6a.execute-api.us-west-2.amazonaws.com/prod/prepare_preview/' 
. $stable;
    $result = json_decode(do_curl($url, [
        CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
        CURLOPT_POST => 1,
        CURLOPT_POSTFIELDS => json_encode(['code' =>
file_get_contents($files[0])]),
        CURLOPT_RETURNTRANSFER => true,
    ]));
    if (!$result->id) {
        echo "ERROR:", PHP_EOL, $result->stderr;
        exit(1);
    }
    if ($all) {
        $files[0] = $result->id . $ext;
    } else {
        echo $argv[0], ' -d ', $result->id, ".(pdf|midi)", PHP_EOL;
        exit;
    }
}

if ($all || $download) {
    $url = 'https://s3-us-west-2.amazonaws.com/lilybin-scores/' . $files[0];
    $file = fopen(@$files[1] ?: $files[0], 'w');
    do_curl($url, [CURLOPT_FILE => $file]);
    fclose($file);
}

_______________________________________________
lilypond-user mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/lilypond-user

Reply via email to