Comments Off on How (K)Not to Deliver a Newspaper on a Rainy Day

How (K)Not to Deliver a Newspaper on a Rainy Day

Photo

Note to my nespaper delivery guy. DO NOT use the ‘single bag knot’ approach on a rainy day. If the bag is so thin you can read the front page through it this will ALWAYS result in a wet paper. Use the ‘double reverse bag tuck’ method instead. (Those who get dry newspapers on a rainy day will understand what that means.) This is a big reason why you don’t get tipped.



Comments to this post

Tags:
Comments Off on Dinner

Dinner

Img_0006

Some would call this desert. I call it dinner.



Comments to this post

Tags:
Comments Off on Two Types of People In This World

Two Types of People In This World

Img_0005

There are two types of people in this world. This picture defines them.



Comments to this post

Tags:
Comments Off on Quick And Easy RSS Parsing With PHP

Quick And Easy RSS Parsing With PHP

When I wrote about creating a Twitter bot with PHP, etc. last week I had in mind to continue developing the idea. To this end I’ve created a bot that replaces the formulaic type of URL for an ‘of the day’ type page (for example http://www.dilbert.com/strips/”.date(“Y”).”-“.date(“m”).”-“.date(“d”).”/”) with an RSS parser so the bot will retweet the links of a given RSS feed.

It turned out to be much easier than I thought. The main tool here is SimpleXML. Here’s how to do it:

  1. Set a variable to represent the RSS feed you want to use. For example:
    $feedUrl = 'http://feeds.nytimes.com/nyt/rss/Baseball';
  2. Retrive the current version of the feed as follows:
    $rawFeed = file_get_contents($feedUrl);
  3. Create a SimpleXML object containing the contents of the RSS file. This will make reading the file simple (no pun intended).
    $xml = new SimpleXmlElement($rawFeed);
  4. The next step is to read the RSS file and pull out the data you want. To do this set up a ‘for’ loop to process each item in the RSS until we’ve reached the end. In my case I want to read the XML in reverse order so if there are multiple articles I want to tweet I can send them in chronological order. (If I read the RSS forward they’d be I’d be reading in reverse chronological order.) Here’s the ‘for’ statement I used:
    for ($counter=count($xml->channel->item); $counter > -1; $counter--) {
    code to process the current item
    }

    The ‘code to process the current item’ is described below. Everything you want to do with this data (assign to variables, send to Twitter, whatever) should be included here because once you move on to the next item the data for the current item will no longer be accessible.
  5. Each trip through the loop will allow us to process another item in the RSS until we get to the end. All that’s needed to grab our data is an array followed by a couple of lines to assign data from the RSS item the loop is currently pointing to to a space in the array. For example, the following lines create an array and stores the link and title of the current item to it:
    $article = array();
    $article['link'] = $xml->channel->item[$counter]->link;
    $article['title'] = $xml->channel->item[$counter]->title;

At this point you’d have the data you want and can do whatever you want with it.



Comments to this post

Tags: , ,
Comments Off on Create a Twitter Bot With PHP, YOURLs and Cron

Create a Twitter Bot With PHP, YOURLs and Cron

Ever since I’ve been on Twitter I’ve been fascinated by Twitter bots. You know, automated Twitter posting robots. There are all sorts of Twitter bots. Most of them are Twitter accounts with a single purpose that are run by some sort of programming. Most often they monitor Twitter’s RSS feed for specific keywords and either reply to or retweet them.

A lesser used (I think) kind of Twitter bot is the kind that will post specific links on a scheduled basis. A couple of weeks ago I came up with an idea for one of these and set out to create it.

The tools I used were PHP, YOURLs (to create a short URL for the links the bot would post), Abraham Williams’s PHP library for Twitter OAuth and a cron job. Here’s how to do it:

  1. Before you start coding you must register the bot as an app on Twitter. To do that go to http://dev.twitter.com/, log in using the Twitter account you’d like to use the bot from and register your app. After that’s done you’ll have proper OAuth keys to use to post to Twitter from your bot.
  2. For the sake of this example we’ll build a bot that will post a link to the day’s Dilbert comic strip. The URL for a given day’s Dilbert strip is as follows:
    http://www.dilbert.com/strips/YYYY-MM-DD/
    To adjust this URL to be able to work on any day simply do a little PHP date substitution and assign it to a variable as follows:
    $url = "http://www.dilbert.com/strips/".date("Y")."-".date("m")."-".date("d")."/";
  3. Now that you have a variable that holds the URL for the strip you’re ready to send it to YOURLs for shortening. (Different URL shortening services have different ways to use their APIs. If you’re not using YOURLs consult the documentation for the API of your service of choice. Continue at step 6 once you have your shotened URL.) First create the URL for the API call and assign it to a variable:
    $link = "http://yourdomain.com/yourls/yourls-api.php?signature=yoursignature&action=shorturl&format=xml&url=".urldecode($url);
    Replace “yourdomain.com” and “yoursignature” in the above code with the domain of your YOURLs installation and the signature for your YOURLs user name.
  4. Use CURL to initiate the API call and retrieve the resulting XML containing the short URL:
    $ch = curl_init($link);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $reply = curl_exec($ch);
    curl_close($ch);
  5. Parse the XML returned by the above code, retrieve the short URL and assign it to a variable:
    $xml = new DOMDocument();
    $xml->loadXML($reply);
    $node = $xml->getElementsByTagName('shorturl')->item(0);
    $shorturl = $node->textContent;
  6. Create the body of the Tweet and assign it to a variable:
    $message = "Today's Dilbert (".date("F j, Y").") - ".$shorturl;
  7. Now it’s time to log into Twitter using OAuth and post the Tweet. Before doing this be sure to include the OAuth library in the script with a simple include statement:
    require_once('twitteroauth.php');
  8. Define the OAuth keys for the application. These keys were supplied by Twitter when you registered your app in step 1 (use your keys in place of the Xs).
    define("CONSUMER_KEY", "XXXXXXXXXXXXXXXXXXXXXX");
    define("CONSUMER_SECRET", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
    define("OAUTH_TOKEN", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
    define("OAUTH_SECRET", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
  9. Log into Twitter:
    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET);
    $content = $connection->get('account/verify_credentials');
  10. Post your Tweet:
    $connection->post('statuses/update', array('status' => $message));
  11. Upload your code and twitteroauth.php & OAuth.php from the PHP library for Twitter OAuth to a directory on your web space.

Your bot is coded and ready to run. You can try it by going to the URL for it and checking your Twitter feed for the result.

Since it’s coded in PHP you’ll need to set a cron job to run it at the time you’d like it to run. Consult the cron documentation for your web provider to learn how to do this.

If you want to see the bot created above in action then subscribe to my twitter feed and watch it at 8:30 AM Eastern time.



Comments to this post

Tags: , , ,