wolfram-alpha: query api for Wolfram Alpha.

db4
John Benediktsson 2011-08-19 13:17:32 -07:00
parent 0364eae439
commit 3cef26fe3c
3 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1 @@
John Benediktsson

View File

@ -0,0 +1 @@
Query API for Wolfram Alpha

View File

@ -0,0 +1,50 @@
! Copyright (C) 2011 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: accessors colors.constants formatting http http.client
images.gif images.http io io.styles kernel namespaces sequences
splitting ui urls.encoding xml xml.data xml.traversal ;
IN: wolfram-alpha
SYMBOL: wolfram-api-id
! "XXXXXX-XXXXXXXXXX" wolfram-api-id set-global
<PRIVATE
: query ( query -- xml )
url-encode wolfram-api-id get-global
"http://api.wolframalpha.com/v2/query?input=%s&appid=%s"
sprintf http-get nip string>xml ;
PRIVATE>
: wolfram-image. ( query -- )
query "pod" tags-named [
[
"title" attr "%s:\n" sprintf H{
{ foreground COLOR: slate-gray }
{ font-name "sans-serif" }
{ font-style bold }
} format
] [
"img" deep-tags-named [
"src" attr " " write http-image.
] each
] bi
] each ;
: wolfram-text. ( query -- )
query "pod" tags-named [
[ "title" attr "%s:\n" printf ]
[
"plaintext" deep-tags-named [
children>string string-lines
[ " %s\n" printf ] each
] each
] bi
] each ;
: wolfram. ( query -- )
ui-running? [ wolfram-image. ] [ wolfram-text. ] if ;