Lacking Natural Simplicity

Random musings on books, code, and tabletop games.

define-word results using wordnik stopped working

I use define-word, a package from melpa (DWM), that looks up the definition of a word online. It uses wordnik.com by default. Recently it stopped returning definitions.

I did a little digging, and it turns out that it works when you visit the url with Chrome, but not if you just try to download it with curl, or url-retrieve in Emacs. Adding a “User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_5_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36” with curl’s -H option made it work.

curl -i -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_5_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36' http://wordnik.com/words/importunate

So I added a let in my advice for define-word that binds url-request-extra-headers to an alist with "User-Agent" as the key and "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_5_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36" as the value, and it works again.

My solution looks something like this (simplified from my actual advice, which does other things too):

(defadvice define-word (around your-around-define-word activate)
  "Dynamically bind tkb-define-word-word to the word passed in."
  (let ((url-request-extra-headers
         '(("User-Agent" . "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_5_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36"))))
    ad-do-it))

I opened an issue on github, suggesting it might be a good idea to add a simpler way to add a User-Agent: header around define-word--to-string’s call to url-retrieve-synchronously.

Print Friendly and PDF

Comments

Comments powered by Disqus