The Most Painful Thing I Ever Ate

Back before Christmas, I discovered a restaurant called Genghis Grill. It’s Mongolian BBQ, and it’s quite tasty. I eat there about once a week, because it is about a 10 minute walk from my office. Anyway, I was there today, and made the same bowl I always make. It had some thin-sliced beef, a metric ton of carrots and broccoli, some chickpeas, black beans and soybeans. As usual, I put a few dried red chile peppers on it. I never eat those peppers; I just like to have them mixed in during cooking to add some flavor.

While I was waiting for my food to cook, a few co-workers who were also there invited me to their table. Once the food arrived, I was talking and eating, and not really paying attention to what made its way onto my fork. You can see where this is going. At one point, I chewed up something that was kind of hard to chew up; sort of leathery. I thought to myself, “Man, they really burned that carrot.” It wasn’t a carrot.

About ten seconds later, it felt as if I were sucking on a blow torch. The entire left side of my mouth felt as if it were melting. My nose started running. My eyes started watering. I’m sure I was turning red in the face. One of my table mates asked, “Eat something hot? Need some more water?” I replied, “Yes. And the only thing that will help this is milk. Or vodka.” As I was chugging my water, knowing that it only provided momentary relief, I was poking around the bowl with my fork, looking for the other two peppers. They were not there. Apparently, all three peppers had gotten stuck together, and I’d chewed them all up at once.

It took a good fifteen minutes before my mouth started to calm down.

Fast Apple Customer Support

Today I bought the 24 song, 2.3 hour-long Van Halen record Live: Right Here, Right Now from the iTunes store. But there was a snag: “Why Can’t This Be Love?” failed with an error -100000. I tried restarting it a couple of different ways, but each time it would download it, then restart, three or four times, until it would finally choke with error -100000. I even downloaded the album on a Windows machine and 23 songs downloaded fine, but that one track failed with the same error. This pretty much proved to me that it was a problem on Apple’s end. I then went to my account in the iTunes store and reported an error with the track.

I received an auto-response pretty quickly with some things to try, but nothing useful. An hour or so later, I got an email from a human at Apple that basically told me to try what I had already tried and to let them know if I was still having problems, as well as providing some other info like ISP, Internet connection type, etc. I emailed them back with the details they asked for, and told them what all I had tried, including trying to download to another machine. I wondered how hard it was going to be to convince them that the problem was on their end.

A couple of hours later, I received another email, this time from a different human. I immediately assumed I would have to re-explain everything. Much to my surprise, this is what the email said:

I understand that you are unable to download the song “Why Can’t This Be Love” as your [sic] getting error -1000000. I know how eager you are to have this resolved at the earliest. I will be glad to assist you today.

Joey, please accept my sincere apologies for the frustration this download has caused. I took the liberty of removing the file causing the issue from your download queue. To give Apple time to investigate the issue and make any corrections that may be necessary, please wait at least two weeks before repurchasing this title.

I have issued a replacement song credit to your account. You can use the credit to buy a song of your choice from the iTunes Store.

So, while it kind of stinks that it could be two or more weeks before I can get the track replaced, it’s nice that they refunded me the money for the track so quickly. That’s nice, fast, customer service.

New WordPress Sociable Plugin Has Unintuitive Setup

I got a notification this morning that the Sociable plugin for WordPress had a new version available, and did I want to install it. I try to keep my plugins updated, so I upgraded. Sociable now has two modes: Skyscraper and Classic. Classic is what you’re used to, with a row of icons under a post. Skyscraper is a tall and thin tower that lives in the left-hand margin of your site, scrolling along with the page. Skyscraper is the new default, but going back to Classic is what is unintuitive.

In your admin page, if you hover over the new menu “Select Sociable Plugin” you will see three options: Select Sociable Plugin, Sociable Options, and Skyscraper Options. Selecting the first one brings you to a page explaining the difference between the two modes, with ginormous buttons at the bottom to choose which version you want. Clicking the button for either mode takes you to the options for that mode, but doesn’t actually change which mode is selected.

At the bottom of each mode’s options page is a checkbox labeled, “Activate…” and the mode name. I assumed that checking the one for Classic mode was all I needed to do, but clearing my cache and viewing the page showed that not to be the case. You must also uncheck the “Activate…” checkbox on the Skyscraper options page to enable Classic mode. If you don’t uncheck it, then you will still get the Skyscraper showing up on your posts. If you decide to switch back from Classic to Skyscraper, you have to uncheck Classic, then go to the Skyscraper options page and check that one.

Since this is a binary choice, they need to have ONE place to enable one and disable the other, or at least explain to you that you have to take the extra step.

Once More, This Time With Clojure

If you happened to read my post from the other day entitled My New “Top Artists Last 7 Days” Widget, you know that I went through three iterations of getting it going. The final solution, written in Ruby worked well. Until bands like Motörhead, Mötley Crüe and Einstürzende Neubauten showed up in the list. At that point, the HTML parsing library I was using would barf, and processing would stop, leaving the list showing on the blog in an incomplete state. It wasn’t the library’s fault; apparently Ruby still has problems dealing with non-ASCII characters. I did everything I thought I needed to do to tell Ruby that it would be dealing with UTF-8 encoding, but it just kept right on barfing.

I was left with only two choices: stop listening to any band with an umlaut in the name (and God help me if any of my Scandinavian bands popped up, with the Ø or å characters), or rewrite the stupid program, again, in a language that I knew could easily deal with UTF-8.

Since I’ve been working in Clojure a lot lately, it seemed lika the logical choice. I spent about an hour working on it last night, and I ended up with a working program and a bit more Clojure experience. Here’s the program for your edification, with a description to follow:

(ns lastfmfetch.core
(:gen-class))

