June 19, 2002

More Google API hacks for Movable Type

Wow, I was pleasantly surprised with the rush of positive feedback from today's Google API hack for Movable Type users. A number of people asked for some new features, and given that it was only anoither 7 lines of perl code or so, I coded it up!

The new patch to MT 2.11 adds the following new features to the MTGoogleSearch tag. It adds the following new properties that will change the behavior of the tag:


  • title="1" : This will cause the google search query to be the title (MTEntryTitle) of your current blog entry
  • excerpt="1" : This will cause the google serch query to be the entry excerpt (MTEntryExcerpt) of the current blog entry
Those two properties are mutually exclusive (along with the current query="" and related="1" properties).

In addition, if you use strip_html="1" inside the MTGoogleSearch tag, it will remove any html that Google replies with, including <b> tags that surround the search query terms.

This is a patch on the default build of Movable Type 2.11. If you've already installed my previous patch, it might be easier to just back out the patch and install this one.

By the way, I haven't done a lot of testing with the MTEntries query code (entries="1") so if you do use it, please send me feedback.

Posted by dsifry at June 19, 2002 10:09 PM | View blog reactions
Comments

I guess I havn't set up the SOAP module correctly, but I get this error when trying to rebuild my blog:

An error occurred:

Can't call method "send_receive" on an undefined value at C:/Perl/site/lib/SOAP/Lite.pm line 93.

MT::App::CMS=HASH(0x1a657c8) Use of uninitialized value in sprintf at C:/Perl/site/lib/SOAP/Lite.pm line 1457.

Posted by: Urban at June 20, 2002 5:29 AM

OK...the new excerpt and strip_html didn't work for me.

Using excerpt="1" I get:

500 Internal Server Error at /mt/lib/MT/Template/Context.pm line 1259

And it looks as though strip_html calls remove_html (if I read correctly) and Ben Trott said that command may not work...

Ben: "The only thing I can think of is that may be due to the XML modules installed on your server. If your server is using the XML::Parser::Lite module (part of SOAP::Lite), it doesn't decode entities, and that could be the issue. The solution is to get your server admin to install XML::Parser. "

And he's right...my server uses XML::Parser::Lite

I hope that helps your debugging...

Posted by: Chuck Welch at June 20, 2002 8:05 AM

OK...the new excerpt and strip_html didn't work for me.

Using excerpt="1" I get:

500 Internal Server Error at /mt/lib/MT/Template/Context.pm line 1259

And it looks as though strip_html calls remove_html (if I read correctly) and Ben Trott said that command may not work...

Ben: "The only thing I can think of is that may be due to the XML modules installed on your server. If your server is using the XML::Parser::Lite module (part of SOAP::Lite), it doesn't decode entities, and that could be the issue. The solution is to get your server admin to install XML::Parser. "

And he's right...my server uses XML::Parser::Lite

I hope that helps your debugging...

Posted by: Chuck Welch at June 20, 2002 8:05 AM

Sorry about that....I didn't mean to post multiple times, but when I post I didn't get a result page. Instead I get the following error message:

500 Internal Server Error on context.pm line 1259

Posted by: Chuck Welch at June 20, 2002 8:12 AM

Hmmm, I wonder if I overdrew my Google allocation for the day, or if Google had an outage of their SOAP APIs. That server error occurs on the SOAP call to Google.

If you're using XML::Parser::Lite, then you may be SOL because of the issue you mentioned above. The workaround would be to rewrite remove_html or install the full XML modules in your home directory. strip_html is more selective than remove_html - the remove_html property removes ALL html resulting from the tag, which isn't what we want - strip_html removes html from the results of the Google search only.

Let's see if it wasn't a burp in Google's service or in my API key, too.

Posted by: Dave Sifry at June 20, 2002 8:58 AM

Yup...

perl -MCPAN -e 'install XML::Parser'

fixed up the line 1259 error for me... 8-)

Posted by: Jason Jordan at June 20, 2002 9:23 AM

Cool, I just heard back from one of the Movable Type developers that this new feature will be included in their 2.2 release. Dave Winer also links to this, along with a macro for radio to add a "Google It" link to any radio weblog. Actually, that's not really what this patch does, Dave. For a better Radio Macro, see Jake Savin's cool hack.

Posted by: Dave Sifry at June 23, 2002 5:32 PM

This is such an excellent use of the api, cheers Dave. I actually experimented with this a bit and combining the php mentioned on movable blog (www.justagwailo.com/movableblog/archives/no_generate_attribute0619.php), I've got mt checking for keywords in the attribute field. If there isn't any, then it doesn't print the "Google search etc:".
Doesn't sound like much, but it's a cool feature for me as sometimes i just want to post rubbish and other times, well, I want useful posts!
Oh and don't forget to set the 'auto excerpt' setting in mt preferences to '0'. I spent roughly a w/e trying everything (hacking Context.pm to only use the 'exceprt' variable among other things) and I couldn't work out why searches were appearing when I hadn't specified anything in the excerpt field. Such a horrible case of RTFM, I'm afraid.
Anyway, I've stuck the sample code on my site (www.guydickinson.com/google.html) if anyone's interested. Excuse the fairly ropey coding, my php knowledge is minimal.

Posted by: guy at June 24, 2002 1:30 PM

nice article, keep up the good work. lookup zip code

Posted by: zip codes at March 15, 2003 4:14 PM

Great article! I have been able to add the feature you described, but I'm not sure how to grab the ampersands out of the URLs Google returns in order to change them to the HTML code for ampersand for xhtml1.1 compliance.

Any thoughts?

Posted by: Kevin at August 13, 2003 9:09 PM