<?php | |
/** | |
* Import a CSV 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 this script and tweets.csv in WordPress' root directory (where wp-load.php is) | |
* - run this file in your browser (http://yoursite/blog/import.php) | |
* - when it's finished, delete this file | |
*/ | |
require( './wp-load.php' ); | |
ozh_ta_require( 'import.php' ); | |
ozh_ta_require( 'option-page.php' ); | |
wp_enqueue_script( 'jquery' ); | |
print_head_scripts(); | |
$start = isset( $_GET['start'] ) ? $_GET['start'] : 0; | |
// Import | |
ozh_ta_schedule_next( 0 ); | |
$import = oti_process_file( oti_get_file(), $start ); | |
$start += $import['count']; | |
$delay = ( $import['ok'] ? OZH_TA_NEXT_SUCCESS : OZH_TA_NEXT_FAIL ); | |
// Continue or mark import as finished | |
if( $import['finished'] ) { | |
global $ozh_ta; | |
// Schedule next operation | |
ozh_ta_schedule_next( $ozh_ta['refresh_interval'] ); | |
// 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 '<p>Finished importing tweets!.</p>'; | |
} else { | |
echo '<p>Imported ' . $import['count'] . ' tweets</p>'; | |
$reload = add_query_arg( array( | |
'start' => $start, | |
), get_bloginfo('url') . '/' . basename( __FILE__ ) ); | |
ozh_ta_reload( $reload, $delay ); | |
} | |
// done. | |
/*****************************************************************************/ | |
/** | |
* Get CSV file name, die if not found | |
* | |
* @param string $file CSV file name (defaults to 'tweets.csv') | |
* @return string Full path to file (or die if not found) | |
*/ | |
function oti_get_file( $file = 'tweets.csv' ) { | |
$file = str_replace( '\\', '/', dirname( __FILE__ ) . '/tweets.csv' ); | |
if( !file_exists( $file ) ) { | |
wp_die( 'Tweet file not found. Put it in the same directory as wp-load.php' ); | |
} | |
return $file; | |
} | |
/** | |
* Read a CSV file, process 1st field of each row by batch of defined size | |
* | |
* @param string $file path to file | |
* @param int $start first line to process, ignoring all those before | |
* @return unknown | |
*/ | |
function oti_process_file( $file, $start ) { | |
$handle = fopen( $file, "r" ); | |
// Always ignore header | |
$ignore = fgetcsv( $handle, 30, "," ); | |
$ignore = $count = 0; | |
$finished = false; | |
$ok = true; | |
while ( $count < OZH_TA_BATCH && $data = fgetcsv( $handle, 30, "," ) ) { | |
// Make sure we're reading an actual comma separated line starting with a tweet ID, and not the sequel of a multiline cell | |
$id = $data[0]; | |
if( preg_match( '/^\d+$/', $id ) ) { | |
// Resume to whished position, ignore lines till line number $start | |
if( $ignore++ < ( $start - 1 ) ) { | |
continue; | |
} | |
// Now we can start processing each line during next batch | |
// Here: not much error checking. Assuming false will be either rate limit exceeded, or Twitter down | |
if( ozh_ta_import_single_tweet( $id ) === false ) { | |
$ok = false; | |
} | |
$count++; | |
} | |
} | |
if( $count < OZH_TA_BATCH && $ok ) { | |
$finished = true; | |
} | |
return array( 'ok' => $ok, 'count' => $count, 'finished' => $finished ); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment