<?php
require __DIR__ . 'pixelfed-php-master/vendor/autoload.php';

use Pixelfed\PixelfedApi;

$token = 'PUT TOKEN HERE';

$files = [];
foreach (glob('/Users/aleigh/pf/img' . '/*.*') as $file) {
    array_push($files, $file);
}

shuffle($files);

foreach ($files as &$f) {
    echo "Processing file. fn: [$f]\n";

    $api = new PixelfedApi('https://pixel.leigh-co.com', $token);
    $bin = file_get_contents($f);
    $data = $api->mediaUpload($bin);
    echo json_encode($data, JSON_PRETTY_PRINT);

    $id = $data['id'];
    echo "Post ID: $id\n";

// They are one-time use only... Or simply a bug in the client?
    $api = new PixelfedApi('https://pixel.leigh-co.com', $token);
    $data = $api->statusCreate(array($id), "");

    if ($data != null) {
        unlink($f);
    } else {
        exit('Unable to post to pixel.');
    }

    echo json_encode($data, JSON_PRETTY_PRINT);
}
?>
