Skip to content

Instantly share code, notes, and snippets.

@timdp
Last active August 12, 2024 22:49
Show Gist options
  • Save timdp/5956736 to your computer and use it in GitHub Desktop.
Save timdp/5956736 to your computer and use it in GitHub Desktop.

Revisions

  1. timdp revised this gist Jul 10, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion delete-old-tweets.php
    Original file line number Diff line number Diff line change
    @@ -79,7 +79,7 @@
    $retried = false;
    $code = $tmhOAuth->user_request($params);
    while ($code != 200 && $code != 404) {
    echo "HTTP $code :-(\nWaiting to retry ...";
    echo ($retried ? '' : ' '), "HTTP $code\nWaiting to retry ...";
    flush();
    zzz();
    $code = $tmhOAuth->user_request($params);
  2. timdp revised this gist Jul 9, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion delete-old-tweets.php
    Original file line number Diff line number Diff line change
    @@ -74,7 +74,7 @@
    $params = array(
    'method' => 'POST',
    'url' => $tmhOAuth->url("1.1/statuses/destroy/$id.json"),
    'params' => array('id' => $id)
    'params' => array('trim_user' => 1)
    );
    $retried = false;
    $code = $tmhOAuth->user_request($params);
  3. timdp created this gist Jul 9, 2013.
    105 changes: 105 additions & 0 deletions delete-old-tweets.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,105 @@
    <?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('id' => $id)
    );
    $retried = false;
    $code = $tmhOAuth->user_request($params);
    while ($code != 200 && $code != 404) {
    echo "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";
    }