Add autofollow.php

This commit is contained in:
Ramses Revengeday 2024-08-16 14:49:33 +00:00
parent 22b2a45572
commit 2816a332a5

142
autofollow.php Normal file
View file

@ -0,0 +1,142 @@
<?php
// Set the access token (You should replace this with your own access token)
$accessToken = 'xxxx-xxxx-xxxx-xxxx';
// The base URL of your Mastodon server (Replace with the base URL of your Mastodon instance)
$baseUrl = 'https://corteximplant.com';
// Function to get the list of followers
function getFollowers($baseUrl, $accessToken) {
// First, get the account details to retrieve the account ID
$url = "$baseUrl/api/v1/accounts/verify_credentials";
$headers = [
'Authorization: Bearer ' . $accessToken,
];
// Initialize a cURL session and set the URL and headers
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Execute the cURL request and get the response
$response = curl_exec($ch);
// Close the cURL session
curl_close($ch);
// Decode the JSON response to get account data
$account = json_decode($response, true);
$accountId = $account['id']; // Get the account ID
// Now get the list of followers
$url = "$baseUrl/api/v1/accounts/$accountId/followers";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
// Return the list of followers
return json_decode($response, true);
}
// Function to get the list of users you are following
function getFollowing($baseUrl, $accessToken) {
// First, get the account details to retrieve the account ID
$url = "$baseUrl/api/v1/accounts/verify_credentials";
$headers = [
'Authorization: Bearer ' . $accessToken,
];
// Initialize a cURL session and set the URL and headers
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Execute the cURL request and get the response
$response = curl_exec($ch);
// Close the cURL session
curl_close($ch);
// Decode the JSON response to get account data
$account = json_decode($response, true);
$accountId = $account['id']; // Get the account ID
// Now get the list of users you are following
$url = "$baseUrl/api/v1/accounts/$accountId/following";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
// Return the list of users you are following
return json_decode($response, true);
}
// Function to follow a user by user ID
function followUser($baseUrl, $accessToken, $userId) {
$url = "$baseUrl/api/v1/accounts/$userId/follow";
$headers = [
'Authorization: Bearer ' . $accessToken,
];
// Initialize a cURL session, set the URL, headers, and method to POST
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Execute the cURL request and get the response
$response = curl_exec($ch);
// Close the cURL session
curl_close($ch);
// Return the response
return json_decode($response, true);
}
// Function to unfollow a user by user ID
function unfollowUser($baseUrl, $accessToken, $userId) {
$url = "$baseUrl/api/v1/accounts/$userId/unfollow";
$headers = [
'Authorization: Bearer ' . $accessToken,
];
// Initialize a cURL session, set the URL, headers, and method to POST
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Execute the cURL request and get the response
$response = curl_exec($ch);
// Close the cURL session
curl_close($ch);
// Return the response
return json_decode($response, true);
}
// Retrieve the list of followers and users you are following
$followers = getFollowers($baseUrl, $accessToken);
$following = getFollowing($baseUrl, $accessToken);
// Create a list of follower IDs
$followerIds = array_column($followers, 'id');
// Loop through the followers list and follow each follower
foreach ($followers as $follower) {
// Follow the user
$followResponse = followUser($baseUrl, $accessToken, $follower['id']);
// Output the account handle of the followed user
echo "Following user: " . $follower['acct'] . "\n";
}
// Loop through the following list and unfollow users who are no longer following you
foreach ($following as $followedUser) {
// If the user is not in the list of followers, unfollow them
if (!in_array($followedUser['id'], $followerIds)) {
$unfollowResponse = unfollowUser($baseUrl, $accessToken, $followedUser['id']);
// Output the account handle of the unfollowed user
echo "Unfollowing user: " . $followedUser['acct'] . "\n";
}
}
?>