summaryrefslogtreecommitdiff
path: root/src/imgreplacer.clj
diff options
context:
space:
mode:
Diffstat (limited to 'src/imgreplacer.clj')
-rw-r--r--src/imgreplacer.clj41
1 files changed, 26 insertions, 15 deletions
diff --git a/src/imgreplacer.clj b/src/imgreplacer.clj
index bc9b00f..4963e02 100644
--- a/src/imgreplacer.clj
+++ b/src/imgreplacer.clj
@@ -5,6 +5,7 @@
java.io.ByteArrayInputStream
javax.imageio.ImageIO
org.htmlcleaner.HtmlCleaner)
+ (:require [clojure.set :as r])
(:use clojure.contrib.duck-streams
clojure.contrib.str-utils
clojure.contrib.command-line
@@ -50,19 +51,29 @@
(def image-url-map (ref {}))
-(defn replace-bad-images [url dryrun?]
+(defn mirror-message! [msg dryrun url-filter]
+ (let [imgs (filter url-filter
+ (take-safe-images (:content msg)))]
+ (doseq [img imgs]
+ (if-not (contains? @image-url-map img)
+ (dosync (alter image-url-map assoc img (mirror-image img)))))
+ (let [replace-map (zipmap imgs (map @image-url-map imgs))
+ new-content (replace-grp-str replace-map (:content msg))]
+ (if (= (:content msg) new-content)
+ (println (format "Message %s: no change" (:message_id msg)))
+ (do
+ (println "\nupdating content of" (:message_id msg) "from:\n" (:content msg) "\nto:\n" new-content)
+ (if-not dryrun
+ (do-update :messages
+ ["message_id = ?" (:message_id msg)]
+ {:content new-content})))))))
+
+(defn mirror-bad-host! [url dryrun]
(doseq [m (fetch-bad-messages url)]
- (let [imgs (filter #(ins-substring? url %)
- (take-safe-images (:content m)))]
- (doseq [img imgs]
- (if-not (contains? @image-url-map img)
- (if-let [path (mirror-image img)]
- (dosync (alter image-url-map assoc img path))
- (println "Unable to mirror" img))))
- (let [replace-map (zipmap imgs (map @image-url-map imgs))
- new-content (replace-grp-str replace-map (:content m))]
- (println "\nupdating content of" (:message_id m) "from:\n" (:content m) "\nto:\n" new-content)
- (if-not dryrun?
- (do-update :messages
- ["message_id = ?" (:message_id m)]
- {:content new-content}))))))
+ (mirror-message! m dryrun
+ #(ins-substring? url %))))
+
+(defn mirror-message-id! [msg-id dryrun]
+ (if-let [m (first (do-select ["SELECT * FROM messages WHERE message_id = ?" msg-id]))]
+ (mirror-message! m dryrun
+ #(not (re-find #"^http://dump.fm" %))))) \ No newline at end of file