<?php
#####################################
#
# CoSo cleaner. Delete posts older than X days
#
# Find me at awk_it_up@counter.social
#
# usage php ./coso_cleaner.php [--dry-run]
#
# Steps
#
# 1. Get your CoSo ID.
# * Log into CoSo.
# * Click your @name to view your profile.
# * In the URL bar you will see the URL is now https://counter.social/web/accounts/XXXXXX. XXXXX is your CoSo ID.
# * Copy that XXXXX value into the $coso_id variable below.
# 2. Create a CoSo app.
# * Edit your profile in CoSo and click the "Developement" link.
# * Click the New Application button
# * Create name (doesn't matter what)
# * Select the Scopes: read & write and click the Submit button. This will bring you to the "Your Applications" page
# * Click the name of your new application.
# * Copy the "Your access token" value and paste it into the $coso_key variable below.
# 3. Decide how many days of toots you want to keep and enter that value into the $age variable below. Default is 7.
# 4. If you want to keep toots with specific tags regardless of age, enter the tags into the $ignore_tags variable below. Case insensitive, comma separated (no spaces), leave "" for none.
# 5. If you want to keep specific toots regardless of age, enter the toot ID(s) into the $ignore_toots variable below. Case insensitive, comma separated (no spaces), leave "" for none.
# 6. Run CoSo cleaner
# * This is a PHP script so you must run it on a system with PHP installed
# * The PHP curl module must also be installed
# * Run: 'php coso_cleaner.php' or 'php ./coso_cleaner.php' on Linux systems
# * Use the --dry-run switch to see what coso_cleaner WOULD delete, but not delete it
# 7. Automate it
# * On a system with a cron scheduler, use this entry to run it every day at 1AM
# * 0 1 * * * /path/to/coso_cleaner/coso_cleaner.php >> /path/to/coso_cleaner/coso_cleaner.log
# 8. Verify
# * Check the coso_cleaner.log each day to see what it has deleted
#
####################################
$coso_key="82567dce7a829c44008c9d41bf65425a378fca5fd5548d4bc59aba09edbd6478"; # Your CoSo Key
$coso_id="11036"; # Your CoSo ID
$age = 6; # In days
$ignore_tags = "cososec, pipepoem, cosopoetry, explicit"; # Comma separated, no spaces, leave "" for none.
$ignore_toots =""; # Comma separated, no spaces, leave "" for none.
## Should not have to change anything below this
$limit=40;
$since_id=0;
$url = "https://counter.social/api/v1/accounts/$coso_id/statuses?limit=$limit";
$statuses;
$num_statuses;
$max_status=0;
$min_status=0;
$status_array=[];
$range = 0;
$min_date = 0;
$dry_run = FALSE;
$date = time();
$delete = 0;
# Dry run?
if ( isset($argv[1]) && $argv[1] == "--dry-run" ) {
$dry_run = TRUE;
print "Doing dry run (will not actually delete anything.\n";
}
# Tags to ignore?
if ( strlen($ignore_tags) > 0 ) {
$ignore_tag_array = explode(",", $ignore_tags);
print "Ignoring tags: $ignore_tags\n";
}
# Toots to ignore?
if ( strlen($ignore_toots) > 0 ) {
$ignore_toot_array = explode(",", $ignore_toots);
print "Ignoring toots: $ignore_toots\n";
}
print "Run time: ".date("Y-M-d:H:i:s")."\n";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer $coso_key"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_USERAGENT, 'CoSo Cleaner: awk_it_up@counter.social');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 0);
$output = curl_exec($ch);
curl_close($ch);
$statuses = json_decode($output, TRUE);
$num_statuses = $statuses[0]['account']['statuses_count']."\n";
# Find status id range
$min_status = $statuses[0]['id'];
print "Statuses found: $num_statuses\n";
# Get the max and min status IDs
for ( $i = 0 ; $i < $num_statuses; $i += $limit ) {
foreach ($statuses as $status ) {
$delete = 1;
if ( $max_status < $status['id'] ) {
$max_status = $status['id'];
}
if ( $min_status > $status['id'] ) {
$min_status = $status['id'];
}
# Ignore toots with specified IDs set
if ( @count($ignore_toot_array) > 0) {
foreach ($ignore_toot_array as $ignore_toot ) {
if ( ($status['id'] == $ignore_toot) && $delete == 1 ) {
$delete = 0;
break;
}
else {
$delete = 1;
}
}
}
# Ignore toots with specified tag set
if ( count($status['tags']) > 0 && @count($ignore_tag_array) > 0 ) {
foreach ($ignore_tag_array as $ignore_tag) {
foreach ($status['tags'] as $status_tag ) {
if ( $ignore_tag == $status_tag['name'] && $delete == 1 ) {
$delete = 0;
break 2;
}
else {
$delete = 1;
}
}
}
}
if ( $delete == 1 ) {
$status_array[$status['id']]['id'] = $status['id'];
$status_array[$status['id']]['date'] = $status['created_at'];
$status_array[$status['id']]['content'] = $status['content'];
}
}
# Reset curl to get the next batch o' statuses
$url = "https://counter.social/api/v1/accounts/$coso_id/statuses?limit=$limit&max_id=$min_status";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer $coso_key"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_USERAGENT, 'CoSo Cleaner: awk_it_up@counter.social');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 0);
$output = curl_exec($ch);
curl_close($ch);
$statuses = json_decode($output, TRUE);
}
# Set up the date parameters
$range =$age*86400;
$today = time();
$min_date = $today - $range;
# Set up the generic curl params again
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer $coso_key"));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_USERAGENT, 'CoSo Cleaner: awk_it_up@counter.social');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 0);
# Delete 'em!
$delete = 0;
foreach ($status_array as $status ) {
if ( strtotime($status['date']) < $min_date ) {
$delete++;
if ( $dry_run ) {
print "Dry run: Status ".$status['id']." on ".$status['date'].": \"".$status['content']."\" is in scope, but not actually being deleted due to dry run.\n\n\n";
}
else {
print "Deleting ".$status['id']." on ".$status['date'].": \"".$status['content']."\"\n\n\n";
curl_setopt($ch, CURLOPT_URL, "https://counter.social/api/v1/statuses/".$status['id']);
curl_exec($ch);
}
}
}
print "Total affected toots: $delete.\n";
# Cleanup, bye bye.
curl_close($ch);