User:Mutante/googlefight

googlefight implementation in know-it-all:

trigger
# googlefight trigger !gf if (substr($con['buffer']['text'], 0, 4) == "!gf ") {

if (substr($con['buffer']['text'], 4, 1) == "\"") { $arguments = explode("\"", $con['buffer']['text']); $term1 = $arguments[1]; $term2 = $arguments[3]; $term1 = str_replace(" ","+",$term1); $term1 = "\"$term1\""; $term2 = str_replace(" ","+",$term2); $term2 = "\"$term2\""; } else { $arguments = explode(" ", $con['buffer']['text']); $term1 = $arguments[1]; $term2 = $arguments[2]; }

cmd_send(prep_text(googlefight($term1, $term2))); }

function
function googlefight($term1, $term2) { $url1 = "http://www.google.de/search?q=". $term1; $url2 = "http://www.google.de/search?q=". $term2;

#echo $url1."\n"; #echo $url2;

$buffer1 = file_get_contents($url1);

# echo $buffer1;

$pieces1 = explode("hr ", $buffer1); $piece1 = $pieces1[1];

$pieces2 = explode("", $piece1);

$hits1 = $pieces2[0]; $hits1 = str_replace(".", "", $hits1); if (!is_numeric($hits1)) { $hits1 = 0; }

$buffer2 = file_get_contents($url2);

$pieces1 = explode("hr ", $buffer2); $piece1 = $pieces1[1];

$pieces2 = explode("", $piece1); $hits2 = $pieces2[0]; $hits2 = str_replace(".", "", $hits2); if (!is_numeric($hits2)) { $hits2 = 0; }

if ($hits1 > $hits2) { $hits1 = number_format($hits1); $hits2 = number_format($hits2);

$output .= "Googlefight: '$term1' beat '$term2' with $hits1 to $hits2 hits."; }       elseif ($hits1 == 0 AND $hits2 == 0) { $hits1 = number_format($hits1); $hits2 = number_format($hits2);

$output .= "Googlefight: No results, neither term has any hits. ($hits1)"; }       elseif ($hits1 == $hits2) { $hits1 = number_format($hits1); $hits2 = number_format($hits2);

$output .= "Googlefight: Draw! Both terms have the same number of hits. ($hits1)"; }        elseif ($hits2 > $hits1) { $hits1 = number_format($hits1); $hits2 = number_format($hits2);

$output .= "Googlefight: '$term2' beat '$term1' with $hits2 to $hits1 hits."; } else { $output .= "Googlefight: unknown error fnord"; }

return $output; }