-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcached-http-request.lisp
More file actions
23 lines (19 loc) · 941 Bytes
/
cached-http-request.lisp
File metadata and controls
23 lines (19 loc) · 941 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
(in-package :language)
(defun cache-path ()
(concatenate 'string (base-path) "cache/"))
(defun cache-filename-from-url (url)
(concatenate 'string (cache-path)
(substitute #\_ #\? (substitute #\| #\/ url))))
(defun cached-http-request (url &key binary)
(ensure-directories-exist (cache-path))
(let ((filename (cache-filename-from-url url)))
(if (probe-file filename)
(with-input-from-file (stream filename :element-type (if binary '(unsigned-byte 8) 'character))
(let ((seq (make-array (file-length stream)
:element-type (if binary '(unsigned-byte 8) 'character))))
(read-sequence seq stream)
seq))
(let ((data (http-request url :external-format-out :utf8 :user-agent :firefox)))
(with-output-to-file (stream filename :element-type (if binary '(unsigned-byte 8) 'character))
(write-sequence data stream))
data))))