Skip to content

Instantly share code, notes, and snippets.

@edorcutt
Created October 11, 2010 20:15
Show Gist options
  • Save edorcutt/621135 to your computer and use it in GitHub Desktop.
Save edorcutt/621135 to your computer and use it in GitHub Desktop.
Annotate search engine results with data from Social Media
ruleset a169x136 {
meta {
name "BiGoHoo"
description <<
Annotate search engine results with data from Social Media
BiGoHoo => Bing Google Yahoo :)
>>
author "Ed Orcutt"
logging on
use css resource "http://kynetx.edorcutt.org/bigohoo/bigohoo.css"
// Facebook Application ID and Application Secret
key facebook {
"consumer_key" : "147894541903202",
"consumer_secret" : "YOUR MILAGE MAY VARY"
}
}
dispatch {
domain "www.google.com"
domain "www.bing.com"
domain "search.yahoo.com"
}
global {
datasource tweeturl <- "http://urls.api.twitter.com/1/urls/count.json?url=" cachable for 1 minutes;
datasource buzzurl <- "http://www.googleapis.com/buzz/v1/activities/count?alt=json&url=" cachable for 1 minutes;
// Format number with commas every 3 decimal places
// http://snipplr.com/view/7345/format-number-with-commas-every-3-decimal-places/
emit <<
function addCommas(nStr) {
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
>>;
}
////////////////////////////////////////
// TEST: Confirm Facebook Authorization
rule welcome is inactive {
select using ".*" setting ()
if (facebook:authorized())
then
notify("Facebook Authorized", "Good to go ...") with sticky = true;
}
////////////////////////////////////////
// Authorize Facebook Application
rule facebook_app_auth is active {
select using ".*" setting ()
if (not facebook:authorized()) then
facebook:authorize(["user_likes"])
with opacity = 1 and
sticky = true;
fired { last; }
}
////////////////////////////////////////
// Search Annotation
rule begin_annotate_rule is active {
select using "google.com|bing.com/search|search.yahoo.com/search" setting()
annotate:annotate("socialrate") with
remote = "event" and
placement = "before" and
wrapper_css = {
"height" : "18px",
"margin" : "4px 0px 2px"
};
}
////////////////////////////////////////
// Search Annotation Event
// This rule looks for the webevent annotate_search and executes only
// when the name is rentals
rule socialrate_annotate_event is active {
select when web annotate_search name "socialrate" setting ()
foreach annJSON setting (annKey,annValue)
pre {
// grab data passed in via annotation framework
annREFI = page:env("annotate_instance");
annJSON = page:env("annotatedata").as("json");
// pull the full URL for the search item
itemURL = annValue.pick("$.url");
// -----------------------------------------------
// retrieve count of Facebook Likes this URL
facebookJSON = facebook:ids({"ids" : [itemURL]});
facebookShares = facebookJSON.pick("$..shares") + '';
// What a Hack!
facebookHack = facebookShares.replace(re/ARRAY.*/, "0");
facebookCount = addCommas(facebookHack);
// -----------------------------------------------
// retrieve count of Twitter shares for this URL
twitterJSON = datasource:tweeturl(itemURL);
twitterCount = twitterJSON.pick("$..count");
// -----------------------------------------------
// retrieve count of Buzz shares for this URL
buzzJSON = datasource:buzzurl(itemURL);
buzzCount = buzzJSON.pick("$..count");
}
every {
annotate:add_annotation(annKey,
"<div class='fbwidget' <span class='fblike'> <b>" +
addCommas(facebookHack) +
"</b> Likes</span></div>",
annREFI);
annotate:add_annotation(annKey,
"<div class='tbwidget'>" +
"<span class='tbbutton'></span>" +
"<span class='tbcount'><span class='tbnumber'>" +
twitterCount +
"</span></span></div>",
annREFI);
annotate:add_annotation(annKey,
"<span class='bzwidget'>" +
buzzCount +
"</span>",
annREFI);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment