Skip to content

Instantly share code, notes, and snippets.

@chrisgward
Created July 20, 2014 05:59
Show Gist options
  • Save chrisgward/740014438bb4b986ecaf to your computer and use it in GitHub Desktop.
Save chrisgward/740014438bb4b986ecaf to your computer and use it in GitHub Desktop.
WolframAlpha Nromal Script (Lain compatible)
package require tdom
proc wolframalpha {nick channel arg} {
if {$arg == ""} {
return "Please provide a search query."
} elseif {[string first "Necrodoom" $arg] != -1} {
return "Did you mean: stop spamming $::botnick?"
}
set xml [wget http://api.wolframalpha.com/v2/query?format=plaintext&appid=APPID&[::http::formatQuery input $arg]]
set document [dom parse $xml]
set result [$document selectNodes /queryresult]
set success [$result getAttribute success]
set error [$result getAttribute error]
if {$success == "true" && $error == "false"} {
set nodes [$result selectNode pod]
foreach node $nodes {
if {[$node hasAttribute primary] && [$node getAttribute primary] == "true"} {
set response "Result: [[lindex [$node selectNode subpod/plaintext] 0] text]"
$document delete
return $response
}
}
foreach node $nodes {
if {[$node hasAttribute id] && [$node getAttribute "id"] == "Value"} {
set response "Result: [[lindex [$node selectNodes subpod/plaintext] 0] text]"
$document delete
return $response
}
}
return "Result: [[lindex [[lindex $nodes 0] selectNodes subpod/plaintext] 0] text]"
} else {
set err [$result selectNode error]
if {$err == ""} {
set didyoumeans [$result selectNodes didyoumeans]
if {$didyoumeans == ""} {
set response "Unknown error."
$document delete
return $response
} else {
set response "Did you mean: [[lindex [$didyoumeans selectNodes didyoumean] 0] text]?"
$document delete
return $response
}
} else {
set code [[$err selectNodes code] text]
set msg [[$err selectNodes msg]]
set response "Error: $code: $msg"
$document delete
return $response
}
}
$document delete
return "No result"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment