git.fiddlerwoaroof.com
Browse code

Cleanup feed-archive, add patmatch to dependencies

fiddlerwoaroof authored on 24/08/2017 07:40:38
Showing 2 changed files
... ...
@@ -5,6 +5,7 @@
5 5
   :depends-on (#:alexandria
6 6
                #:alimenta
7 7
                #:fwoar.lisputils
8
+               #:alimenta+patmatch
8 9
                #:ironclad
9 10
                #:local-time
10 11
                #:serapeum
... ...
@@ -99,9 +99,13 @@
99 99
 (defun feed-index (index-stream pull-time references)
100 100
   (yason:with-output (index-stream :indent t)
101 101
     (yason:encode-object
102
-     (make-feed-index pull-time (remove-if 'null references)))))
102
+     (make-instance 'feed-index
103
+                    :pull-time pull-time
104
+                    :references (remove-if 'null references)))))
105
+
103 106
 
104 107
 (defun pull-and-store-feed (feed-url stream-provider &optional (feed-puller #'safe-pull-feed))
108
+  (declare (optimize (debug 3)))
105 109
   (flet ((log-pull (stream)
106 110
            (declare (inline) (dynamic-extent stream))
107 111
            (log-pull feed-puller feed-url stream))
... ...
@@ -110,19 +114,23 @@
110 114
            (log-serialization feed-url stream feed
111 115
                               (merge-pathnames path
112 116
                                                (stream-provider:root stream-provider)))))
113
-
114
-    (with-simple-restart (skip-feed "Stop processing for ~a" feed-url)
115
-      (let* ((feed (with-retry ("Pull feed again.")
116
-                     (normalize-feed feed-url (log-pull t)))))
117
-        (trivia:match (store feed stream-provider)
118
-          ((list title path)
119
-           (log-serialization t feed path)
120
-           (make-feed-reference (alimenta:feed-link feed)
121
-                                :title title
122
-                                :path (feed-relative-pathname
123
-                                       (uiop:pathname-directory-pathname
124
-                                        (merge-pathnames path
125
-                                                         (stream-provider:root stream-provider)))))))))))
117
+    (handler-bind ((cl+ssl:ssl-error-verify
118
+                    (lambda (c)
119
+                      (declare (ignore c))
120
+                      (format *error-output* "~&SSL Error while pulling ~a~%"
121
+                              feed-url))))
122
+      (with-simple-restart (skip-feed "Stop processing for ~a" feed-url)
123
+        (let* ((feed (with-retry ("Pull feed again.")
124
+                       (normalize-feed feed-url (log-pull t)))))
125
+          (trivia:match (store feed stream-provider)
126
+            ((list title path)
127
+             (log-serialization t feed path)
128
+             (make-feed-reference (alimenta:feed-link feed)
129
+                                  :title title
130
+                                  :path (feed-relative-pathname
131
+                                         (uiop:pathname-directory-pathname
132
+                                          (merge-pathnames path
133
+                                                           (stream-provider:root stream-provider))))))))))))
126 134
 
127 135
 (defun archive-feeds (pull-time pull-directory index-stream)
128 136
   (prog1-bind (references (mapcar (op (pull-and-store-feed _ pull-directory))
... ...
@@ -166,8 +174,9 @@
166 174
       (handler-bind ((alimenta.feed-archive.encoders:feed-error
167 175
                       (op (fix-pathname-or-skip _1 :wrapped-condition (alimenta.feed-archive.encoders:the-condition _1))))
168 176
                      (alimenta:feed-type-unsupported #'feed-type-unsupported)
169
-                     ((or usocket:timeout-error usocket:ns-error
170
-                          ) (op (alimenta.pull-feed:skip-feed _)))
177
+                     ((or usocket:timeout-error usocket:ns-error cl+ssl:ssl-error-verify)
178
+                      (op (alimenta.pull-feed:skip-feed _)))
179
+                     
171 180
                      (error
172 181
                       (op
173 182
                         (format t "~&Error signaled, ~a (count ~d)" _1 error-count)
... ...
@@ -179,3 +188,31 @@
179 188
           (alimenta.pull-feed:with-user-agent ("Feed Archiver v0.1b")
180 189
             (archive-feeds-nondeterm)))))))
181 190
 
191
+
192
+(defpackage :alimenta.feed-archive/tests
193
+  (:use :cl :should-test)
194
+  (:export ))
195
+(in-package :alimenta.feed-archive/tests)
196
+
197
+(eval-when (:compile-toplevel :load-toplevel :execute)
198
+  (import 'alimenta.feed-archive::feed-index))
199
+
200
+(defun hash-table= (ht1 ht2 &key (key-test 'equal) (value-test 'equal))
201
+  (let ((ht1-keys (alexandria:hash-table-keys ht1))
202
+        (ht2-keys (alexandria:hash-table-keys ht2)))
203
+    (and (= (length ht1-keys)
204
+            (length ht2-keys))
205
+         (every key-test ht1-keys ht2-keys)
206
+         (every value-test
207
+                (alexandria:hash-table-values ht1)
208
+                (alexandria:hash-table-values ht2)))))
209
+
210
+(deftest feed-index ()
211
+    (should be hash-table=
212
+            (yason:parse
213
+             (with-output-to-string (s)
214
+               (feed-index s (local-time:encode-timestamp 0 0 0 0 1 1 1) '()))
215
+             :object-as :hash-table :json-arrays-as-vectors nil)
216
+            (alexandria:alist-hash-table
217
+             '(("pull-time" . "0001-01-01T00:00:00.000000-08:00")
218
+               ("feeds" . ())))))