Skip to content

Instantly share code, notes, and snippets.

@oneness
Created August 19, 2011 06:45
Show Gist options
  • Save oneness/1156214 to your computer and use it in GitHub Desktop.
Save oneness/1156214 to your computer and use it in GitHub Desktop.
gcount
(ns gcount.core
"generates charts out of the result from google search term(s)."
(:use (incanter core charts))
(:require [clj-http.client :as http-client]))
(def *search-provider* "http://www.google.com/search?hl=en&q=")
(def *search-pattern* #"About.*?([\d,]+).*?")
(defn search-for-term [term]
(let [encoded-term (.replaceAll (apply str term) " " "+")
qstring (str *search-provider* encoded-term)
page (:body (http-client/get qstring))
hits (second (re-find *search-pattern* page))
nhits (Integer/parseInt (.replaceAll hits "," ""))]
(hash-map term nhits)))
(defn view-bar-chart [mcoll]
(view (bar-chart (keys mcoll) (vals mcoll)
:x-label "Search terms"
:y-label "Search results")))
(defn search-view-terms [terms]
(->> (reduce merge (map search-for-term terms))
(view-bar-chart)))
@tce
Copy link

tce commented Feb 26, 2012

Here's a lein project file for this gist:

(defproject gcount "1.0.0-SNAPSHOT"
:description "FIXME: write description"
:dependencies [[org.clojure/clojure "1.3.0"]
[clj-http "0.3.2"]
[incanter "1.3.0"]
])

@tce
Copy link

tce commented Feb 26, 2012

Also, in search-for-term, instead of Integer/parseInt, I used Long/parseLong. That way, if one of your search terms is "search", you won't get an error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment