Pushing into the Tube

Pushing into the Tube
Photo by Moritz Mentges / Unsplash

A PeerTube instance, 60 users to create, and an import feature not yet implemented. But a suggestion: "You can easily develop a script that create these users via API call".

Here, my script:

<?php

require_once('vendor/autoload.php');
use GuzzleHttp\Client;

$endpoint = 'https://my.peertube.server/api/v1/';
$username = 'MY_USERNAME';
$password = 'MY_PASSWORD';

$client = new Client();

$response = $client->request('GET', $endpoint . 'oauth-clients/local');
$response = json_decode($response->getBody());

$response = $client->request('POST', $endpoint . 'users/token', [
    'form_params' => [
        'client_id' => $response->client_id,
        'client_secret' => $response->client_secret,
        'grant_type' => 'password',
        'response_type' => 'code',
        'username' => $username,
        'password' => $password,
    ]
]);
$response = json_decode($response->getBody());

$f = fopen('users.csv', 'r');

while($row = fgetcsv($f)) {
    try {
        $client->request('POST', $endpoint . 'users', [
            'json' => [
                /*
                    Take care in sorting the CSV columns as required (or change the indexes here).
                    Remember: to set unlimited videoQuota, you must use the value -1
                */
                'username' => trim($row[0]),
                'password' => trim($row[1]),
                'email' => trim($row[2]),
                'videoQuota' => (int) $row[3],
                'videoQuotaDaily' => (int) $row[4],
                'role' => (int) $row[5],
            ],
                'headers' => [
                'Authorization' => 'Bearer ' . $response->access_token,
                'Accept' => 'application/json',
            ]
        ]);
    }
    catch(\Exception $e) {
        echo "Error: " . $e->getMessage() . "\n";
    }
}

You must have a users.csv file with (in this order) username, password, email, video quota, daily video quota and role. As documented, role may be 0 for admins, 1 for moderators, 2 for users. As not documented, videoQuota and videoQuotaDaily must be -1 for unlimited quota.

To execute, put both this script (with, on top, your own server URL, and valid credentials for an admin user) and your CSV file into a folder and execute:

composer require guzzlehttp/guzzle:^7.0
php peer.php