From 1aaae0c4e31d7daae5f478f8c0cf14dc38c6c145 Mon Sep 17 00:00:00 2001 From: Santiago Lema Date: Thu, 21 Aug 2025 03:02:34 +0000 Subject: [PATCH] clean up php with php-cs-fixer --- .php-cs-fixer.cache | 1 + fedi_slurp.php | 335 ++++++++++++++++++++++---------------------- 2 files changed, 167 insertions(+), 169 deletions(-) create mode 100644 .php-cs-fixer.cache diff --git a/.php-cs-fixer.cache b/.php-cs-fixer.cache new file mode 100644 index 00000000..bc404ecb --- /dev/null +++ b/.php-cs-fixer.cache @@ -0,0 +1 @@ +{"php":"8.2.29","version":"3.86.0:v3.86.0#4a952bd19dc97879b0620f495552ef09b55f7d36","indent":" ","lineEnding":"\n","rules":{"binary_operator_spaces":{"default":"at_least_single_space"},"blank_line_after_opening_tag":true,"blank_line_between_import_groups":true,"blank_lines_before_namespace":true,"braces_position":{"allow_single_line_empty_anonymous_classes":true},"class_definition":{"inline_constructor_arguments":false,"space_before_parenthesis":true},"compact_nullable_type_declaration":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_parentheses":true,"no_blank_lines_after_class_opening":true,"no_extra_blank_lines":{"tokens":["use"]},"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"imports_order":["class","function","const"],"sort_algorithm":"none"},"return_type_declaration":true,"short_scalar_cast":true,"single_import_per_statement":{"group_to_single_imports":false},"single_space_around_construct":{"constructs_followed_by_a_single_space":["abstract","as","case","catch","class","const_import","do","else","elseif","final","finally","for","foreach","function","function_import","if","insteadof","interface","namespace","new","private","protected","public","static","switch","trait","try","use","use_lambda","while"],"constructs_preceded_by_a_single_space":["as","else","elseif","use_lambda"]},"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"unary_operator_spaces":{"only_dec_inc":true},"visibility_required":true,"blank_line_after_namespace":true,"constant_case":true,"control_structure_braces":true,"control_structure_continuation_position":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"attribute_placement":"ignore","on_multiline":"ensure_fully_multiline"},"no_break_comment":true,"no_closing_tag":true,"no_multiple_statements_per_line":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_line_after_imports":true,"spaces_inside_parentheses":true,"statement_indentation":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true},"hashes":{"fedi_slurp.php":"2cbe2b549c703ecb4a3be4f2e15694f9"}} \ No newline at end of file diff --git a/fedi_slurp.php b/fedi_slurp.php index 1f8437e2..0c33318d 100755 --- a/fedi_slurp.php +++ b/fedi_slurp.php @@ -13,7 +13,7 @@ $MINIMUM_TEXT_SIZE = 500; // article with less characters of content will be ig $fediAccounts = loadAccounts(__DIR__ . '/_credentials/fedi_accounts.txt'); $readeckAccount = loadAccounts(__DIR__ . '/_credentials/readeck_account.txt'); -// _credentials/readeck_account.txt +// _credentials/readeck_account.txt // should have only one line with host|token // ex: gone.lema.org|XXXXYYYXXXYYY @@ -27,199 +27,197 @@ echo "Fedi Accounts to loop: ".count($fediAccounts)."\n"; // _credentials/fedi_accountst.txt -// each line like with host|token +// 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 ""; + echo ""; + echo ""; + echo "--------------------------------\n"; + echo "Host: $MASTODON_HOST\n"; + echo "Token: $MASTODON_TOKEN\n"; + echo "--------------------------------\n"; + echo ""; -//----------------------------- -// FETCH MASTODON BOOKMARKS -//----------------------------- -echo "# Fetching mastodon / gotosocial / snac bookmarks...\n"; -date_default_timezone_set('America/Sao_Paulo'); -echo date('Y-m-d H:i:s')."\n"; + //----------------------------- + // FETCH MASTODON BOOKMARKS + //----------------------------- + 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"); + $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)", + #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" - ] -]); - -$bookmarksJson = curl_exec($ch); -$bookmarks = json_decode($bookmarksJson, true); -if (!is_array($bookmarks)) { - die("❌ Failed to parse Mastodon bookmarks.\n"); -} - -echo "Found bookmarks:".count($bookmarks)."\n"; - -#print_r($bookmarks); - -//----------------------------- -// FIND VALID URLs in posts -//----------------------------- - -foreach ($bookmarks as $status) { - if (!isset($status['content'])) { - continue; - } - $content = strip_tags($status['content']); - preg_match_all('/https?:\/\/[^\s"<]+/', $content, $matches); - if (!empty($matches[0])) { - - $oneLink = $matches[0][0]; - if (filter_var($oneLink, FILTER_VALIDATE_URL)) { - $links[] = $oneLink; - } else { - // This happens for example if URL has an emoji at the end - echo "INVALID URL: $oneLink\n"; - } - } -} - -if (isset($links)) -{ -echo "Valid URLS:".count($links)."\n"; -print_r($links); -} -else -{ - echo "NO links founds. Kthxbye \n"; - die(0); - -} - -//----------------------------- -// SEND LINKS TO READECK -//----------------------------- - -$apiUrl = "https://$READECK_HOST/api/bookmarks"; - -$ch = curl_init(); -curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); -curl_setopt($ch, CURLOPT_POST, true); -curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0'); - -$headers = [ - "Authorization: Bearer $READECK_TOKEN", - 'Accept: application/json', - 'Content-Type: application/json' -]; - -$alreadySentDir = __DIR__ . "/_already_sent"; - -if (!is_dir($alreadySentDir)) { - mkdir($alreadySentDir, 0755, true); // recursive mkdir -} - - - -foreach ($links as $link) { - -if (isYouTubeLink($link)) { - addVideoToFediList($link); - continue; -} - - - - // READECK will accept several times the same URL ! - // Make sure we don't send it several times by keeping an archive here - $hash = md5($link); - $filePath = __DIR__ . "/_already_sent/{$hash}.txt"; - - if (file_exists($filePath)) { - echo "ℹ️ Already sent: $link\n"; - continue; - } - - - $options = [ - 'http' => [ - 'method' => 'GET', - 'header' => "User-Agent: Mozilla/5.0\r\n" - ] -]; - - // First check if page has content - //$ch = curl_init($link);; - curl_setopt($ch, CURLOPT_URL, $link); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - $content = curl_exec($ch); - - if ($content === false) { - echo "❌ Failed to fetch $link\n"; - continue; - } - $plainText = strip_tags($content); - - if (strlen($plainText) < $MINIMUM_TEXT_SIZE) { - echo "⚠️ Skipping $link\ncontent too small (".strlen($plainText)." chars < $MINIMUM_TEXT_SIZE )\n"; - continue; - } - - echo "🟢 Will add to Readeck $link\nLength: " . strlen($plainText)."\n"; - - //not passing title here, since we don't have it - $payload = json_encode([ - "labels" => ["automasto"], - "url" => $link + CURLOPT_HTTPHEADER => [ + "Authorization: Bearer $MASTODON_TOKEN", + "Accept: application/json" + ] ]); - curl_setopt($ch, CURLOPT_URL, $apiUrl); - curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + $bookmarksJson = curl_exec($ch); + $bookmarks = json_decode($bookmarksJson, true); + if (!is_array($bookmarks)) { + die("❌ Failed to parse Mastodon bookmarks.\n"); + } - $response = curl_exec($ch); - $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + echo "Found bookmarks:".count($bookmarks)."\n"; + #print_r($bookmarks); - if (curl_errno($ch)) { - echo "❌ Error adding $link: " . curl_error($ch) . "\n"; - } else { + //----------------------------- + // FIND VALID URLs in posts + //----------------------------- - // Store already sent file only if connection worked - file_put_contents($filePath, $link); + foreach ($bookmarks as $status) { + if (!isset($status['content'])) { + continue; + } + $content = strip_tags($status['content']); + preg_match_all('/https?:\/\/[^\s"<]+/', $content, $matches); + if (!empty($matches[0])) { - $json = json_decode($response, true); - if (json_last_error() === JSON_ERROR_NONE) { - if ($httpCode >= 200 && $httpCode < 300) { - echo "✅ [$httpCode] Successfully added: $link\n"; + $oneLink = $matches[0][0]; + if (filter_var($oneLink, FILTER_VALIDATE_URL)) { + $links[] = $oneLink; } else { - echo "⚠️ Server returned status $httpCode for $link\n"; + // This happens for example if URL has an emoji at the end + echo "INVALID URL: $oneLink\n"; } - } else { - echo "⚠️ Response is not valid JSON for $link: $response\n"; } } -} + if (isset($links)) { + echo "Valid URLS:".count($links)."\n"; + print_r($links); + } else { + echo "NO links founds. Kthxbye \n"; + die(0); -curl_close($ch); + } + + //----------------------------- + // SEND LINKS TO READECK + //----------------------------- + + $apiUrl = "https://$READECK_HOST/api/bookmarks"; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0'); + + $headers = [ + "Authorization: Bearer $READECK_TOKEN", + 'Accept: application/json', + 'Content-Type: application/json' + ]; + + $alreadySentDir = __DIR__ . "/_already_sent"; + + if (!is_dir($alreadySentDir)) { + mkdir($alreadySentDir, 0755, true); // recursive mkdir + } + + + + foreach ($links as $link) { + + if (isYouTubeLink($link)) { + addVideoToFediList($link); + continue; + } + + + + // READECK will accept several times the same URL ! + // Make sure we don't send it several times by keeping an archive here + $hash = md5($link); + $filePath = __DIR__ . "/_already_sent/{$hash}.txt"; + + if (file_exists($filePath)) { + echo "ℹ️ Already sent: $link\n"; + continue; + } + + + $options = [ + 'http' => [ + 'method' => 'GET', + 'header' => "User-Agent: Mozilla/5.0\r\n" + ] +]; + + // First check if page has content + //$ch = curl_init($link);; + curl_setopt($ch, CURLOPT_URL, $link); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $content = curl_exec($ch); + + if ($content === false) { + echo "❌ Failed to fetch $link\n"; + continue; + } + $plainText = strip_tags($content); + + if (strlen($plainText) < $MINIMUM_TEXT_SIZE) { + echo "⚠️ Skipping $link\ncontent too small (".strlen($plainText)." chars < $MINIMUM_TEXT_SIZE )\n"; + continue; + } + + echo "🟢 Will add to Readeck $link\nLength: " . strlen($plainText)."\n"; + + //not passing title here, since we don't have it + $payload = json_encode([ + "labels" => ["automasto"], + "url" => $link + ]); + + curl_setopt($ch, CURLOPT_URL, $apiUrl); + curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + + $response = curl_exec($ch); + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + + + if (curl_errno($ch)) { + echo "❌ Error adding $link: " . curl_error($ch) . "\n"; + } else { + + // Store already sent file only if connection worked + file_put_contents($filePath, $link); + + $json = json_decode($response, true); + if (json_last_error() === JSON_ERROR_NONE) { + if ($httpCode >= 200 && $httpCode < 300) { + echo "✅ [$httpCode] Successfully added: $link\n"; + } else { + echo "⚠️ Server returned status $httpCode for $link\n"; + } + } else { + echo "⚠️ Response is not valid JSON for $link: $response\n"; + } + } + + } + + curl_close($ch); } // end accounts loop -function loadAccounts(string $filepath): array { +function loadAccounts(string $filepath): array +{ $accounts = []; if (!file_exists($filepath)) { @@ -230,7 +228,9 @@ function loadAccounts(string $filepath): array { foreach ($lines as $line) { $line = trim($line); - if ($line === '') continue; + if ($line === '') { + continue; + } [$host, $token] = explode('|', $line, 2); $accounts[] = [ @@ -241,6 +241,3 @@ function loadAccounts(string $filepath): array { return $accounts; } - - -