From 2fa7588ca7bfb76ef1af132d0a9f30df23ab574a Mon Sep 17 00:00:00 2001 From: Paul Weinstein Date: Tue, 22 Oct 2019 11:17:31 -0700 Subject: [PATCH] Added including retweet, grouping of tweets of the same day --- bin/twitter_2_wp.php | 68 +++++++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 19 deletions(-) diff --git a/bin/twitter_2_wp.php b/bin/twitter_2_wp.php index 6c20f7e..4593f4e 100644 --- a/bin/twitter_2_wp.php +++ b/bin/twitter_2_wp.php @@ -48,26 +48,30 @@ for( $counter = 0; $counter < 100; $counter++ ){ $totalCount = ( $counter + 1 ) + ( $masterCount * 100 ); - +/* if (( $masterCount > 0 ) && $counter == 0 ) { // Skip repeat of last/first $counter++; } - - // TO DO: Update to refect same process as with cache! +*/ + // Same process as with cache! processTweet( $tweets[$counter] ); + array_push( $posts, $post ); if( $counter <= 99 ) { $endWith = $tweets[$counter]->id; } + } } + validate( $posts ); + } else { $fh = fopen( $file, 'r' ); @@ -93,14 +97,27 @@ function validate( $posts ) { // TO DO: Review and commit to creating WP post or skip - // Post title: mon dd yy - like old posts - // TO DO: Compare current & previous ids, is this a duplicate? - for( $counter = 0; $counter <= sizeof( $posts ); $counter ++ ) { - $post = $posts[$counter]; - $key = key( $post ); - print( $post[$key] ); + // If multiple tweets on same day, include in one WP post + // with multiple links to orginal tweet on Twitter + while( date( "Y-m-d", strtotime( $posts[$counter]['date'] )) == date( "Y-m-d", strtotime( $posts[$counter+1]['date'] ))) { + + print( $posts[$counter]['post'] ); + print( $posts[$counter+1]['post'] ); + $counter = $counter + 1; + $multi = true; + + } + + if( !$multi ) { + + print( $posts[$counter]['post'] ); + + } + + $multi = false; + print( 'Type "y" to continue: ' ); $handle = fopen ( 'php://stdin', 'r' ); $line = fgets( $handle ); @@ -112,9 +129,9 @@ function validate( $posts ) { fclose( $handle ); - if( !empty($posts[$counter + 1])) { + if( !empty($posts[$counter+1])) { - if ( !array_diff_key( $posts[$counter], $posts[$counter+1] )) { + if ( $post['id'] == $posts[$counter+1]['id'] ) { # Duplicates, skip next tweet $counter++; @@ -129,17 +146,30 @@ function validate( $posts ) { function processTweet( $tweetObj ) { - // If multiple tweets on same day, include in one WP post? - // With multiple links to orginal tweet on Twitter? - // Include other entities, such as link headline/previews? - // If retweet, include orginal message in full - // If reply/conversation? - // TO DO: Print Geo info, if tagged + // Include other entities, such as link title/desc from URL Object? + // If reply/conversation? in_reply_to_status + // TO DO: Print Geo info, coordinates +// var_dump( $tweetObj ); + // Better format for date than Twitters + // Post title: mon dd yy - like old posts + $tDate = $tweetObj->created_at; + $localTimeStamp = date( "Y-m-d h:i:s", strtotime( $tDate )); + $wpPostDate = date( "M d y", strtotime( $tDate )); + $humanDate = date( "d F Y", strtotime( $tDate )); + $tweet = preg_replace( '/((http|https):\/\/[^\s]+)/', '$0', $tweetObj->full_text ); - $post = "

First published: " .$tweetObj->created_at. " on Twitter

"; + $post = "

First published: " .$humanDate. " on Twitter

"; $post .= "

" .preg_replace( '/@([^\s]+)/', '$0', $tweet ). "

\n"; + // If retweet, include orginal message in full + if( !empty( $tweetObj->retweeted_status )) { + + $retweet = preg_replace( '/((http|https):\/\/[^\s]+)/', '$0', $tweetObj->retweeted_status->full_text ); + $post .= "

Original Tweet: " .$retweet. "

\n"; + + } + if( !empty( $tweetObj->entities->media )) { // Can have more than one media element! @@ -155,7 +185,7 @@ function processTweet( $tweetObj ) { } - $posts[$tweetObj->id] = $post; + $posts = array( date => $localTimeStamp, id => $tweetObj->id, post => $post ); return $posts; }