Scraping Twitpics with PHP [Coding]

Update! My twitpic scraper (as well as search API calls) have been integrated into NCSU’s Tweetgator. Check it out on Github!

A couple months ago, IU East re-vamped its twitter wall. We incorporated a codebase originally developed by NCSU, and then I extended it by adding inline hashtag searching and a twitpic scraper.

At the time I wrote it, I could not find any other existing Twitpic scraper – Twitpic doesn’t have a formal API (or at least, it didn’t then; I don’t think it does at the time of this writing, either).

Effectively what this script does (see after the jump) is to browse the Twitpic site, parse out the image IDs, and then re-create the Twitpic images. It is somewhat rudimentary in that it does not cache nor does is it actually download the images — anyone reading this may feel free to extend the code into something like that. Continue reading

Syndicating your shared Google Reader items

I’m an avid user of Google Reader. I follow a couple dozen different websites, check it daily (and sometimes quasi-daily), and have a very elaborate system of tags.

One thing that I did a year or two ago was to take my public broadcast feed of my shared items and integrate it into my main website directly. The goal was to have the homepage of my website always have fresh content by linking it to my shared Reader items (I share anywhere from 3-8 items per day, on average).

Originally, my solution was a bit of a hack: I used Magpie RSS reader to consume the feeds, then did some serious PHP acrobatics to parse out the Google Reader feed — what I failed to understand, at the time, was the fundamental differences between RSS and Atom feeds (subtle, yet significant). All in all, I think it took somewhere from 100-150 lines of code to effectively snatch the feed and parse it out; and it wasn’t perfect, either — frequentle the title and source would get mashed together. But for that particular time, it was fine.

The other night, though, I decided to try and fix it up proper. I discovered that the Zend Framework has some native Atom Feed classes and thought this would be a good place to start. (Details, including my PHP source code, after the jump)

Continue reading

Rock, Paper, Scissors

The current assignment I have for my PHP class is to create a web-version of the children’s game, “Rock Paper Scissors”. As part of the description for the assignment, the instructor mentioned the World RPS website. Yes, “World RPS” as in: “The International Organization for Rock/Paper/Scissors Championships.” I’m totally serious.

cells contain the winner
  Rock Paper Scissors
Rock - Paper Rock
Paper Paper - Scissors
Scissors Rock Scissors -

At first, I thought it was funny. You know, a bunch of people getting together and having a good time. Probably drinking alcohol, cheering their favorite player on, etc. Then I found their “Gambit Plays” and “Advanced RPS” links. (Not hard to find, they’re on the left-nav of the home page) Gambit Plays illustrates the “8 great gambit plays”. Let me provide you with a choice sample and then explain why this bothers me:

[Picture, illustrating: "Rock, Paper, Paper", entitled "Fistful of Dollars". The caption reads: "This gambit is still arguably one of the great surprise offensive moves"] This move took the 1967 RPS World Championships by surprise and is arguably still one of the great surprise offensive moves. The rapid switch from offensive to defensive play can force an opponent into a vulnerable spot.

There are 8 others of a similar nature. The problem I have with this is that Rock,Paper,Scissors does not inherently HAVE a strategy. There is no implicitly dominant strategy. Picking “Rock” is the same as picking “Paper” is the same as picking “Scissors”. Each choice trumps one other and loses to one other. They are all equal. Furthermore, the page attempts to personify the individual choices with psychological traits:

Use of rock as an opening move is seen by many players to be a sign of aggression. Rock also happens to be the most effortless of the throws and fast reactions are never required to employ it with success. By careful examination of the options and atmosphere of play, a well-placed rock will crush a carelessly thrown pair of scissors every time.

