Skip to content

Instantly share code, notes, and snippets.

@ozh
Created December 6, 2015 13:23
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ozh/f5deafcffd8d271bfa92 to your computer and use it in GitHub Desktop.
Tweets json importer for Ozh' Tweet Archive WordPress plugin
Script made by @danielfaulknor, copied in case it goes missing
Original: https://github.com/danielfaulknor/ozh-tweet-archiver/blob/cli-importer-json/import-json-cli.php
<?php
/**
* Import a JSON tweet list
*
* Import tweets from an archive as generated by Twitter
*
* Usage :
* - Import your tweets with the plugin. The plugin will import only the 3200 most recent tweets.
* - Download your archive from Twitter, open the tweets.csv file (delete first 3200 most recent tweets to speed up things)
* - put tweets.csv into the plugin directory
* - run this file with PHP on the command line
*/
if( php_sapi_name() !== 'cli' ) {
die("Meant to be run from command line");
}
function find_wordpress_base_path() {
$dir = dirname(__FILE__);
do {
//it is possible to check for other files here
if( file_exists($dir."/wp-config.php") ) {
return $dir;
}
} while( $dir = realpath("$dir/..") );
return null;
}
define( 'BASE_PATH', find_wordpress_base_path()."/" );
define('WP_USE_THEMES', false);
global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header;
require(BASE_PATH . 'wp-load.php');
require(BASE_PATH . "wp-admin" . '/includes/image.php');
require(BASE_PATH . "wp-admin" . '/includes/file.php');
require(BASE_PATH . "wp-admin" . '/includes/media.php');
ozh_ta_require( 'import.php' );
ozh_ta_require( 'option-page.php' );
$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 from " . $file . "\n";
$contents = file_get_contents($dir_path . $file);
$contents = substr($contents, strpos($contents, "["));
$contents = "{ \"json\" : $contents }";
$json = json_decode($contents);
$count = 0;
foreach($json->json as $tweet){
if ( ozh_ta_insert_tweets(array($tweet)) ) { echo "."; }
sleep(0.1);
$count++;
}
echo "\nImported: " . $count . " \n";
}
}
closedir($dir_handler);
}
}
global $ozh_ta;
// Update real last_tweet_id_inserted, stats, & reset API paging
$ozh_ta['twitter_stats']['link_count'] = $wpdb->get_var( "SELECT COUNT(ID) FROM `$wpdb->posts` WHERE `post_type` = 'post' AND `post_status` = 'publish' AND `post_content` LIKE '%class=\"link%'" );
$ozh_ta['twitter_stats']['replies'] = $wpdb->get_row( "SELECT COUNT( DISTINCT `meta_value`) as unique_names, COUNT( `meta_value`) as total FROM `$wpdb->postmeta` WHERE `meta_key` = 'ozh_ta_reply_to_name'", ARRAY_A );
$ozh_ta['twitter_stats']['total_archived'] = $wpdb->get_var( "SELECT COUNT(`meta_key`) FROM `$wpdb->postmeta` WHERE `meta_key` = 'ozh_ta_id'" );
update_option( 'ozh_ta', $ozh_ta );
echo 'Finished importing tweets!.';
@danielmcclure
Copy link

Any idea why this would be failing? I get:

Fatal error: Class 'Requests_Hooks' not found in /home/user/public_html/status/wp-includes/class-wp-http-requests-hooks.php on line 29

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment