Support » Plugin: Twitter Tools » Importing Twitter's JSON archive of my tweets

  • adamwulf

    (@adamwulf)


    Thanks Alex for your plugin, it’s great!

    I don’t seem to be able to import very old tweets when I press the “Download Tweets Now” button. I also don’t have luck with old tweets using the backfill_tweets.php form https://gist.github.com/alexkingorg/3470627.

    I have manually downloaded an archive of all of my tweets from twiiter, I’m hoping there is a straightforward way to import these tweets into twitter tools? The format provided in the archive is both csv or json format.

    If you can point me in the right direction, I may be able to hack some php together to make it happen. I’m hoping I can parse the json from the twitter archive, and then import it with:

    [code]
    $t = new AKTT_Tweet($tweet);
    if (!$t->exists_by_guid()) {
    $t->add();
    }
    [/code]

    does that sound right?

    thanks!

    http://wordpress.org/extend/plugins/twitter-tools/

Viewing 4 replies - 1 through 4 (of 4 total)
  • adamwulf

    (@adamwulf)

    and to answer my own question: yes! it does seem to work 🙂

    script I used:

    if(isset($_REQUEST["tweet_load"])){
    
    	$dir_path = "/path/to/tweet/js/file/data/";
    
    	$haveCount = 0;
    	$addCount = 0;
    
    	if (is_dir($dir_path)) {
    	    if ($dir_handler = opendir($dir_path)) {
    	        while (($file = readdir($dir_handler)) !== false) {
    	        	if(strpos($file, ".js") > 0){
    
    	        		echo "loading " . $dir_path . $file . "<br>";
    
    					$contents = file_get_contents($dir_path . $file);
    					$contents = substr($contents, strpos($contents, "["));
    					$contents = "{ \"json\" : $contents }";
    					$json = json_decode($contents);
    
    					foreach($json->json as $twt){
    						$t = new AKTT_Tweet($twt);
    						if (!$t->exists_by_guid()) {
    							$t->add();
    							echo "adding " . $t->id() . "<br>";
    							$addCount++;
    						}else{
    							echo "passing " . $t->id() . "<br>";
    							$haveCount ++;
    						}
    					}
    	        	}
    	        }
    	        closedir($dir_handler);
    	    }
    	}
    
    	echo "adding: $addCount<br>";
    	echo "have: $haveCount<br>";
    
    exit;

    Hi, Adam,

    In wich file have you put this script? How do I work with it?

    Marlos

    Apyus, if you’re still looking for a way to make that work, here’s what I did.

    The code needs Twitter Tools to be loaded, so I created a page template, then created a page in WordPress using that template, then previewed the new page, triggering the code.

    I had uploaded all of my .js files into a Tweets directory in my website’s home directory.

    This is the full template that I used:

    <?php
    /*
    Template Name: Backfill Tweets
    */
    
    header('Content-Type: text/html charset='.get_option('blog_charset'), true);
    echo "<html>";
    echo "\n";
    echo "<body>";
    
    	$dir_path = "tweets/";
    
    	$haveCount = 0;
    	$addCount = 0;
    
    	if (is_dir($dir_path)) {
    	    if ($dir_handler = opendir($dir_path)) {
    	        while (($file = readdir($dir_handler)) !== false) {
    	        	if(strpos($file, ".js") > 0){
    
    	        		echo "loading " . $dir_path . $file . "<br />";
    
    					$contents = file_get_contents($dir_path . $file);
    					$contents = substr($contents, strpos($contents, "["));
    					$contents = "{ \"json\" : $contents }";
    					$json = json_decode($contents);
    
    					foreach($json->json as $twt){
    						$t = new AKTT_Tweet($twt);
    						if (!$t->exists_by_guid()) {
    							$t->add();
    							echo "adding " . $t->id() . "<br>";
    							$addCount++;
    						}else{
    							echo "passing " . $t->id() . "<br>";
    							$haveCount ++;
    						}
    					}
    	        	}
    	        }
    	        closedir($dir_handler);
    	    }
    	}
    
    	echo "adding: $addCount<br>";
    	echo "have: $haveCount<br>";
    
    echo "</body>";
    echo "\n";
    echo "</html>";
    
    ?>

    It took, all told, about two hours to load the 17,000 tweets I’ve made in the last four years.

    Hope that helps. 🙂

    The next thing, for me, is to create daily Twitter digest now that I have all of this data. I’ve worked out a way of making it happen. This weekend I’ll code it, and it will be grand.

    Hi, allyngibson,

    IT’S WORKING!!!

    Thank you very much.

    But, just one thing to the others users…

    Please, remember to adjust this line:
    “$dir_path = “tweets/”;”

    At my server, it worked when I changed for something like this:

    $dir_path = “/home/USERNAME/WEBSITE_FOLDER/tweets/data/js/tweets/”;

    But I uploaded in a “tweets” folder all the files that Twitter Archive gives.

    (Ps.: Sorry if my english was not so good.)

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Importing Twitter's JSON archive of my tweets’ is closed to new replies.