How To: Twitter Spam

I feel so dirty for writing this, but I was inspired by this twitterer.

After reading how to create a twitter bot in 5 minutes, I realized that I could combine this with simplepie and create a twitter spammer in less than ten minutes. Well, ok it took me fifteen.

So the steps were:
Get something to say,
Get someone to say it to,
Say it.

First, I needed an account with something to say. I used my fsbo homes already-spamming twitter feed. ( . http://twitter.com/fsbohomes ). It just takes the new stuff in the fsbo auction site’s rss feed and twitters it via twitterfeed.

As it turns out, the public feed probably isn’t the best tool for this; a better tool would be twitter’s search at http://search.twitter.com/ . Note the rss feed icon on the results page.

Here’s what I came up with:

// simplepie makes the rss parsing very easy.
include_once( "libs/simplepie.inc" );
$keywords=
    array(
                    ‘fsbo’=>"Hey come check out great prices on real estate auctions: http://www.fsboauction.info"
);

$username = ‘fsbohomes’;
$password = ’somepassword’;

// read the public timeline. Note that this is cached for 60 seconds, so asking for it more often
// than that is silly. Also notice that it won’t catch _everything_ but you should get at least one a day.

$read=‘http://twitter.com/statuses/public_timeline.rss’;
$feed=new SimplePie( );
$feed->set_feed_url( $read );
$feed->set_cache_duration( 60 );
$feed->init();
$feed->handle_content_type();

$count=0;
foreach( $feed->get_items() as $item ){
// read the tweet.
                $tweet=$item->get_title();
// split it into username and the contents.
// we’ll be comparing our keywords against the contents and
// sending the username an @ message.
                list( $a, $t)=split(‘:’, $tweet, 2);
// I had a lot of multibyte-encoded lines but didn’t feel like parsing them.
// since I don’t speak the languages anyway.
                $e= mb_detect_encoding( $t );
                if ( $e == ‘ASCII’ ) {
// check each keyword
                    foreach( $keywords as $seek=>$message ){
                        $count++;
                        if ( strpos( $t, $seek ) !== false ) {
// if the keyword is found, send a message.
                                    $msg = ‘@’.$a.‘ ‘.$message;
                                    $msg = substr( $msg, 0, 140 );
                                    sendTweet( $msg );
                        }
                    }
                }
}
// http://twitter.com/statuses/public_timeline.format
// formats: xml, json, rss, atom

// this is from the twitter bot post referenced above
// it’s just a function for posting a twitter.
function sendTweet($msg){
                global $username;
                global $password;

    $url = ‘http://twitter.com/statuses/update.xml’;

    $curl_handle = curl_init();

    curl_setopt($curl_handle, CURLOPT_URL, $url);
    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl_handle, CURLOPT_POST, 1);
    curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$msg");
    curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
    $buffer = curl_exec($curl_handle);
    curl_close($curl_handle);
    if (empty($buffer)) {
        echo "failed\n";
    }
    else {
        echo "succeeded\n";
    }
}
 

Ta-daa. I set it to run every five minutes and it caught about one twitter every few hours.

Written by russ on November 16th, 2008 with no comments.
Read more articles on Current Methods and code and tips.

Related articles

No comments

There are still no comments on this article.

Leave your comment...

If you want to leave your comment on this article, simply fill out the next form:




You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .