<?php

//
// delete-old-tweets.php - https://github.com/timdp
//
// -- Instructions --
// 1. Save this script somewhere as delete-old-tweets.php
// 2. Get your Twitter archive and extract it to the same folder
// 3. Clone https://github.com/themattharris/tmhOAuth to the same folder
// 4. Register an app at dev.twitter.com and enter its credentials below
// 5. Run this script
//

require_once dirname(__FILE__) . '/tmhOAuth/tmhOAuth.php';

//
// Configuration
//

$max_month = '2012_12';

$tmhOAuth = new tmhOAuth(array(
        'consumer_key'    => '...',
        'consumer_secret' => '...',
        'token'           => '...',
        'secret'          => '...'
    ));

$interval = 5;

//
// Script
//

$state_file = preg_replace('/\.php$/', '.json', __FILE__);
if (file_exists($state_file)) {
    $state = json_decode(file_get_contents($state_file));
} else {
    $state = new stdClass();
}

$prev_last = ($state->last_deleted)
    ? $state->last_deleted
    : array('0', '0');

$path = 'data/js/tweets';
$files = scandir($path);
sort($files);
foreach ($files as $file) {
    if ($file == '.' || $file == '..') continue;
    $fpath = "$path/$file";
    $month = preg_replace('/\.js$/', '', $file);
    if (strcmp($month, $max_month) >= 0) {
        echo "Reached month $max_month, exiting\n";
        break;
    }
    $month_cmp = strcmp($month, $prev_last[0]);
    if ($month_cmp < 0) {
        echo "Skipping previously deleted month $month\n";
        continue;
    }
    echo "Entering month $month\n";
    $contents = file_get_contents($fpath);
    $json = preg_replace('/^.*?=\s*/s', '', $contents);
    $data = json_decode($json);
    $data = array_reverse($data);
    foreach ($data as &$tweet) {
        $id = $tweet->id_str;
        if ($month_cmp == 0 && strcmp($id, $prev_last[1]) <= 0) {
            echo "Skipping previously deleted tweet $month/$id\n";
        } else {
            echo "Deleting tweet $month/$id ...";
            flush();
            $params = array(
                    'method' => 'POST',
                    'url'    => $tmhOAuth->url("1.1/statuses/destroy/$id.json"),
                    'params' => array('trim_user' => 1)
                );
            $retried = false;
            $code = $tmhOAuth->user_request($params);
            while ($code != 200 && $code != 404) {
                echo ($retried ? '' : ' '), "HTTP $code\nWaiting to retry ...";
                flush();
                zzz();
                $code = $tmhOAuth->user_request($params);
                $retried = true;
            }
            echo ($retried ? '' : ' '), 'Sleeping ...';
            flush();
            zzz();
            $state->last_deleted = array($month, $id);
            file_put_contents($state_file, json_encode($state));
        }
    }
}

function zzz() {
    global $interval;
    for ($i = $interval; $i > 0; $i--) {
        echo " $i";
        flush();
        sleep(1);
    }
    echo "\n";
}