fedi_slurp/create_fedilist.php
2025-05-21 00:44:19 +00:00

118 lines
3.5 KiB
PHP
Executable file

#!/usr/bin/php
<?php
// === Load client credentials ===
$secrets = json_decode(file_get_contents(__DIR__ . '/_credentials/client_secret.json'), true);
$client = $secrets['installed'] ?? $secrets['web'] ?? null;
if (!$client || !isset($client['client_id'], $client['client_secret'])) {
die("Error: Invalid client_secret.json format.\n");
}
$clientId = $client['client_id'];
$clientSecret = $client['client_secret'];
// === cURL helper function ===
function curlPost($url, $data, $headers = []) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge([
'Content-Type: application/x-www-form-urlencoded',
'User-Agent: curl/7.64.1'
], $headers));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$response = curl_exec($ch);
if (curl_errno($ch)) {
die("cURL error: " . curl_error($ch) . "\n");
}
curl_close($ch);
return json_decode($response, true);
}
// === Create playlist helper ===
function createPlaylist($accessToken) {
$data = [
'snippet' => [
'title' => 'FediList',
'description' => 'A custom playlist for federated videos'
],
'status' => [
'privacyStatus' => 'private'
]
];
$ch = curl_init('https://www.googleapis.com/youtube/v3/playlists?part=snippet,status');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer $accessToken",
"Content-Type: application/json",
"User-Agent: curl/7.64.1"
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
if (curl_errno($ch)) {
die("cURL error: " . curl_error($ch) . "\n");
}
curl_close($ch);
$result = json_decode($response, true);
$result = json_decode($response, true);
if (isset($result['id'])) {
echo "FediList created!\nPlaylist ID: " . $result['id'] . "\n";
file_put_contents(__DIR__ . '/_credentials/fedilist_id.txt', $result['id']);
} else {
echo "Failed to create playlist:\n$response\n";
}
}
// === Step 1: Get device code ===
$deviceData = curlPost('https://oauth2.googleapis.com/device/code', [
'client_id' => $clientId,
'scope' => 'https://www.googleapis.com/auth/youtube'
]);
if (!isset($deviceData['user_code'])) {
die("Failed to get device code.\n");
}
echo "==== DEVICE AUTHORIZATION ====\n";
echo "Visit: " . $deviceData['verification_url'] . "\n";
echo "Enter code: " . $deviceData['user_code'] . "\n\n";
// === Step 2: Poll for token ===
$token = null;
$startTime = time();
while (true) {
sleep($deviceData['interval']);
$tokenResponse = curlPost('https://oauth2.googleapis.com/token', [
'client_id' => $clientId,
'client_secret' => $clientSecret,
'device_code' => $deviceData['device_code'],
'grant_type' => 'urn:ietf:params:oauth:grant-type:device_code'
]);
if (isset($tokenResponse['access_token'])) {
$token = $tokenResponse;
file_put_contents(__DIR__ . '/_credentials/token.json', json_encode($token));
break;
}
if (isset($tokenResponse['error']) && $tokenResponse['error'] !== 'authorization_pending') {
die("Auth error: " . $tokenResponse['error'] . "\n");
}
if (time() - $startTime > $deviceData['expires_in']) {
die("Authorization timed out.\n");
}
}
// === Step 3: Create FediList Playlist ===
createPlaylist($token['access_token']);
?>