Skip to content

Instantly share code, notes, and snippets.

@jackreichert
Last active November 8, 2015 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackreichert/9835284074437266a76c to your computer and use it in GitHub Desktop.
Save jackreichert/9835284074437266a76c to your computer and use it in GitHub Desktop.
Gists for Augmenting Ozh's Twitter Archiver
check = function () {
if (twitter_ids.length >= 1) {
// was timing out at the 100 max you can play with this, will differ per server
var batch = twitter_ids.splice(0, 15);
wp_jsonp("import_tweets", "import_tweets", {variable: batch}, import_tweets);
// 150 calls per 15 minues. 900sec/150 + a bit for safety.
setTimeout(check, timeout);
} else {
if (error_ids.length > 0) {
$('#csv_file_upload').prepend("<p>" + error_ids.length + " tweets not imported. You may have hit a limit, you can try again in 15 minutes to get the rest.</p>");
}
$('#csv_file_upload').prepend("<p>Done.</p>");
}
}
static function get_single_tweet( $id ) {
global $ozh_ta;
if ( ! ozh_ta_is_configured() ) {
ozh_ta_debug( 'Config incomplete, cannot import tweets' );
return false;
}
$api = 'https://api.twitter.com/1.1/statuses/lookup.json?id=' . $id;
$headers = array(
'Authorization' => 'Bearer ' . $ozh_ta['access_token'],
);
ozh_ta_debug( "Polling $api" );
$response = wp_remote_get( $api, array(
'headers' => $headers,
'timeout' => 10
) );
$tweet = json_decode( wp_remote_retrieve_body( $response ) );
if ( isset( $tweet->errors ) ) {
ozh_ta_debug( "Error with tweet #$id : " . $tweet->errors[0]->message );
return false;
}
return $tweet;
}
function import_as_tweet( $post ) {
$post['post_type'] = 'tweet';
return $post;
}
// Plugins: hack here
$post = apply_filters( 'ozh_ta_insert_tweets_post', $post );
$post_id = wp_insert_post( $post );
function post_insert_term( $object_id, $terms, $tt_ids, $taxonomy ) {
global $ozh_ta;
if ( 'tweet' == get_post_type( $object_id ) && 'post_tag' == $taxonomy && 'yes' == $ozh_ta['add_hash_as_tags'] ) {
$in = '(';
foreach ( $tt_ids as $tid ) {
$in .= $tid . ',';
}
$in = rtrim( $in, ',' ) . ')';
global $wpdb;
$sql = "UPDATE `$wpdb->term_taxonomy` SET taxonomy='hashtag' WHERE term_id IN $in";
$wpdb->query( $sql );
}
}
function post_insert_tweet( $post_id ) {
global $ozh_ta;
if ( $ozh_ta['link_usernames'] == 'yes' ) {
$tweet = get_post( $post_id );
$dom = new DOMDocument;
$dom->encoding = 'utf-8';
if ( '' != $tweet->post_content ) {
$dom->loadHTML( utf8_decode( $tweet->post_content ) );
$mentions = array();
foreach ( $dom->getElementsByTagName( 'span' ) as $node ) {
if ( false !== strpos( $node->getAttribute( "class" ), 'username' ) && 0 === strpos( $node->nodeValue, '@' ) ) {
$mentions[] = substr( $node->nodeValue, 1 );
}
}
wp_set_post_terms( $post_id, $mentions, 'mention', true );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment