Aug
23
googleTitleSearch redux
Filed Under Wikis, Weblogs and Outliners |
The googleTitleSearch macro caches the results each time it searches Google in Radio Userland’s inbuilt database. This means that it doesn’t have to rerun the search every time you publish your blog. However, it also means that the See Also: becomes fixed at the point you publish, which isn’t always what you want - particularly if you’re commenting on something recent that Google doesn’t know much about yet, or if the links in question are no longer relevant, months later.
So, I changed the script to store the date that the query was ran, and then rerun it if once a day for the first week, and then again if we’re republishing in more than a month’s time.
I also decided to refactor the code quite a bit, so I could learn more about Radio’s macro system. I learnt a lot about nested functions, and arrays, and how much I really miss map-style functions for dealing with lists!
The new code:
on googleTitleSearch (adrpost) {
if not defined (adrpost^.title) and not defined (adrpost^.link) { return “” }; on htmlForResult (adr) {
on chooseToolTip(adr) {
if sizeOf (adr^.summary) > 0 { return adr^.summary};
return adr^.snippet}; on chooseTitle (adr) {
if sizeOf (adr^.directoryTitle) > 0 { return adr^.directoryTitle };
return adr^.title }; on tidyTooltip (tip) {
return string.replaceAll(searchEngine.stripMarkup(tip), “”", “"”)}; on tidyTitle (title) {
title = searchEngine.stripMarkup (title);
return string.trimWhiteSpace(string.replaceAll(title, “…”, “”)) }; local (url = adr^.url);
local (title = tidyTitle(chooseTitle(adr)));
local (tooltip = tidyTooltip(chooseToolTip(adr)));
return “<a href=”" + url + “” title=”" + tooltip + “”>” + title + “</a>”}; on resultsAsHTML (theResults) {
local (adr, htmlHTML = {});
for adr in theResults { htmlHTML[0] = htmlForResult(adr) };
return htmlHTML }; on daysAgo(aDate) {
return (long(clock.now()) - long(aDate)) / (60 * 60 * 24) }; on haveResults (result) {
if not defined (result^) { return false };
if result^.searchComments == “Sorry, no content found for this URL” {
return false };
if not defined (result^.searchDate) { return false };
local (lastCheck = daysAgo(result^.searchDate));
if (lastCheck > 1 and lastCheck < 7) or lastCheck > 30 { return false };
return true }; on join (aString, anArray) {
local (i, returnString, arraySize = sizeOf(anArray));
for i = 1 to arraySize - 1 { returnString = returnString + anArray[i] + aString };
return returnString + anArray[arraySize] }; local (seeAlso = “”);
try {
local (adrSearchResult = @adrpost^.googleTitleSearchResult);
if not haveResults(adrSearchResult) {
if defined (adrpost^.link) {
adrSearchResult^ = google.search (”related:” + adrpost^.link)};
if not haveResults(adrSearchResult) {
adrSearchResult^ = google.search (adrpost^.title)}};
adrSearchResult^.searchDate = clock.now();
seeAlso = join(” | “, resultsAsHTML(@adrSearchResult^.resultElements)) };
return seeAlso };