Exemple #1
0
function aktt_update_tweets()
{
    global $aktt;
    // let the last update run for 10 minutes
    if (time() - intval(get_option('aktt_doing_tweet_download')) < $aktt->tweet_download_interval()) {
        return;
    }
    // wait 10 min between downloads
    if (time() - intval(get_option('aktt_last_tweet_download')) < $aktt->tweet_download_interval()) {
        return;
    }
    update_option('aktt_doing_tweet_download', time());
    global $wpdb, $aktt;
    if (empty($aktt->twitter_username)) {
        update_option('aktt_doing_tweet_download', '0');
        return;
    }
    if (aktt_oauth_test() && ($connection = aktt_oauth_connection())) {
        $data = $connection->get(AKTT_API_USER_TIMELINE);
        if ($connection->http_code != '200') {
            update_option('aktt_doing_tweet_download', '0');
            return;
        }
    } else {
        return;
    }
    // hash results to see if they're any different than the last update, if so, return
    $hash = md5($data);
    if ($hash == get_option('aktt_update_hash')) {
        update_option('aktt_last_tweet_download', time());
        update_option('aktt_doing_tweet_download', '0');
        do_action('aktt_update_tweets');
        return;
    }
    $data = preg_replace('/"id":(\\d+)/', '"id":"$1"', $data);
    // hack for json_decode on 32-bit PHP
    $tweets = json_decode($data);
    if (is_array($tweets) && count($tweets)) {
        $tweet_ids = array();
        foreach ($tweets as $tweet) {
            $tweet_ids[] = $wpdb->escape($tweet->id);
        }
        $existing_ids = $wpdb->get_col("\n\t\t\tSELECT tw_id\n\t\t\tFROM {$wpdb->aktt}\n\t\t\tWHERE tw_id\n\t\t\tIN ('" . implode("', '", $tweet_ids) . "')\n\t\t");
        foreach ($tweets as $tw_data) {
            if (!$existing_ids || !in_array($tw_data->id, $existing_ids)) {
                $tweet = new aktt_tweet($tw_data->id, $tw_data->text);
                $tweet->tw_created_at = $tweet->twdate_to_time($tw_data->created_at);
                if (!empty($tw_data->in_reply_to_status_id)) {
                    $tweet->tw_reply_tweet = $tw_data->in_reply_to_status_id;
                    $url = aktt_api_status_show_url($tw_data->in_reply_to_status_id);
                    $data = $connection->get($url);
                    if (strcmp($connection->http_code, '200') == 0) {
                        $status = json_decode($data);
                        $tweet->tw_reply_username = $status->user->screen_name;
                    }
                }
                // make sure we haven't downloaded someone else's tweets - happens sometimes due to Twitter hiccups
                if (strtolower($tw_data->user->screen_name) == strtolower($aktt->twitter_username)) {
                    $tweet->add();
                }
            }
        }
    }
    aktt_reset_tweet_checking($hash, time());
    do_action('aktt_update_tweets');
}
Exemple #2
0
function update_my_tweets()
{
    $profile_id = get_profile_id();
    if (!$profile_id) {
        return;
    }
    // activate Twitter Tools
    $_GET['activate'] = true;
    // trip the init() function
    aktt_init();
    // get the Twitter Tools object
    global $wpdb, $aktt, $db;
    if (empty($aktt->twitter_username) || empty($aktt->twitter_password)) {
        return;
    }
    // make a new tweet object
    $tweet = new aktt_tweet();
    // let the last update run for 5 minutes
    if (time() - intval(get_option('aktt_doing_tweet_download')) < 300) {
        return;
    }
    update_option('aktt_doing_tweet_download', time());
    if (empty($aktt->twitter_username) || empty($aktt->twitter_password)) {
        update_option('aktt_doing_tweet_download', '0');
        die;
    }
    require_once ABSPATH . WPINC . '/class-snoopy.php';
    $snoop = new Snoopy();
    $snoop->agent = 'Twitter Tools http://alexking.org/projects/wordpress';
    $snoop->user = $aktt->twitter_username;
    $snoop->pass = $aktt->twitter_password;
    $snoop->fetch('http://tweetpass.com/statuses/friends_timeline.json');
    if (!strpos($snoop->response_code, '200')) {
        update_option('aktt_doing_tweet_download', '0');
        return;
    }
    $data = $snoop->results;
    $hash = md5($data);
    if ($hash == get_option('aktt_update_hash')) {
        update_option('aktt_doing_tweet_download', '0');
        return;
    }
    $json = new Services_JSON();
    $tweets = $json->decode($data);
    if (is_array($tweets) && count($tweets) > 0) {
        $tweet_ids = array();
        foreach ($tweets as $tweet) {
            $tweet_ids[] = $wpdb->escape($tweet->id);
        }
        $existing_ids = $wpdb->get_col("\n      SELECT tw_id\n      FROM {$wpdb->aktt}\n      WHERE tw_id\n      IN ('" . implode("', '", $tweet_ids) . "')\n    ");
        $new_tweets = array();
        foreach ($tweets as $tw_data) {
            if (!$existing_ids || !in_array($tw_data->id, $existing_ids)) {
                $tweet = new aktt_tweet($tw_data->id, $tw_data->text);
                $tweet->tw_created_at = $tweet->twdate_to_time($tw_data->created_at);
                $new_tweets[] = $tweet;
            }
        }
        foreach ($new_tweets as $tweet) {
            $AkTwitter =& $db->get_table('ak_twitter');
            $Entry =& $db->get_table('entries');
            $t = $AkTwitter->find_by('tw_id', $tweet->tw_id);
            if (!$t) {
                $tweet->add();
                $created = date("Y-m-d H:i:s", $tweet->tw_created_at - 8 * 3600);
                $t = $AkTwitter->find($db->last_insert_id($AkTwitter));
                if ($t) {
                    $t->set_etag();
                }
                $atomentry = $Entry->find_by(array('resource' => 'ak_twitter', 'record_id' => $t->id), $t->id);
                if ($atomentry) {
                    $result = $db->get_result("UPDATE entries SET last_modified = '{$created}' WHERE id = " . $atomentry->id);
                }
                $user = new Snoopy();
                $user->agent = 'Twitter Tools http://alexking.org/projects/wordpress';
                $user->user = $aktt->twitter_username;
                $user->pass = $aktt->twitter_password;
                $user->fetch('http://tweetpass.com/statuses/show/' . $tweet->tw_id . '.json');
                $data = $user->results;
                $json = new Services_JSON();
                $notice = $json->decode($data);
                $uarr = $notice->user;
                $TwitterUser =& $db->model('TwitterUser');
                $twuser = $TwitterUser->find_by('twitter_id', $uarr->id);
                if (!$twuser) {
                    $twuser = $TwitterUser->base();
                    $twuser->set_value('description', $uarr->description);
                    $twuser->set_value('screen_name', $uarr->screen_name);
                    $twuser->set_value('url', $uarr->url);
                    $twuser->set_value('name', $uarr->name);
                    $twuser->set_value('protected', $uarr->protected);
                    $twuser->set_value('followers_count', $uarr->followers_count);
                    $twuser->set_value('profile_image_url', $uarr->profile_image_url);
                    $twuser->set_value('location', $uarr->location);
                    $twuser->set_value('twitter_id', $uarr->id);
                    $twuser->save_changes();
                }
                $t->set_value('profile_id', $twuser->twitter_id);
                $t->save_changes();
            }
            $AkTwitter->has_and_belongs_to_many('identities');
            $join =& $db->get_table($Entry->join_table_for('ak_twitter', 'identities'));
            $j = $join->base();
            $j->set_value('aktwitter_id', $t->id);
            $j->set_value('identity_id', get_profile_id());
            $j->save_changes();
        }
    }
    update_option('aktt_update_hash', $hash);
    update_option('aktt_last_tweet_download', time());
    update_option('aktt_doing_tweet_download', '0');
}