]> gitweb.factorcode.org Git - factor.git/commitdiff
google.search: adding simple wrapper for Google Search API.
authorJohn Benediktsson <mrjbq7@gmail.com>
Tue, 24 Jul 2012 18:57:42 +0000 (11:57 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 24 Jul 2012 18:57:42 +0000 (11:57 -0700)
extra/google/search/authors.txt [new file with mode: 0644]
extra/google/search/search.factor [new file with mode: 0644]
extra/google/search/summary.txt [new file with mode: 0644]
extra/google/translate/summary.txt [new file with mode: 0644]

diff --git a/extra/google/search/authors.txt b/extra/google/search/authors.txt
new file mode 100644 (file)
index 0000000..e091bb8
--- /dev/null
@@ -0,0 +1 @@
+John Benediktsson
diff --git a/extra/google/search/search.factor b/extra/google/search/search.factor
new file mode 100644 (file)
index 0000000..0120459
--- /dev/null
@@ -0,0 +1,67 @@
+! Copyright (C) 2011 John Benediktsson
+! See http://factorcode.org/license.txt for BSD license
+
+USING: accessors arrays assocs assocs.extras classes.tuple
+colors.constants combinators formatting fry http.client io
+io.styles json.reader kernel sequences urls wrap.strings ;
+
+IN: google.search
+
+<PRIVATE
+
+: search-url ( query -- url )
+    URL" http://ajax.googleapis.com/ajax/services/search/web"
+        "1.0" "v" set-query-param
+        swap "q" set-query-param
+        "8" "rsz" set-query-param
+        "0" "start" set-query-param ;
+
+: set-slots ( assoc obj -- )
+    '[ swap _ set-slot-named ] assoc-each ;
+
+: from-slots ( assoc class -- obj )
+    new [ set-slots ] keep ;
+
+TUPLE: search-result cacheUrl GsearchResultClass visibleUrl
+title content unescapedUrl url titleNoFormatting ;
+
+PRIVATE>
+
+: http-search ( query -- results )
+    search-url http-get nip json>
+    { "responseData" "results" } deep-at
+    [ \ search-result from-slots ] map ;
+
+<PRIVATE
+
+: write-heading ( str -- )
+    H{
+        { font-size 14 }
+        { background COLOR: light-gray }
+    } format nl ;
+
+: write-title ( str -- )
+    H{
+        { foreground COLOR: blue }
+    } format nl ;
+
+: write-content ( str -- )
+    60 wrap-string print ;
+
+: write-url ( str -- )
+    dup >url H{
+        { font-name "monospace" }
+        { foreground COLOR: dark-green }
+    } [ write-object ] with-style nl ;
+
+PRIVATE>
+
+: http-search. ( query -- )
+    [ "Search results for '%s'" sprintf write-heading nl ]
+    [ http-search ] bi [
+        {
+            [ titleNoFormatting>> write-title ]
+            [ content>> write-content ]
+            [ unescapedUrl>> write-url ]
+        } cleave nl
+    ] each ;
diff --git a/extra/google/search/summary.txt b/extra/google/search/summary.txt
new file mode 100644 (file)
index 0000000..8ceb1a5
--- /dev/null
@@ -0,0 +1 @@
+Google Search API
diff --git a/extra/google/translate/summary.txt b/extra/google/translate/summary.txt
new file mode 100644 (file)
index 0000000..1ad2e5f
--- /dev/null
@@ -0,0 +1 @@
+Google Translate API