Skip to content

Instantly share code, notes, and snippets.

@mamontov-cpp
Created May 16, 2014 09:39
Show Gist options
  • Save mamontov-cpp/a3c4e16c279345f4e2a8 to your computer and use it in GitHub Desktop.
Save mamontov-cpp/a3c4e16c279345f4e2a8 to your computer and use it in GitHub Desktop.
<?php
error_reporting(E_ALL);
require_once 'RestApi.php';
// Please replace this with own data, by creating application in dev.twitter.com
// Read-only permissions is enough for app.
$consumerKey = '';
$consumerSecret = '';
$accessToken = '';
$accessTokenSecret = '';
$username = 'XXXXXXXXXXXXXXXXXX'; //!< Twitter screen name for user, whose followers are compared
$email = 'joe@example.com'; //!< Email, where data should be sent
$twitter = new \TwitterPhp\RestApi($consumerKey,$consumerSecret,$accessToken,$accessTokenSecret);
$user = $twitter->connectAsUser();
$list = $user->get(
'followers/list',
array(
'screen_name' => $username,
'cursor' => -1,
'count' => 1000 /* If you have more followers than 1000, increase this number */
)
);
if ($list == null)
{
echo "Script execution failed. First check SSL certificate. Then check token and token secret data.";
}
$currentnames = array();
if ($list != null)
{
foreach($list['users'] as $u)
{
$currentnames[] = $u['screen_name'];
}
}
$file = dirname(__FILE__) . '/previous.php';
if (file_exists($file) == false)
{
file_put_contents($file, '$oldnames = ' .var_export($currentnames, true) . ';');
}
else
{
require_once($file);
$absentnames = array();
$newnames = array();
foreach($oldnames as $name)
{
if (!in_array($name, $currentnames))
{
$absentnames []= $name;
}
}
foreach($currentnames as $name)
{
if (!in_array($name, $oldnames))
{
$newnames []= $name;
}
}
file_put_contents($file, '<?php' . "\n" . '$oldnames = ' .var_export($currentnames, true) . ';');
if (count($absentnames) || count($newnames))
{
$message = "Dear $username,\n";
if (count($absentnames))
{
$message .= "You have lost followers: \n";
$message .= implode("\n", $absentnames) . "\n";
}
if (count($newnames))
{
$message .= "You have gained followers: \n";
$message .= implode("\n", $newnames) . "\n";
}
mail($email, 'Changes in followers list for ' . $username, $message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment