From 8dc8688d2e62da0d25106005478a994b6fa052fb Mon Sep 17 00:00:00 2001 From: Santiago Lema Date: Wed, 21 May 2025 02:11:08 +0000 Subject: [PATCH 1/4] Untrack files from _already_sent and keep folder with .gitkeep --- _already_sent/0cf7dedb7cfac4340056a612bc3c50b1.txt | 1 - _already_sent/12b2cc8c05f23758c6e812ef34044f53.txt | 1 - _already_sent/3f2721d52927f2d2c5f1146665b441dd.txt | 1 - _already_sent/8eef8d9550fb4952c4ef2ba2656b4038.txt | 1 - _already_sent/a3ffc5f64551046ad7132d159f1f40e7.txt | 1 - _already_sent/db41d26877002dfa9dba650122b8a298.txt | 1 - 6 files changed, 6 deletions(-) delete mode 100644 _already_sent/0cf7dedb7cfac4340056a612bc3c50b1.txt delete mode 100644 _already_sent/12b2cc8c05f23758c6e812ef34044f53.txt delete mode 100644 _already_sent/3f2721d52927f2d2c5f1146665b441dd.txt delete mode 100644 _already_sent/8eef8d9550fb4952c4ef2ba2656b4038.txt delete mode 100644 _already_sent/a3ffc5f64551046ad7132d159f1f40e7.txt delete mode 100644 _already_sent/db41d26877002dfa9dba650122b8a298.txt diff --git a/_already_sent/0cf7dedb7cfac4340056a612bc3c50b1.txt b/_already_sent/0cf7dedb7cfac4340056a612bc3c50b1.txt deleted file mode 100644 index a963820c..00000000 --- a/_already_sent/0cf7dedb7cfac4340056a612bc3c50b1.txt +++ /dev/null @@ -1 +0,0 @@ -https://www.openculture.com/2025/05/how-frank-lloyd-wrights-architecture-evolved-over-70-years-and-changed-america.html \ No newline at end of file diff --git a/_already_sent/12b2cc8c05f23758c6e812ef34044f53.txt b/_already_sent/12b2cc8c05f23758c6e812ef34044f53.txt deleted file mode 100644 index 83138f71..00000000 --- a/_already_sent/12b2cc8c05f23758c6e812ef34044f53.txt +++ /dev/null @@ -1 +0,0 @@ -https://en.wikipedia.org/wiki/Rainhill_trials \ No newline at end of file diff --git a/_already_sent/3f2721d52927f2d2c5f1146665b441dd.txt b/_already_sent/3f2721d52927f2d2c5f1146665b441dd.txt deleted file mode 100644 index e60c2b8a..00000000 --- a/_already_sent/3f2721d52927f2d2c5f1146665b441dd.txt +++ /dev/null @@ -1 +0,0 @@ -https://fossforce.com/2025/04/is-free-or-open-source-software-sustainable/ \ No newline at end of file diff --git a/_already_sent/8eef8d9550fb4952c4ef2ba2656b4038.txt b/_already_sent/8eef8d9550fb4952c4ef2ba2656b4038.txt deleted file mode 100644 index db0cfd4f..00000000 --- a/_already_sent/8eef8d9550fb4952c4ef2ba2656b4038.txt +++ /dev/null @@ -1 +0,0 @@ -https://manualdousuario.net/en/writing-chatgpt-ai/ \ No newline at end of file diff --git a/_already_sent/a3ffc5f64551046ad7132d159f1f40e7.txt b/_already_sent/a3ffc5f64551046ad7132d159f1f40e7.txt deleted file mode 100644 index 2e5d8665..00000000 --- a/_already_sent/a3ffc5f64551046ad7132d159f1f40e7.txt +++ /dev/null @@ -1 +0,0 @@ -https://goblackcat.com/feeling-exhausted/ \ No newline at end of file diff --git a/_already_sent/db41d26877002dfa9dba650122b8a298.txt b/_already_sent/db41d26877002dfa9dba650122b8a298.txt deleted file mode 100644 index 8b80b665..00000000 --- a/_already_sent/db41d26877002dfa9dba650122b8a298.txt +++ /dev/null @@ -1 +0,0 @@ -https://fenati.org.br/brasil-prepara-marco-regulatorio-para-data-centers-com-beneficios-fiscais-e-regras-sustentaveis/#datacenter \ No newline at end of file From b5adf7dbd076053bd738d2ef32d6200af3b14ba3 Mon Sep 17 00:00:00 2001 From: Santiago Lema Date: Wed, 21 May 2025 02:24:15 +0000 Subject: [PATCH 2/4] Handles token refrsh for oAUTH API --- add_to_fedilist.php | 48 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/add_to_fedilist.php b/add_to_fedilist.php index 74a2b7d5..df246b0d 100644 --- a/add_to_fedilist.php +++ b/add_to_fedilist.php @@ -8,24 +8,62 @@ function isYouTubeLink($url) { } function addVideoToFediList($videoUrl) { - - $playlistId = @file_get_contents(__DIR__ .'/_credentials/fedilist_id.txt'); + + $credentialsDir = __DIR__ . '/_credentials'; + $tokenPath = "$credentialsDir/token.json"; + $playlistId = @file_get_contents("$credentialsDir/fedilist_id.txt"); + if (!$playlistId) { echo "ā€¼ļø Error: FediList ID not found. Make sure fedilist_id.txt exists.\n"; return false; } - $token = @json_decode(file_get_contents(__DIR__ .'/_credentials/token.json'), true); + // === Load client credentials === + $secrets = json_decode(file_get_contents("$credentialsDir/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']; + + // === Load token === + $token = @json_decode(file_get_contents($tokenPath), true); if (!$token || !isset($token['access_token'])) { echo "ā€¼ļø Error: token.json missing or invalid. Authenticate first.\n"; return false; } - // Extract video ID from URL + // === Refresh token if expired === + if (isset($token['expires_at']) && time() >= $token['expires_at']) { + echo "šŸ”„ Access token expired. Refreshing...\n"; + + $refreshResponse = curlPost('https://oauth2.googleapis.com/token', [ + 'client_id' => $clientId, + 'client_secret' => $clientSecret, + 'refresh_token' => $token['refresh_token'], + 'grant_type' => 'refresh_token' + ]); + + if (isset($refreshResponse['access_token'])) { + $token['access_token'] = $refreshResponse['access_token']; + $token['expires_in'] = $refreshResponse['expires_in']; + $token['expires_at'] = time() + $refreshResponse['expires_in']; + file_put_contents($tokenPath, json_encode($token)); + echo "āœ… Token refreshed.\n"; + } else { + echo "ā€¼ļø Failed to refresh token: " . ($refreshResponse['error'] ?? 'unknown') . "\n"; + return false; + } + } + + // === Extract video ID === if (!preg_match('/(?:v=|\/)([a-zA-Z0-9_-]{11})/', $videoUrl, $matches)) { echo "ā‰ļø Invalid YouTube URL: $videoUrl\n"; return false; - } + } $videoId = $matches[1]; // === Step 1: Check if video is already in playlist === From dee25226765467409f8c67a2beef5317f95f3b41 Mon Sep 17 00:00:00 2001 From: Santiago Lema Date: Wed, 21 May 2025 02:36:26 +0000 Subject: [PATCH 3/4] handles Google refresh token --- add_to_fedilist.php | 12 +++++++++++- update_google_token.php | 9 +++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/add_to_fedilist.php b/add_to_fedilist.php index df246b0d..d9910859 100644 --- a/add_to_fedilist.php +++ b/add_to_fedilist.php @@ -35,9 +35,17 @@ function addVideoToFediList($videoUrl) { echo "ā€¼ļø Error: token.json missing or invalid. Authenticate first.\n"; return false; } + + //print_r($token); // === Refresh token if expired === - if (isset($token['expires_at']) && time() >= $token['expires_at']) { + if (isset($token['expires_at'])) { + $secondsLeft = $token['expires_at'] - time(); + if ($secondsLeft > 0) { + $minutes = floor($secondsLeft / 60); + $seconds = $secondsLeft % 60; + echo "ā³ Token expires in $minutes minutes and $seconds seconds.\n"; + } else { echo "šŸ”„ Access token expired. Refreshing...\n"; $refreshResponse = curlPost('https://oauth2.googleapis.com/token', [ @@ -58,6 +66,8 @@ function addVideoToFediList($videoUrl) { return false; } } +} + // === Extract video ID === if (!preg_match('/(?:v=|\/)([a-zA-Z0-9_-]{11})/', $videoUrl, $matches)) { diff --git a/update_google_token.php b/update_google_token.php index f183070a..d6fe5412 100755 --- a/update_google_token.php +++ b/update_google_token.php @@ -109,12 +109,13 @@ while (true) { ]); if (isset($tokenResponse['access_token'])) { - $token = $tokenResponse; - echo "Saving token.json\n"; - file_put_contents(__DIR__ . '/_credentials/token.json', json_encode($token)); - break; + $tokenResponse['expires_at'] = time() + $tokenResponse['expires_in']; + file_put_contents(__DIR__ . '/_credentials/token.json', json_encode($tokenResponse)); + echo "āœ… Token saved with expiration.\n"; + break; } + if (isset($tokenResponse['error']) && $tokenResponse['error'] !== 'authorization_pending') { die("Auth error: " . $tokenResponse['error'] . "\n"); } From 920c8932a0ecf7dc7ce89f795e3ab495c08ae520 Mon Sep 17 00:00:00 2001 From: Santiago Lema Date: Wed, 21 May 2025 03:05:53 +0000 Subject: [PATCH 4/4] cleaner login process --- add_to_fedilist.php | 12 ++++++++++-- update_google_token.php | 42 ++++++++++++++++------------------------- utils.php | 21 +++++++++++++++++++++ 3 files changed, 47 insertions(+), 28 deletions(-) create mode 100644 utils.php diff --git a/add_to_fedilist.php b/add_to_fedilist.php index d9910859..a9aff48a 100644 --- a/add_to_fedilist.php +++ b/add_to_fedilist.php @@ -1,8 +1,11 @@ 0) { + if ($secondsLeft > $refreshSecondMargin) { $minutes = floor($secondsLeft / 60); $seconds = $secondsLeft % 60; echo "ā³ Token expires in $minutes minutes and $seconds seconds.\n"; } else { - echo "šŸ”„ Access token expired. Refreshing...\n"; + echo "šŸ”„ Access token expired or will expire in less than $refreshSecondMargin seconds. (Seconds Left: $secondsLeft). Refreshing...\n"; $refreshResponse = curlPost('https://oauth2.googleapis.com/token', [ 'client_id' => $clientId, diff --git a/update_google_token.php b/update_google_token.php index d6fe5412..82ed8075 100755 --- a/update_google_token.php +++ b/update_google_token.php @@ -1,6 +1,8 @@ #!/usr/bin/php $clientId, @@ -110,22 +99,23 @@ while (true) { if (isset($tokenResponse['access_token'])) { $tokenResponse['expires_at'] = time() + $tokenResponse['expires_in']; - file_put_contents(__DIR__ . '/_credentials/token.json', json_encode($tokenResponse)); - echo "āœ… Token saved with expiration.\n"; + $path = __DIR__ . '/_credentials/token.json'; + file_put_contents($path, json_encode($tokenResponse)); + echo "\nāœ… Token saved as $path.\n"; break; } if (isset($tokenResponse['error']) && $tokenResponse['error'] !== 'authorization_pending') { - die("Auth error: " . $tokenResponse['error'] . "\n"); + die("\nAuth error: " . $tokenResponse['error'] . "\n"); } if (time() - $startTime > $deviceData['expires_in']) { - die("Authorization timed out.\n"); + die("\nAuthorization timed out.\n"); } } // === Step 3: Create FediList Playlist === -createPlaylist($token['access_token']); +createPlaylist($tokenResponse['access_token']); ?> diff --git a/utils.php b/utils.php new file mode 100644 index 00000000..39efbd04 --- /dev/null +++ b/utils.php @@ -0,0 +1,21 @@ +