(require '[clj-http.client :as client])
(import '(java.io PrintStream)
'(org.htmlcleaner HtmlCleaner))

(defn get-artist-and-playcount [cell]
(let [title (.getAttributeByName cell "title")
[match artist playcount] (re-matches #"^(.+), played ([wd]+)s*S*$" title)
playcountStr (if (= playcount "once") "1" playcount)]
[artist playcountStr]))

(defn get-url [cell]
(let [links (.getElementsByName cell "a" true)
a (first links)
href (.getAttributeByName a "href")]
(str "http://last.fm" href)))

(defn fetch-data [filename]
(let [response (client/get "http://www.last.fm/user/joeyGibson/charts?rangetype=week&subtype=artists")
cleaner (HtmlCleaner.)]
(if (= (:status response) 200)
(with-open [out (PrintStream. filename "UTF-8")]
(.println out "<html><head><meta charset="UTF-8"/></head><body><ol>")
(doto (.getProperties cleaner)
(.setOmitComments true)
(.setPruneTags "script,style"))
(when-let [node (.clean cleaner (:body response))]
(let [subjectCells (take 5 (.getElementsByAttValue node "class" "subjectCell" true true))]
(doseq [cell subjectCells]
(let [[artist playcount] (get-artist-and-playcount cell)
url (get-url cell)]
(.println out (str "<li><a href='" url "'>" artist "</a>, Plays: " playcount "</li>"))))))
(.println out "</ol></body></html>")))))

;; Main
(defn -main [& args]
(if (< (count args) 1)
(println "Usage: lastfmfetch <output_file>")
(fetch-data (first args))))

I ended up using a library called clj-http to handle the fetching of the URL. It’s a Clojure wrapper for the Apache HTTP Commons library, and was really easy to use. I’m using Leningen, by the way, so including clj-http was just a matter of including a line in the project.clj file. I also used a Java library called HTMLCleaner, that fixes broken HTML and makes it available as a DOM. Since it is also in Maven Central, it was easy to include by adding another line to the project file.

(defproject lastfmfetch "1.0.0-SNAPSHOT"
:description "Fetch chart data from Last.fm"
:dependencies [[org.clojure/clojure "1.3.0"]
[net.sourceforge.htmlcleaner/htmlcleaner "2.2"]
[ clj-http "0.2.6"]]
:main lastfmfetch.core)

The -main function begins on line 38, but all it really does is check that there is a single command-line argument, and exits with a usage message if there is not. It then calls the fetch-data function, which begins on line 20.

On line 21, we declare two locals; one that will contain the results of fetching the web page, and one that is the HTML cleaner. If the fetch of the URL was successful, the status code will be the standard HTTP 200. If we got that, we then open a PrintStream on the filename given, specifying that it should be encoded with UTF-8. (I’ve been working with Java for a very long time, and I always assumed that since Java strings are Unicode, files created with Java would default to UTF-8. That is not the case. That’s why there’s a second argument when creating the PrintStream, and why I’m not using a PrintWriter.) We then print the first part of the output HTML file, set a couple of options to HTML Cleaner that cause it to strip comments, style and script sections from the HTML, and then start doing the real work.

On line 29, we declare a local called node that will contain the output of HTML Cleaner if it successfully parsed and cleaned the HTML. That’s what when-let does; it assigns the local as long as the function returns something truthy and then executes its body. If that function doesn’t return something truthy, the rest of the code is skipped. We then take the first five elements from the HTML that have an attribute called “class” with a value of “subjectCell”. These are table cells. We then loop over them, extracting the artist and playcount value, and the URL. We do these things in two separate functions.

The function called get-artist-and-playcount, starting on line 8, takes the table cell as input. It then gets the attribute called “title” and uses a regular expression to pull out the artist and playcount values. If the playcount is the word “once,” it converts it to a 1, so all the values are numeric. It then returns the two values as a vector.

The function called get-url, starting on line 14, also takes the table cell as input. It then gets all the “a” elements from the cell (there’s only one), and then gets the “href” attribute’s value, which is the URL.

Back at line 34, we take the three values we extracted with the two support functions and concatenates them together into HTML that will be a single line in an ordered list. We then output all the necessary closing tags to make the HTML well-formed, and we’re done.

While the Clojure code is a bit more dense than the Ruby code, it’s actually four lines shorter. And it handles Unicode characters, which makes me happy.

We Have To Get the Money Out of Politics

It costs a lot to get elected to high political office. A lot. So much, in fact, that a candidate has to either be independently wealthy, or they have to sell their souls votes to those who would finance their campaigns. This is one of the many things that is wrong with our political system. There is no possible way that someone like me could run for anything higher than local office; I don’t have the money, and even if some donors wanted to fund me, I would’t want to be beholden to them on their pet issues. So what do we do about it?

For one, we overturn the horrible Citizens United case. Senator Sanders is introducing a Constitutional amendment, called the Saving American Democracy amendment, to do just that, stating in no uncertain terms that to be considered a “person,” a pulse is required. It would forbid corporations from being classed as “people” and from donating to any politician. It will never pass or be ratified, but at least he’s trying. You should watch this video of him explaining it on the floor of the Senate.

The second is public financing of elections. I used to think this was a horrible idea, but upon more level-headed reflection, it’s the only way to go. Michael Moore has a post today about this very thing. The worst example of the amount of money in elections was Meg Whitman, running for Congress in 2010, who spent $119 million of her own money. You can’t compete with that, unless you also have access to that sort of money. If elections were publicly funded, and everyone who runs for office has the same financial limitations placed upon them, the need for obscene amounts of cash to campaign goes away. Now, I don’t know how the determination would be made of who counts as a viable candidate, since we can’t very well fund everyone who says they want to run for office. That would have to be worked out later. But, just like Senator Sanders’ amendment, this will never happen.

Third, we have to stop the “revolving door” between the Congress and the lobby. In that same Michael Moore post, he mentions Senator Michael Bennett, a Democrat from Colorado, who has introduced something he calls a Plan for Washington Reform that would, among other things, place a lifetime ban on former Congressmen from becoming lobbyists. You have to read this! I just developed a man-crush on Senator Bennet as I was reading it. The things he proposes would fix so many problems in Washington. That means, of course, it has no chance of passing.

Oh, one more thing. Have you ever wondered what sort of financial distribution there is among our elected “representatives”? In other words, how many millionaires are there vs. how many average joes. Here’s a handy chart to answer that question.

Anyway, this wasn’t the most eloquent explanation, but that’s what I think needs to happen. Go read Michael Moore’s blog post, then watch Senator Sanders’ video, then read Senator Bennet’s proposal.

Fizzing and Buzzing With Clojure

I’ve known about the FizzBuzz problem for a few years. I’ve written solutions for it in a few languages, but never posted them. I’ve been working with Clojure lately, and after reading articles about how many job applicants can’t solve a simple problem like this here, here and here, I decided to do a Clojure version. (It baffles me that someone who claims to be a developer can’t come up with a solution for this, no matter how good or bad it might be.)

I ended up doing it three different ways. The first is a simple first-cut solution. The second is somewhat better, I think, and the third is a refinement of the second. In all three cases, they use a nested function to do the evaluation, and return a lazy, infinite sequence. Here’s the first

[clojure]
(defn fizzbuzz []
(map (fn [i] (cond
(and (= (rem i 3) 0)
(= (rem i 5) 0)) "FizzBuzz!"
(= (rem i 3) 0) "Fizz!"
(= (rem i 5) 0) "Buzz!"
:else i))
(iterate inc 1)))

(doseq [i (take 100 (fizzbuzz))]
(println i))
[/clojure]

This solution does work, but I have a problem with the fact that the division tests are done twice. I think doing those tests twice increases the chances of making a mistake. The second version does the tests one time, assigning the results to locals. It then checks them for nil, and concatenates them together, relying on the fact that a nil will not print.

[clojure]
(defn fb []
(let [fb1 (fn [n]
(let [fizz (if (= (rem n 3) 0) "Fizz")
buzz (if (= (rem n 5) 0) "Buzz")]
(if (or fizz buzz)
(str fizz buzz "!")
n)))]
(map fb1 (iterate inc 1))))

(doseq [i (take 100 (fb))]
(println i))
[/clojure]

In this version, instead of passing an anonymous function to map, I assigned it to a local in a let expression. You can see that I only do the math once, assigning locals with either the appropriate word, or nil. I then check that one or the other of the locals are non-nil, cat them together and return it. If both are nil, the number itself is returned.

The third version is almost identical to the second. The only difference is that the second one used a let expression, and the third one uses a letfn expression. It’s effectively the same thing, but the third one is ever-so-slightly shorter, and I think every-so-slightly easier to read.

[clojure]
(defn fb2 []
(letfn [(fb3 [n]
(let [fizz (if (= (rem n 3) 0) "Fizz")
buzz (if (= (rem n 5) 0) "Buzz")]
(if (or fizz buzz)
(str fizz buzz "!")
n)))]
(map fb3 (iterate inc 1))))

(doseq [i (take 100 (fb2))]
(println i))
[/clojure]

I don’t claim that these are particularly good solutions, though I do claim they work correctly. Any Clojure experts care to point out problems and/or offer suggestions?

My New “Top Artists Last 7 Days” Widget

Note Redux: I changed my approach, yet again. Scroll farther down to see the latest.

Note: I changed my approach on this, so scroll down to see how I’m doing it now.

I’ve been wanting a widget or an auto-post on the blog for a while that would show my most-listened-to bands over the previous week. Tumblr users have had something like this for a while, and there were efforts to do this for WordPress before, but they either don’t seem to work with the latest versions of WP, or they only pulled top tracks (not artists), or they pulled album covers, instead of text. All of that is to say that I couldn’t find anything pre-made to use.

So, I had to roll my own. I did so in about 10 minutes using the PHP Code Widget and the script on this page. The only drawback to this is you have to get a developer account with last.fm, but it’s free, so no big deal there. I installed the PHP Code Widget , then pasted the script into a new widget. The only changes I had to make were to replace the appropriate bits in the script with my info, and to escape a couple of double-quotes. Now if you look down the right side of the blog, below the Twitter and Facebook links, you’ll see a rolling record of my top-artists. In case you were wondering what I’ve been listening to. 🙂

The only thing I’m not sure about is how this will work with the two levels of caching I use (WP Super Cache and Cloudflare). I suppose we’ll see in the next few days, eh?

11/23/2011 Update: I decided that I didn’t like the way I was doing this, for a couple of reasons. First, each time someone viewed the page, it would be making a call to Last.fm for my stats. This is too often. Also, the values returned using the developer API were at odds with what you can get just going through the web. So what I did was write a Ruby script to pull the feed once a day, parse it and output HTML to a file. I then used the PHP Code Widget to include it. Far simpler, in my opinion.

Here’s the Ruby code:

[ruby]
#!/usr/bin/ruby

require ‘rexml/document’
require ‘open-uri’

include REXML

open("http://ws.audioscrobbler.com/2.0/user/joeyGibson/weeklyartistchart.xml") do |http|
response = http.read
doc = REXML::Document.new response

index = 0

File.open(ARGV[0], "w") do |out|
out.write("<html><head>n")
out.write("<meta charset="UTF-8"/>n")
out.write("<body><ol>n")

doc.elements.each("weeklyartistchart/artist") do |artist|
break if index == 5

out.write "<li><a href="#{artist.elements[‘url’].text}">#{artist.elements[‘name’].text}</a>, Plays: #{artist.elements[‘playcount’].text}</li>n"

index += 1
end

out.puts("</ol></body></html>n")
end
end
[/ruby]

and here’s the PHP that loads it:

[php]
<?php include("/tmp/artists.html"); ?>
[/php]

That’s it.

11/26/2011 Update: Well, I’ve changed it again. I discovered that the RSS feed I was pulling is not updated with any sort of frequency. It certainly doesn’t represent the “last seven days” as it claims to. At any rate, it differs greatly from what Last.fm shows on the web. So I decided to grab the HTML and pull out the interesting bits. I wrote another Ruby script, this time using Hpricot to parse the HTML, which took about 10 minutes. So now, what you see on the right should be the current values for the “last seven days.” Here’s the latest script:

[ruby]
#!/usr/local/bin/ruby

require ‘rubygems’
require ‘hpricot’
require ‘open-uri’

open("http://www.last.fm/user/your-username-here/charts?rangetype=week&subtype=artists&quot;) do |http|
doc = Hpricot.parse(http.read)

count = 0

File.open(ARGV[0], "w") do |out|
out.write("<html><head>n")
out.write("<meta charset="UTF-8"/>n")
out.write("<body><ol>n")

doc.search("td[@class=subjectCell]").each do |subjectCell|
break if count == 5

artistString = subjectCell.get_attribute("title")

artistString =~ /^(.+), played (d+) times$/
artist = $1
playCount = $2

subjectCell.search("a").each do |a|
url = a.get_attribute("href")
url = "http://last.fm#{url}"

str = "#{artist}, #{url}, #{playCount}"

out.write "<li><a href="#{url}">#{artist}</a>, Plays: #{playCount}</li>n"
end

count += 1
end

out.puts("</ol></body></html>n")
end
end
[/ruby]

I’m hopeful this is the last change.

Lego VW Microbus

Thomas and I spent six hours this past weekend building this. It was my big splurge from our last trip to Disney World, back in October. What is it? It’s a classic, 1962 Volkswagen microbus. It was expensive, and had far too many pieces (1,332), but it was a blast putting it together. Now it’s sitting on my desk, where I can gaze at it and/or remove the roof and pretend it’s real… 🙂

You can read more about the real microbus here.