Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wheee, fix forecast problem in issue #4 #19

Merged
4 commits merged into from
Oct 26, 2010
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/sexpbot/plugins/leet.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
(ns sexpbot.plugins.leet
(:use sexpbot.respond)
)

(:use sexpbot.respond))

(defn char-to-leet [c]
(condp = c
Expand Down
3 changes: 1 addition & 2 deletions src/sexpbot/plugins/lmgtfy.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(ns sexpbot.plugins.lmgtfy
(:use [sexpbot respond])
)
(:use [sexpbot respond]))

(defn create-url [args]
(str "http://www.lmgtfy.com/?q=" (apply str (interpose "+" args))))
Expand Down
1 change: 0 additions & 1 deletion src/sexpbot/plugins/notifo.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
; http://your-server-ip:8080/notifo as your Notification Webhook URL
; * Enjoy


(ns sexpbot.plugins.notifo
(:use sexpbot.respond
[compojure.core :only [POST]]))
Expand Down
3 changes: 1 addition & 2 deletions src/sexpbot/plugins/rss.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
[sexpbot.plugins.shorturl :only [shorten-url]])
(:require [clojure.xml :as xml]
[clojure.zip :as zip]
[clojure.contrib.zip-filter.xml :as zf]
))
[clojure.contrib.zip-filter.xml :as zf]))

(defn cull [zipper]
(let [items (take 3 (zf/xml-> zipper :channel :item))
Expand Down
1 change: 0 additions & 1 deletion src/sexpbot/plugins/sed.clj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
(when-not (= orig-msg new-msg) (send-message irc bot channel (str "<" user-to "> " new-msg))))
(catch Exception _ (when verbose? (format-msg irc bot nick channel)))))))


(defplugin
(:hook
:on-message
Expand Down
40 changes: 23 additions & 17 deletions src/sexpbot/plugins/weather.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,43 @@
[clojure.contrib.str-utils :only [re-sub]])
(:require [clojure.xml :as xml]
[clojure.zip :as zip]
[clojure.contrib.zip-filter.xml :as zf]
))
[clojure.contrib.zip-filter.xml :as zf])
(:import [org.apache.commons.lang StringEscapeUtils]))

(def forcasturl "http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=")
(def forecasturl "http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=")

(defn cull [zipper]
(let [[date & more] (zf/xml-> zipper :txt_forecast :date zf/text)
[pdate & more] (zf/xml-> zipper :simpleforecast :forecastday :date :pretty_short zf/text)
ftext (zf/xml-> zipper :txt_forecast :forecastday :fcttext zf/text)
[hi & more] (zf/xml-> zipper :simpleforecast :forecastday :high :celsius zf/text)
[low & more] (zf/xml-> zipper :simpleforecast :forecastday :low :celsius zf/text)
[condition & more] (zf/xml-> zipper :simpleforecast :forecastday :conditions zf/text)]
(let [date (zf/xml1-> zipper :txt_forecast :date zf/text)
pdate (zf/xml1-> zipper :simpleforecast :forecastday :date :pretty_short zf/text)
ftext (zf/xml-> zipper :txt_forecast :forecastday :fcttext zf/text)
hi (zf/xml1-> zipper :simpleforecast :forecastday :high :celsius zf/text)
low (zf/xml1-> zipper :simpleforecast :forecastday :low :celsius zf/text)
condition (zf/xml1-> zipper :simpleforecast :forecastday :conditions zf/text)]
(if (seq date)
[date ftext]
[pdate (str "High: " hi " Low: " low " Conditions: " condition)])))

(defn strip-space [s]
(re-sub #", " "," (apply str (seq s))))

(defn unescape [str]
(if-not (coll? str)
(StringEscapeUtils/unescapeHtml str)
(map unescape str)))

(defn get-fcst [query]
(->> query
strip-space
(#(.replace % " " "%20"))
(str forcasturl)
xml/parse
zip/xml-zip
cull))
(-> query
strip-space
(.replace " " "%20")
(->> (str forecasturl))
xml/parse
zip/xml-zip
cull
unescape))

(defplugin
(:cmd
"Get's the forecast for a location. Can take a zipcode, or a City, State combination."
"Gets the forecast for a location. Can take a zipcode, or a City, State combination."
#{"fcst"}
(fn [{:keys [irc bot channel nick args]}]
(let [[date [today tonight :as a]] (->> args (interpose " ") get-fcst)
Expand Down
21 changes: 16 additions & 5 deletions src/sexpbot/respond.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,23 @@
(defmacro def- [name & value]
(concat (list 'def (with-meta name (assoc (meta name) :private true))) value))

;; I'm sleepy, so I'm using loop. Don't judge me. Fuck off.
;; This version wastes a little time scanning through fns even after the
;; final is known to be nil, and won't work with infinite sequences of fns.
;; But I think it's a lot clearer and more idiomatic.
(defn nil-comp [irc bot channel s action? & fns]
(loop [cs s f fns]
(if (seq f)
(when-let [new-s ((first f) irc bot channel cs action?)]
(recur new-s (next f)))
(reduce #(when %1
(%2 irc bot channel %1 action?))
s fns))

;; Adjusted this version to use destructuring at least; with the cryptic
;; variable names it took me a while to figure out what was going on, but
;; this version should work identically to the previous. Uncomment if you
;; like it better.
#_(defn nil-comp [irc bot channel s action? & fns]
(loop [cs s [f & fs] fns]
(if f
(when-let [cs (f irc bot channel cs action?)]
(recur cs fs))
cs)))

(defn pull-hooks [bot hook-key]
Expand Down