hy/eg/twisted/get-page.hy

40 lines
1.3 KiB
Hy
Raw Normal View History

2013-06-25 19:54:40 +02:00
;; To run this example, do the following:
;; $ hy get-page.hy http://docs.hylang.org/en/latest/
;;
;; At which point, you should see output like this:
;; 2013-06-24 23:03:57-0700 [-] Log opened.
;; 2013-06-24 23:03:57-0700 [-] Starting factory <HTTPClientFactory: http://docs.hylang.org/en/latest/>
;; 2013-06-24 23:03:57-0700 [HTTPPageGetter,client] Byte count for the content of the HTTP page passed: 11835
;; 2013-06-24 23:03:57-0700 [HTTPPageGetter,client] Preparing to stop reactor ...
;; 2013-06-24 23:03:57-0700 [HTTPPageGetter,client] Stopping factory <HTTPClientFactory: http://docs.hylang.org/en/latest/>
;; 2013-06-24 23:03:57-0700 [-] Main loop terminated.
(import sys)
(import [twisted.web.client [getPage]]
[twisted.internet [reactor]]
[twisted.python [log]])
(defn get-page-size [result]
2013-06-25 19:54:40 +02:00
(print
(+ "Byte count for the content of the HTTP page passed: "
(str (len result)))))
(defn log-error [err]
2013-06-25 19:54:40 +02:00
(log.msg err))
(defn finish [ignore]
2013-06-25 19:54:40 +02:00
(log.msg "Preparing to stop reactor ...")
(reactor.stop))
(defn get-page [url]
(let [[d (getPage url)]]
2013-06-25 19:54:40 +02:00
(d.addCallback get-page-size)
(d.addErrback log-error)
(d.addCallback finish)))
(if (= __name__ "__main__")
(do
(log.startLogging sys.stdout)
(get-page (second sys.argv))
2013-06-25 19:54:40 +02:00
(reactor.run)))