Created
August 19, 2011 06:45
-
-
Save oneness/1156214 to your computer and use it in GitHub Desktop.
gcount
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) |
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
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"]
])