supports multiple accounts, removed harcoded account on top, added _credentials_example

This commit is contained in:
Santiago Lema 2025-08-21 02:40:31 +00:00
parent 0d3b7594f7
commit 8b6d35779e
7 changed files with 84 additions and 10 deletions

View file

View file

@ -0,0 +1,2 @@
{"installed":{"client_id":"XYZ.apps.googleusercontent.com","project_id":"yourproject_with_youtube_access","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"XYZXYZ"}}

View file

@ -0,0 +1,2 @@
your.instance.org|XXXYYY
another.instance.com.br|XXXYYY

View file

@ -0,0 +1 @@
XXXXXXXYYYYYYYYY

View file

@ -0,0 +1 @@
your.readeck.fr|XXXXXYYYY

View file

@ -0,0 +1 @@
{"access_token":"XXXYYY","expires_in":3599,"refresh_token":"1\/\/AAABBBCCC","scope":"https:\/\/www.googleapis.com\/auth\/youtube","token_type":"Bearer","expires_at":1755745256}

View file

@ -1,31 +1,63 @@
#!/usr/bin/php
<?php
require("add_to_fedilist.php");
//-----------------------------
// CREDENTIALS
//-----------------------------
$MASTODON_TOKEN = '8beea62e32b336e5d934d06a21b0b996';
$MASTODON_HOST = 'go.lema.org';
$READECK_TOKEN = 'LDJb4YbGKe6Fp8cSygpuw5LjmwkgGTAbFbP77TQtYwe1hFZ4';
$READECK_HOST = 'read.lema.org';
$MINIMUM_TEXT_SIZE = 500; // article with less characters of content will be ignored
$fediAccounts = loadAccounts(__DIR__ . '/_credentials/fedi_accounts.txt');
$readeckAccount = loadAccounts(__DIR__ . '/_credentials/readeck_account.txt');
// _credentials/readeck_account.txt
// should have only one line with host|token
// ex: gone.lema.org|XXXXYYYXXXYYY
$acc = $readeckAccount[0];
$READECK_HOST = $acc['host'];
$READECK_TOKEN = $acc['token'];
echo "Readeck Host: $READECK_HOST \n";
echo "Fedi Accounts to loop: ".count($fediAccounts)."\n";
// _credentials/fedi_accountst.txt
// each line like with host|token
// ex: gotosocial.lema.org|XXXXYYYXXXYYY
foreach ($fediAccounts as $acc) {
$MASTODON_HOST = $acc['host'];
$MASTODON_TOKEN = $acc['token'];
echo "";
echo "";
echo "--------------------------------\n";
echo "Host: $MASTODON_HOST\n";
echo "Token: $MASTODON_TOKEN\n";
echo "--------------------------------\n";
echo "";
//-----------------------------
// FETCH MASTODON BOOKMARKS
//-----------------------------
echo "# Fetching mastodon / snac bookmarks...\n";
echo "# Fetching mastodon / gotosocial / snac bookmarks...\n";
date_default_timezone_set('America/Sao_Paulo');
echo date('Y-m-d H:i:s')."\n";
$ch = curl_init("https://$MASTODON_HOST/api/v1/bookmarks");
#GotoSocial will reply with error "I am a teapot" if no user agent is sent...
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERAGENT => "FediSlurperScript/1.0 (https://code.lema.org/santiago/fedi_slurp)",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer $MASTODON_TOKEN",
"Accept: application/json"
@ -40,6 +72,8 @@ if (!is_array($bookmarks)) {
echo "Found bookmarks:".count($bookmarks)."\n";
#print_r($bookmarks);
//-----------------------------
// FIND VALID URLs in posts
//-----------------------------
@ -62,12 +96,17 @@ foreach ($bookmarks as $status) {
}
}
if ($links)
if (isset($links))
{
echo "Valid URLS:".count($links)."\n";
else
echo "NO links founds \n";
print_r($links);
}
else
{
echo "NO links founds. Kthxbye \n";
die(0);
}
//-----------------------------
// SEND LINKS TO READECK
@ -92,7 +131,6 @@ if (!is_dir($alreadySentDir)) {
mkdir($alreadySentDir, 0755, true); // recursive mkdir
}
require("add_to_fedilist.php");
foreach ($links as $link) {
@ -177,3 +215,32 @@ if (isYouTubeLink($link)) {
}
curl_close($ch);
} // end accounts loop
function loadAccounts(string $filepath): array {
$accounts = [];
if (!file_exists($filepath)) {
return $accounts; // empty if file not found
}
$lines = file($filepath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
$line = trim($line);
if ($line === '') continue;
[$host, $token] = explode('|', $line, 2);
$accounts[] = [
'host' => $host,
'token' => $token
];
}
return $accounts;
}