Let’s imagine, for a moment, that instead of throwing a “rock”, “paper”, or “scissors”, you’re throwing a letter: “A, B, C”. A beats B, which beats C, which beats A. Same relationship. Dig? Now, in that light, is ANY ONE or ANY SEQUENCE of throws ANY DIFFERENT than another? If I throw “A” is that a more aggressive play than throwing “B”? And if so, how? There’s no more risk involved in throwing “A” than there is in throwing “B” or “C”. I have nothing to gain or lose by choosing one over another. The same goes for RPS. If I throw “Rock,” that’s inherently no different than throwing Paper or Scissors. In fact, the best strategy to have is to have no strategy at all! Seriously! Think of it like this: There are three choices. Any given choice you choose is a 33% chance of losing and a 66% chance of not-losing (either a win or a tie). The only way you can disadvantage yourself is by playing with a system because your opponent might be able to predict your next move. If you play TRULY RANDOMLY (which is harder than it sounds) you will win 1/3 of the time (and lose and tie 1/3 of the time). Rock, Paper, Scissors is no different than if you each picked Heads or Tails and simultaneously flipped coins. Both heads or both tails means victory for one player, a mix of heads and tails or tails and heads means a tie. (Technically speaking, the odds of winning are now reduced to 25%, but the odds of tying are raised to 50%, so its comparable)

Adding a Fourth

RPS is not really a true game of strategy. But how do you make it one? Force the players to make strategic or economical decisions. Take some REAL risk. For example, by simply adding in a fourth symbol. We’ll call it, for the sake of example, “Tofu.” (Please feel free to come up with a better name) The new matrix looks like this (the cell value shows the victor for any given matchup):

  Rock Paper Scissors Tofu
Rock - Rock Rock Tofu
Paper Rock - Scissors Paper
Scissors Rock Scissors - Scissors
Tofu Tofu Paper Scissors -

The first thing you’ll notice is that Rock now beats paper (as I’ve always thought it should. Ever heard of a paper-weight?). The new contender, tofu, only beats rock, and loses to everything else. See the difference in strategy now? Rock beats 2/3 of the things thrown against it. All the other choices only beat 1/3 of the opposing choices. But the essential difference here is that Rock and Tofu balance each other. Rock may seem like a dominant strategy because it beats 50% of the opposition. But if the other player plays “Tofu” then the Rock player loses. The opponent is also aware of this dynamic. Playing “Rock” is now requires more decision making, and playing “Rock, Rock, Rock” (the “Avalanche” gambit from worldrps) is now most definitely a Gambit! A game would probably go something like this:

Round Player A Player B Comments
1 Rock Rock Both players open with rock. Not surprising. Both players should also *expect* that their opponent will open with rock as well. It’s a dominant first-turn strategy
2 Rock Paper Player B was expecting Player A to switch from rock, but Player A is playing more aggressively, demanding that Player B keep the status quo.
3 Tofu Rock Player B returns to the Rock strategy a turn too late, and Player A anticipated his switch to the dominant strategy and plays the foil.
4 Scissors Scissors Both players switch to the inferior strategy (paper or scissors) in hopes the other will return to the dominant strategy (rock).
This round is crucial: Player B must contemplate, quickly, what Player A will throw. Will Player A return to the dominant Rock strategy or will player A be expecting him to think that, and play something else instead?
5 Tofu Paper Player A attempts to gambit the last round by throwing Tofu, expecting B to return to the dominant Rock strategy.
Player A is now in a predicament. Which strategy should he adopt? He could continue to play Tofu, or another inferior strategy, but he would stand a 2/3 chance of losing. He could switch back to Rock, which has a better chance of winning unless “B” decides to play Tofu.
7 Scissors Tofu Player A guesses correctly for the win. Player B was anticipating Player A to return to the dominant strategy but was ultimately mistaken.

While the metagaming here may look like real RPS, the important difference is one of statistics. You cannot just randomly throw one of the four and expect to win the same amount as your opponent. It *forces* you to consider what your opponent is doing instead of just blindly throwing whatever (Blindly throwing random hands will cause you to lose 50% of the time, tie 25% of the time, and win 25% of the time.)

I do want to say that I don’t mean this to be  knocking on the RPS champs at all — I’m sure the championships are a whole lot of fun, exciting, and a good time. I’m merely saying the the game is, in itself, not a strategy game, ergo there are no “gambits”.