Created
January 7, 2012 01:07
-
-
Save ga2arch/1573335 to your computer and use it in GitHub Desktop.
pfetch
This file contains hidden or 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 clj-pfetch.core | |
| (:use [clojure.java.shell :only (sh)]) | |
| (:require [clojure.xml :as xml])) | |
| ;;; Records | |
| (defrecord Url [start href output freq args]) | |
| ;;; Agents | |
| (def running (agent true)) | |
| ;;; Methods | |
| (defn- read-file [filename] | |
| (slurp filename)) | |
| (defn- parse-xml [data] | |
| (-> (.getBytes data "UTF-8") | |
| input-stream | |
| xml/parse | |
| xml-seq)) | |
| (defn- make-url [data] | |
| (let [attrs (:attrs data) | |
| href (:href attrs) | |
| output (:output attrs) | |
| freq (Integer/parseInt (:freq attrs)) | |
| content (:content data) | |
| path (:path (:attrs (first content))) | |
| args (map #(first (:content %)) (:content (first content)))] | |
| (Url. (rand-int 10) href output freq (cons path args)))) | |
| (defn- make-urls [x] | |
| (let [r-urls (:content (first x))] | |
| (map make-url r-urls))) | |
| (defn exec [args] | |
| (try | |
| (apply sh args) | |
| (catch Exception e | |
| nil))) | |
| (defn- process-url [url] | |
| (Thread/sleep (* (:start url) 1000)) | |
| (loop [] | |
| (let [content (slurp (:href url))] | |
| (spit (:output url) content) | |
| (exec (:args url)) | |
| (Thread/sleep (* (:freq url) 1000)) | |
| (if @running | |
| (recur) | |
| nil)))) | |
| (defn process-urls [urls] | |
| (map #(send (agent %) process-url) urls)) | |
| ;;; Main | |
| (defn -main [filename] | |
| (let [content (read-file filename) | |
| p-xml (parse-xml content) | |
| urls (make-urls p-xml)] | |
| (process-urls urls))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment